当前位置: 代码迷 >> J2SE >> 窗体如何不显示添加的组件,大家帮忙看看
  详细解决方案

窗体如何不显示添加的组件,大家帮忙看看

热度:110   发布时间:2016-04-24 12:31:34.0
窗体怎么不显示添加的组件,大家帮忙看看!
Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TestButton {    public static void main(String[] args) {                  ButtonFrame frame = new ButtonFrame();          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          frame.setSize(150,100);          frame.setResizable(false);          frame.setVisible(true);    }}class ButtonFrame extends JFrame{        public ButtonFrame(){                setTitle("test button");        ButtonPanel panel = new ButtonPanel();        add(panel);        //pack();    }    }class ButtonPanel extends JPanel{        public ButtonPanel(){                JPanel panel = new JPanel();        panel.setLayout(new GridLayout(2,1));                panel.add(new JButton("1"));        panel.add(new JButton("2"));[color=#FF0000]//添加的button怎么不显示?[/color]    }        }


------解决方案--------------------

或者
Java code
;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TestButton {    public static void main(String[] args) {    ButtonFrame frame = new ButtonFrame();    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.setSize(150, 100);    frame.setResizable(false);    frame.setVisible(true);    }}class ButtonFrame extends JFrame {    public ButtonFrame() {    setTitle("test button");    ButtonPanel panel = new ButtonPanel();    add(panel);    }}class ButtonPanel extends JPanel {    public ButtonPanel() {    JPanel panel = new JPanel();    panel.setLayout(new GridLayout(2, 1));    panel.add(new JButton("1"));    panel.add(new JButton("2"));//添加的button怎么不显示?[/color]    add(panel);    }}
  相关解决方案