进入GUI并试图简化一些事情。但是,我似乎只是在制作一个简单的“退出”按钮。我正在使用Eclipse的WindowsBuilder插件来帮助我。
private void initialize()
{
mainWindowFrame = new JFrame();
mainWindowFrame.setTitle("Lock and Log");
mainWindowFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("/lockandlog/main/resources/icon.png")));
mainWindowFrame.setResizable(false);
mainWindowFrame.setBounds(100, 100, 854, 480);
mainWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindowFrame.getContentPane().setLayout(null);
final JTextArea logDisplay = new JTextArea();
logDisplay.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
logDisplay.setEditable(false);
logDisplay.setBounds(259, 11, 579, 250);
mainWindowFrame.getContentPane().add(logDisplay);
JLabel viewCmdDisplayLbl = new JLabel("View Command Display:");
viewCmdDisplayLbl.setFont(new Font("Tahoma", Font.BOLD, 11));
viewCmdDisplayLbl.setBounds(259, 272, 133, 14);
mainWindowFrame.getContentPane().add(viewCmdDisplayLbl);
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
textArea.setBounds(259, 297, 579, 104);
mainWindowFrame.getContentPane().add(textArea);
JToolBar toolBar = new JToolBar();
toolBar.setBounds(0, 412, 848, 16);
mainWindowFrame.getContentPane().add(toolBar);
JPanel manualGrp = new JPanel();
manualGrp.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
manualGrp.setBounds(10, 11, 239, 72);
mainWindowFrame.getContentPane().add(manualGrp);
manualGrp.setLayout(null);
manualTxt = new JTextField();
manualTxt.setBounds(10, 7, 219, 20);
manualGrp.add(manualTxt);
manualTxt.setColumns(10);
JButton manualBtn = new JButton("Manual Override");
manualBtn.setFont(new Font("Tahoma", Font.BOLD, 12));
manualBtn.setBounds(10, 38, 219, 23);
manualGrp.add(manualBtn);
JMenuBar menuBar = new JMenuBar();
mainWindowFrame.setJMenuBar(menuBar);
final JMenu fileBtn = new JMenu("File");
fileBtn.addMouseListener(new MouseAdapter() //This works
{
@Override
public void mouseEntered(MouseEvent e)
{
fileBtn.setSelected(true);
}
@Override
public void mouseExited(MouseEvent e)
{
fileBtn.setSelected(false);
}
});
menuBar.add(fileBtn);
JMenuItem exitBtn = new JMenuItem("Exit");
//Code is definitely passing through here.
exitBtn.addMouseListener(new MouseAdapter() //This doesn't work?
{
@Override
public void mouseClicked(MouseEvent e)
{
logDisplay.setText("Test"); //This isn't even being called!
System.exit(0);
}
});
//Definitely adds the button so code is passing through here.
fileBtn.add(exitBtn);
}
这就是现在的整个初始化类。但是如果你注意到底部附近我有一个退出按钮的事件,如果你点击它,程序应该关闭。除此之外,当我点击按钮时没有任何反应。在fileBtn中尝试其他类似的事件,效果很好。那么我做错了什么?
答案 0 :(得分:3)
不要使用mouseListeners,请使用ActionListeners。
import java.awt.event.*;
public class SOQ3
{
JFrame mainWindowFrame;
public static void main(String[] args)
{
SOQ3 s = new SOQ3();
}
public SOQ3()
{
initialize();
}
private void initialize()
{
mainWindowFrame = new JFrame();
mainWindowFrame.setTitle("Lock and Log");
mainWindowFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("/lockandlog/main/resources/icon.png")));
mainWindowFrame.setResizable(false);
mainWindowFrame.setBounds(100, 100, 854, 480);
mainWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindowFrame.getContentPane().setLayout(null);
final JTextArea logDisplay = new JTextArea();
logDisplay.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
logDisplay.setEditable(false);
logDisplay.setBounds(259, 11, 579, 250);
mainWindowFrame.getContentPane().add(logDisplay);
JLabel viewCmdDisplayLbl = new JLabel("View Command Display:");
viewCmdDisplayLbl.setFont(new Font("Tahoma", Font.BOLD, 11));
viewCmdDisplayLbl.setBounds(259, 272, 133, 14);
mainWindowFrame.getContentPane().add(viewCmdDisplayLbl);
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
textArea.setBounds(259, 297, 579, 104);
mainWindowFrame.getContentPane().add(textArea);
JToolBar toolBar = new JToolBar();
toolBar.setBounds(0, 412, 848, 16);
mainWindowFrame.getContentPane().add(toolBar);
JPanel manualGrp = new JPanel();
manualGrp.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
manualGrp.setBounds(10, 11, 239, 72);
mainWindowFrame.getContentPane().add(manualGrp);
manualGrp.setLayout(null);
manualTxt = new JTextField();
manualTxt.setBounds(10, 7, 219, 20);
manualGrp.add(manualTxt);
manualTxt.setColumns(10);
JButton manualBtn = new JButton("Manual Override");
manualBtn.setFont(new Font("Tahoma", Font.BOLD, 12));
manualBtn.setBounds(10, 38, 219, 23);
manualGrp.add(manualBtn);
JMenuBar menuBar = new JMenuBar();
mainWindowFrame.setJMenuBar(menuBar);
final JMenu fileBtn = new JMenu("File");
fileBtn.addMouseListener(
new MouseAdapter() //This works
{
@Override
public void mouseEntered(MouseEvent e)
{
fileBtn.setSelected(true);
}
@Override
public void mouseExited(MouseEvent e)
{
fileBtn.setSelected(false);
}
});
menuBar.add(fileBtn);
JMenuItem exitBtn = new JMenuItem("Exit");
//Code is definitely passing through here.
exitBtn.addActionListener(
new ActionListener() //This doesn't work?
{
@Override
public void actionPerformed(ActionEvent e)
{
logDisplay.setText("Test"); //This isn't even being called!
System.exit(0);
}
});
//Definitely adds the button so code is passing through here.
fileBtn.add(exitBtn);
}
}
现在至于这背后的原因,你试图使用MouseListeners,而MouseListeners是好的,你需要获得你正在听的内容。简单地添加一个监听器现在可以做任何类似于ActionListener的事情,这也是我选择使用它的部分原因。另外,给你提示,使用ActionListeners监听由它附加的对象完成的Action,MouseListeners监听你及其所做的一切,无论它是否与你的程序有任何关系。将来,在使用听众时,请确保他们只听取您的程序绝对必要的内容。如果您不需要知道鼠标的位置,请不要遵循它。
答案 1 :(得分:0)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
通常这是捕获退出事件的方式。
要使用自定义退出按钮,而不是MouseListener
,请尝试使用简单的ActionListener
然后将捕获该按钮上的任何事件,并正常退出您的代码。