2012-03-31 92 views
1

我有一個JMenu,並且我添加了一個Actionlistener,但Action listener沒有做任何事情。JMenu上的Actionlistener不起作用

我想使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() 
{ 

} 

}

回答

4

你必須改變代碼行從

JMenu mnExit = new JMenu("Exit"); 

JMenuItem mnExit = new JMenuItem("Exit"); 

因爲你定義JMenu,而不是JMenuItem,但不要忘記創建JMenu然後放在那裏JMenuItem,更多教程JMenu/JMenuItem

+3

@ user1272067:是的,mKorbel是正確的(1+)。也可以考慮閱讀[菜單教程](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html),因爲它在那裏都有說明。當你處理它時,考慮看看[佈局管理器教程](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html),因爲它們可以讓你的GUI生活變得更容易。 – 2012-03-31 12:14:38

+0

非常感謝。 – user1272067 2012-03-31 13:57:29

+0

很高興爲您效勞 – mKorbel 2012-03-31 13:58:15