如果我对代码进行注释,JButtons 就会消失
public class analisi_spesa {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
/*
JLabel headerLabel = new JLabel("headerLabel",JLabel.CENTER );
JLabel statusLabel = new JLabel("statusLabel",JLabel.CENTER);
statusLabel.setSize(350,100);
*/
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
/*
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
*/
mainFrame.setVisible(true);
// headerLabel.setText("Control in action: Button");
JButton okButton = new JButton("OK");
JButton submitButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
}
我希望它们能显示出来,因为我没有对代码进行注释。我从 https://www.tutorialspoint.com/swing/swing_event_handling.htm 中拿了这个例子 谢谢大家,我在等待答案。
最佳答案
不要评论这部分:
mainFrame.add(controlPanel);
因为以后你会做
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
因此您将所有按钮添加到“controlPanel” 但是如果你不添加 controlPanel 到你的 mainFrame,按钮将不会显示
关于java - 如果我注释了一些代码,JButton 就会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54646648/