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