将弹出菜单的所选项目添加到相同形式的文本字段中



如何将弹出菜单的所选项目添加到相同形式的文本字段中?

我使用Swing创建了一个表单。我在下面张贴代码。我希望将弹出菜单中选定的项目添加到文本字段中。因为我对这个话题还不熟悉。请帮帮我。

我的目标是在选择jmenuitem1时将其存储在textfield1中。

 public class XXX extends javax.swing.JFrame {
    public XXX() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {
        jPopupMenu1 = new javax.swing.JPopupMenu();
        John = new javax.swing.JMenuItem();
        Smith = new javax.swing.JMenuItem();
        Jacob = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        John.setText("jMenuItem1");
        John.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JohnActionPerformed(evt);
            }
        });
        jPopupMenu1.add(John);
        Smith.setText("jMenuItem2");
        jPopupMenu1.add(Smith);
        Jacob.setText("jMenuItem3");
        jPopupMenu1.add(Jacob);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("Name");
        jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTextField1MouseReleased(evt);
            }
        });
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jLabel2.setText("CLASS");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                .addContainerGap(226, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(226, Short.MAX_VALUE))
        );
        pack();
    }
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
    }
    private void JohnActionPerformed(java.awt.event.ActionEvent evt) {
    }
    private void jTextField1MouseReleased(java.awt.event.MouseEvent evt) {
  if(evt.isPopupTrigger())
        {
            jPopupMenu1.show(this,evt.getX(),evt.getY());
        }        
    }

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(XXX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(XXX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(XXX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(XXX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new XXX().setVisible(true);
            }
        });
    }
    private javax.swing.JMenuItem Jacob;
    private javax.swing.JMenuItem John;
    private javax.swing.JMenuItem Smith;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    }

您需要使用JTextFieldsetText()方法。以及ActionEventgetSource(),用于确定所选的JMenuItem`。

您可以通过以下方式存储JMenuItem的文本:

private void JohnActionPerformed(java.awt.event.ActionEvent evt) {
    JMenuItem source = (JMenuItem) evt.getSource();
    jTextField1.setText(source.getText());
}

如果需要存储对象(JMenuItem),则需要使用实现扩展JTextField

相关内容

  • 没有找到相关文章

最新更新