首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

2011-12-22 
窗体怎么不显示添加的组件,大家帮忙看看!Java codeimport java.awt.*import java.awt.event.*import jav

窗体怎么不显示添加的组件,大家帮忙看看!

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

热点排行