MyException - 我的异常网
当前位置:我的异常网» J2SE » 窗体如何不显示添加的组件,大家帮忙看看

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

www.MyException.Cn  网友分享于:2013-12-23  浏览:25次
窗体怎么不显示添加的组件,大家帮忙看看!
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);
    }

}

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有