java - 将弹出菜单的选定项目添加到相同格式的文本字段中

标签 java swing actionlistener jtextfield jpopup

如何将弹出菜单的选定项添加到相同表单的文本字段中?

我使用 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;
    }

最佳答案

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

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

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

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

关于java - 将弹出菜单的选定项目添加到相同格式的文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20560923/

相关文章:

java - ActionListener 是如何工作的?

java - 使用 Action 监听器实现颜色变化

java - 如何从不同的 Activity 编辑 EditText

java - 如何为服务器端的每个 websocket session 创建相互映射?

java - 仅 EDT 代码中调用 getPreferredSize() 时发生 BoxLayout 异常

java - 为什么我可以在链接方法时省略 import 语句,但如果我单独调用它们则不能?

java - 如何在actionListener(java)中共享变量

java - 在 Glassfish 中运行的应用程序可以访问哪些域文件?

java - 在 Java 中向 Neo4j 服务器执行 Cypher 查询时出现 UnsupportedOperationException

java - 将 JFrame 传递给新创建的类以从新类接收日志数据