为什么这个 Java Swing 菜单项上的 actionEvent 不起作用?

huangapple 未分类评论49阅读模式
英文:

Why this actionEvent on a Java Swing menu item does not work?

问题

我使用NetBeans表单设计器创建了一个基本的JFrame。我已将其简化为仅包含几个菜单项。

我打算在用户单击“Open”时执行某些操作,我理解我需要通过侦听器添加一个“actionPerformed”,这可以通过NetBeans Designer完成。

private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    System.out.println("ACTION PERFORMED");
}  

但是,当我点击“Open”时,什么都没有发生,我看不到输出消息。

这是我的主要程序:

package test.dbviewer;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SealionDBViewer {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        
        UIManager.setLookAndFeel ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        JFrame databaseViewer = new DatabaseViewer();
        databaseViewer.setVisible(true);
    }
    
}

这是NetBeans生成的可重现代码:

package test.dbviewer;

public class DatabaseViewer extends javax.swing.JFrame {

    public DatabaseViewer() {
        initComponents();
    }

    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();

        jMenuItem1.setText("jMenuItem1");

        jMenuItem2.setText("jMenuItem2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu3.setText("Open");
        jMenu3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu3ActionPerformed(evt);
            }
        });
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Exit");
        jMenuBar2.add(jMenu4);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 267, Short.MAX_VALUE)
        );

        pack();
    }

    private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        System.out.println("ACTION PERFORMED");
    } 

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        // ...

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DatabaseViewer().setVisible(true);
            }
        });
    }

    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JSplitPane jSplitPane1;
}
英文:

I have created a basic JFrame using NetBeans form designer. I have reduced it to just a couple of menu items.

I intend to do something when user clicks on "Open", my understanding is that I have to add an "actionPerformed" through a listener, which can be done using NetBeans Designer.

为什么这个 Java Swing 菜单项上的 actionEvent 不起作用?

    private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        System.out.println("ACTION PERFORMED");
    }                  

But when I click on "Open" nothing happens I can not see the output message.

This is my main routine:

<!-- language: java -->

package test.dbviewer;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SealionDBViewer {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        
        UIManager.setLookAndFeel (&quot;com.sun.java.swing.plaf.motif.MotifLookAndFeel&quot;);
        JFrame databaseViewer = new DatabaseViewer();
        databaseViewer.setVisible(true);
    }
    
}

This is the reproducible code that NetBeans generate:

<!-- language: java -->

package test.dbviewer;


public class DatabaseViewer extends javax.swing.JFrame {

    /**
     * Creates new form DatabaseViewer
     */
    public DatabaseViewer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings(&quot;unchecked&quot;)
    // &lt;editor-fold defaultstate=&quot;collapsed&quot; desc=&quot;Generated Code&quot;&gt;                          
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();

        jMenuItem1.setText(&quot;jMenuItem1&quot;);

        jMenuItem2.setText(&quot;jMenuItem2&quot;);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu3.setText(&quot;Open&quot;);
        jMenu3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu3ActionPerformed(evt);
            }
        });
        jMenuBar2.add(jMenu3);

        jMenu4.setText(&quot;Exit&quot;);
        jMenuBar2.add(jMenu4);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 267, Short.MAX_VALUE)
        );

        pack();
    }// &lt;/editor-fold&gt;                        

    private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        System.out.println(&quot;ACTION PERFORMED&quot;);
    }                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //&lt;editor-fold defaultstate=&quot;collapsed&quot; desc=&quot; Look and feel setting code (optional) &quot;&gt;
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if (&quot;Nimbus&quot;.equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DatabaseViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //&lt;/editor-fold&gt;

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DatabaseViewer().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration                   
}

huangapple
  • 本文由 发表于 2020年5月19日 19:26:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61889893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定