【问题标题】:Actionlistener on a JMenu not workingJMenu 上的 Actionlistener 不起作用
【发布时间】:2012-03-31 12:06:03
【问题描述】:

我有一个 JMenu,我向它添加了一个 Actionlistener,但 Action 侦听器没有做任何事情。

我试图让 mnExit 菜单退出程序 (System.exit(0)),但 Actionlistener 没有做任何事情。

谁能告诉我我做错了什么?

import java.io.IOException;


public class Main {
private int a;
private Server s;
private Client c;
private JFrame j;
private static JTextField ipClient;
private static JTextField hostPort;
private static JTextField portClient;
private static JPasswordField passwordClient;
private static JPasswordField passwordHost;
/**
 * @param args
 * @wbp.parser.entryPoint
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Main start=new Main();
    JFrame j=new JFrame("IControl");
    j.setSize(new Dimension(448, 291));
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setSize(700,700);
    j.getContentPane().setLayout(null);

    JButton clientButton = new JButton("connect to server");
    clientButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Client c=new Client("localhost","bbc");
        }
    });
    clientButton.setBounds(32, 209, 154, 23);
    j.getContentPane().add(clientButton);

    JButton serverButton = new JButton("wait for connection");
    serverButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Server s=new Server("abc");
        }
    });
    serverButton.setBounds(278, 209, 154, 23);
    j.getContentPane().add(serverButton);

    ipClient = new JTextField();
    ipClient.setBounds(67, 96, 123, 20);
    j.getContentPane().add(ipClient);
    ipClient.setColumns(10);

    hostPort = new JTextField();
    hostPort.setBounds(298, 130, 86, 20);
    j.getContentPane().add(hostPort);
    hostPort.setColumns(10);

    JLabel lblIp = new JLabel("IP");
    lblIp.setBounds(25, 99, 32, 14);
    j.getContentPane().add(lblIp);

    JLabel lblPort = new JLabel("port");
    lblPort.setBounds(25, 133, 32, 14);
    j.getContentPane().add(lblPort);

    portClient = new JTextField();
    portClient.setBounds(67, 130, 86, 20);
    j.getContentPane().add(portClient);
    portClient.setColumns(10);

    passwordClient = new JPasswordField();
    passwordClient.setBounds(67, 161, 86, 20);
    j.getContentPane().add(passwordClient);

    passwordHost = new JPasswordField();
    passwordHost.setBounds(298, 161, 86, 20);
    j.getContentPane().add(passwordHost);

    JLabel lblPass = new JLabel("pass");
    lblPass.setBounds(32, 158, 32, 14);
    j.getContentPane().add(lblPass);



    JMenuBar menuBar = new JMenuBar();
    menuBar.setBounds(0, 0, 93, 21);
    j.getContentPane().add(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenu mnExit = new JMenu("Exit");
    mnExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
             System.exit(0);
        }
    });
    mnFile.add(mnExit);


    j.setVisible(true);

//  j.pack();
}
/**
 * @wbp.parser.entryPoint
 */
public Main()
{

}

}

【问题讨论】:

    标签: java swing actionlistener jmenu jmenuitem


    【解决方案1】:

    你必须从

    更改代码行
    JMenu mnExit = new JMenu("Exit");
    

    JMenuItem mnExit = new JMenuItem("Exit");
    

    因为你定义了JMenu而不是JMenuItem,但是不要忘记创建JMenu然后把JMenuItem放在那里,更多关于JMenu / JMenuItem的教程中

    【讨论】:

    • @user1272067:是的,mKorbel 是对的 (1+)。还可以考虑通过menu tutorials,因为它在那里进行了解释。当您使用它时,请考虑查看layout manager tutorials,因为它们可以让您的 GUI 生活更轻松。
    【解决方案2】:

    因为 jmenu 不会触发它自己的动作监听器,所以最好的选择是在它上面使用鼠标事件。类似的东西;

    jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuMouseClicked(evt);
            }
    

    ......

    private void jMenuMouseClicked(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        if(evt.getButton() == MouseEvent.BUTTON1)
        System.out.println("good");
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多