Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton不显示_Java_Swing_Jframe_Jbutton_Layout Manager - Fatal编程技术网

Java JButton不显示

Java JButton不显示,java,swing,jframe,jbutton,layout-manager,Java,Swing,Jframe,Jbutton,Layout Manager,嗨,有人知道为什么我的“按钮1”没有显示吗?当我执行程序时,我似乎无法理解它,它可以正常工作并成功运行,但它没有显示此按钮。任何帮助都将不胜感激 private Container c; private JPanel gridPanel; private JComboBox combo; final JLabel label = new JLabel(); private JButton button1 = new JButton("Clear"); private JButton button

嗨,有人知道为什么我的“按钮1”没有显示吗?当我执行程序时,我似乎无法理解它,它可以正常工作并成功运行,但它没有显示此按钮。任何帮助都将不胜感激

private Container c;
private JPanel gridPanel;
private JComboBox combo;
final JLabel label = new JLabel();
private JButton button1 = new JButton("Clear");
private JButton button2 = new JButton("Exit");

/**
 * Christopher Haddad - 559815X
 */
public Planets() {
    c = getContentPane();
    gridPanel = new JPanel();
    gridPanel.setLayout(new GridLayout(5, 0, 0, 0));

    label.setVisible(true);

    combo = new JComboBox(); 
    combo.setEditable(false);
    combo.addItem("No Planet Selected");
    combo.addItem("Mercury"); 
    combo.addItem("Venus"); 
    combo.addItem("Earth"); 
    gridPanel.add(combo);

    add(button1);
    add(button2);
    button1.addActionListener(this);
    button2.addActionListener(this);

    c.add(gridPanel, BorderLayout.NORTH);
    setTitle("Planet Diameter"); 
    setSize(700, 250);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    combo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            JComboBox comboBox = (JComboBox) event.getSource();

            Object select = comboBox.getSelectedItem();

            if(select.toString().equals("No Planet Selected"))
                label.setText("");
            else if(select.toString().equals("Mercury"))
                label.setText("The planet Mercury is 3100kms in diameter");
            else if(select.toString().equals("Venus"))
                label.setText("The planet Venus is 7500kms in diameter");
            else if (select.toString().equals("Earth"))
                label.setText("The planet Earth is 8000kms in diameter");

        }
    });
    getContentPane().add(combo);
    getContentPane().add(label);
}

    // event handling method, implementing the actionPerformed method of ActionListener 
    public void actionPerformed(ActionEvent e) 
    { 
        // set the button label to number of times it has been clicked
        if(e.getSource() == button1) {
            label.setText(" ");
        }
        else if(e.getSource() == button2) {
            System.exit(0);
        }
    } 

很难确定,但我假设您是直接将内容添加到顶级容器中,如
JFrame

JFrame
使用
BorderLayout
作为默认布局管理器,因此使用

add(button1);
add(button2);
基本上说,
按钮1
添加到
中心位置,然后
按钮2
添加到
中心位置<代码>边框布局
只允许单个组件存在于特定位置


首先尝试将按钮添加到另一个面板…

这很难确定,但我假设您正在将内容直接添加到顶级容器,如
JFrame

JFrame
使用
BorderLayout
作为默认布局管理器,因此使用

add(button1);
add(button2);
基本上说,
按钮1
添加到
中心位置,然后
按钮2
添加到
中心位置<代码>边框布局
只允许单个组件存在于特定位置


尝试先将按钮添加到另一个面板…

没关系,解决了,谢谢你的布局,删除帖子,然后xDIIRC只有版主才能删除帖子。OP回答了他们自己的问题在你的案例中添加了太多组件,没有任何原因,如回答中所述,为什么使用这一行
gridPanel.add(combo)当您以后必须这样做时,正如您的代码
getContentPane().add(combo)中所写。为什么
label.setVisible(true),默认情况下组件是可见的,只需将它们添加到容器中并调用顶级容器的visible属性即可查看所有组件。没关系,解决了它,感谢您的布局,删除帖子然后只有xDIIRC版主才能删除帖子。OP已经回答了他们自己的问题。如答案中所述,在您的案例中无原因地添加了太多组件,以及为什么使用此行
gridPanel.add(combo)当您以后必须这样做时,正如您的代码
getContentPane().add(combo)中所写。为什么
label.setVisible(true),组件在默认情况下是可见的,只需将它们添加到容器中并调用顶级容器的visible属性即可查看所有组件。