JButton disappear if I comment a bit of code

问题: if I comment the code as shown the JButtons disappear public class analisi_spesa { public static void main(String[] args) { JFrame mainFrame = new JFrame("Java SWIN...

问题:

if I comment the code as shown the JButtons disappear

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);

}

}

I wish they were shown as I don't comment the code. I took this example from https://www.tutorialspoint.com/swing/swing_event_handling.htm thank you all, I'm waiting for the answers.


回答1:

do not comment this part:

mainFrame.add(controlPanel);

because later you do

controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton); 

so you add all your buttons to "controlPanel" but if you dont add controlPanel to your mainFrame, the buttons will not show


回答2:

You have a BLOCK comment that wraps around:

mainFrame.add(controlPanel);

Therefore that line isn't executed, therefore your panel with buttons isn't added to your frame.

That is all there is to this. The real answer here: details matter. Each and any character that you put into your source code matters.


回答3:

controlPanel is never added to mainFrame because you commented it. Also, components should be added to the contentPane of mainFrame, not to mainFrame itself:

mainFrame.getContentPane().add(controlPanel);

instead of:

mainFrame.add(controlPanel);

Also, set the layout of the contentPane:

mainFrame.getContentPane().setLayout(new GridLayout(3, 1));
  • 发表于 2019-02-13 18:58
  • 阅读 ( 164 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除