Java 如何在GUI中单击按钮更改面板的内容?

Java 如何在GUI中单击按钮更改面板的内容?,java,swing,netbeans,jpanel,jbutton,Java,Swing,Netbeans,Jpanel,Jbutton,呸。我在这个问题上纠缠了这么久。我正在做一个GUI程序,模拟一个突击测验。但我不知道当我想让我的程序变成这样的时候,该放什么代码 当我点击开始按钮时,面板应该是这样的。。。 到目前为止,这是我的启动菜单 public static void main (String []args){ JFrame f = new JFrame("Pop Quiz"); f.setSize(400,300); f.setDefaultCloseOperation(JFrame.EXIT_

呸。我在这个问题上纠缠了这么久。我正在做一个GUI程序,模拟一个突击测验。但我不知道当我想让我的程序变成这样的时候,该放什么代码

当我点击开始按钮时,面板应该是这样的。。。

到目前为止,这是我的启动菜单

public static void main (String []args){
    JFrame f = new JFrame("Pop Quiz");
    f.setSize(400,300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);
    f.setResizable(false);

    JPanel p1 = new JPanel();
    p1.setSize(400,100);
    p1.setLocation(0,0);
    p1.setLayout(new GridLayout(3,1));
    f.add(p1);

    JLabel l1 = new JLabel("Welcome to POP Quiz!");
    p1.add(l1);

    JLabel l2 = new JLabel("Enter your name:");
    p1.add(l2);

    final JTextField name = new JTextField ();
    p1.add(name);

    JPanel p2 = new JPanel();
    p2.setSize(400,50);
    p2.setLocation(0,225);
    f.add(p2);

    JButton start = new JButton ("Start");
    p2.add(start);

    start.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             String player = name.getText();
             //what should be added here to change the contents of the panel?
         }
    });

    f.show();
}
至于问题

public static void main(String[] args){
    JFrame f = new JFrame("Pop Quiz");
    f.setSize(400,300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);
    f.setResizable(false);

    JPanel p1 = new JPanel();
    p1.setSize(400,100);
    p1.setBackground(Color.PINK);
    f.add(p1);

    JLabel question = new JLabel();
    question.setText("In computers, what is the smallest and basic unit of information storage?");
    p1.add(question);

    JPanel p2 = new JPanel();
    p2.setSize(400,175);
    p2.setLocation(0,100);
    p2.setLayout(new GridLayout(2,4));
    f.add(p2);

    JButton a = new JButton("a. Bit");
    p2.add(a);

    JButton b = new JButton("b. Byte");
    p2.add(b);

    JButton c = new JButton("c. Data");
    p2.add(c);        

    JButton d = new JButton("d. Newton");
    p2.add(d);        

    f.show();
}
如果有人能帮忙,我会非常感激。提前谢谢!祝你今天愉快!:)

尝试f.getContentPane().add(面板)

使用。如图所示

提示 布局 使用布局!这一点我怎么强调都不过分。布局可能看起来很复杂,但它们是在GUI中布局复杂组件组的唯一可行解决方案,用于不同平台(PLAF、屏幕分辨率…)

控制 考虑到按钮所用字符串的性质,它们似乎最好是
JComboBox
JList
ButtonGroup
中的按钮

不推荐的方法 此方法已弃用,您的编译器应警告您该方法已弃用或有其他警告被忽略。查看这些警告,并修复它们。方法被弃用是有原因的。

试试这个。。
Try this.. 


   public class test {
    public static void main(String[] args) {
        final JFrame f = new JFrame("Pop Quiz");
        f.setSize(400, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);
        f.setResizable(false);

        final JPanel p1 = new JPanel();
        p1.setSize(400, 100);
        p1.setLocation(0, 0);
        p1.setLayout(new GridLayout(3, 1));
        f.add(p1);

        JLabel l1 = new JLabel("Welcome to POP Quiz!");
        p1.add(l1);

        JLabel l2 = new JLabel("Enter your name:");
        p1.add(l2);

        final JTextField name = new JTextField();
        p1.add(name);

        final JPanel p2 = new JPanel();
        p2.setSize(400, 50);
        p2.setLocation(0, 225);
        f.add(p2);

        JButton start = new JButton("Start");
        p2.add(start);

        start.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String player = name.getText();
               p1.setVisible(false);
               p2.setVisible(false);
                JPanel testPanel = new JPanel();
                testPanel.setSize(400, 100);
                testPanel.setBackground(Color.PINK);
                f.add(testPanel);

                JLabel question = new JLabel();
                question.setText("<html>In computers, what is the smallest and basic unit<br/> of information storage?</html>");
                testPanel.add(question);

                JPanel p2 = new JPanel();
                p2.setSize(400, 175);
                p2.setLocation(0, 100);
                p2.setLayout(new GridLayout(2, 4));
                f.add(p2);

                JButton a = new JButton("a. Bit");
                p2.add(a);

                JButton b = new JButton("b. Byte");
                p2.add(b);

                JButton c = new JButton("c. Data");
                p2.add(c);

                JButton d = new JButton("d. Newton");
                p2.add(d);

                f.show();
            }
        });

        f.show();
    }
}
公开课考试{ 公共静态void main(字符串[]args){ 最终JFrame f=新JFrame(“突击测验”); f、 设置大小(400300); f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f、 setLayout(空); f、 可设置大小(假); 最终JPanel p1=新JPanel(); p1.设置尺寸(400100); p1.设定位置(0,0); p1.设置布局(新网格布局(3,1)); f、 添加(p1); JLabel l1=新的JLabel(“欢迎参加突击测验!”); p1.添加(l1); JLabel l2=新的JLabel(“输入您的姓名:”); p1.添加(l2); 最终JTextField名称=新JTextField(); p1.添加(名称); 最终JPanel p2=新JPanel(); p2.设置尺寸(400,50); p2.设定位置(0225); f、 添加(p2); JButton start=新JButton(“start”); p2.添加(开始); start.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件e){ String player=name.getText(); p1.setVisible(假); p2.设置可见(假); JPanel testPanel=newjpanel(); 测试面板。设置尺寸(400100); 测试面板。背景(颜色。粉红色); f、 添加(测试面板); JLabel question=新JLabel(); question.setText(“在计算机中,信息存储的最小基本单位是什么?”); 添加(问题); JPanel p2=新的JPanel(); p2.设置大小(400175); p2.设置位置(0,100); p2.设置布局(新网格布局(2,4)); f、 添加(p2); JButton a=新JButton(“a.位”); p2.加入(a); JButton b=新JButton(“b.字节”); p2.加入(b); JButton c=新JButton(“c.数据”); p2.加入(c); JButton d=新JButton(“d.牛顿”); p2.加入(d); f、 show(); } }); f、 show(); } }
实现此目标的步骤:
1.创建一个主面板并添加到
JFrame

2.将内容添加到主面板中的welcomePanel
JPanel

3.一旦用户按下Welcome面板中的“开始”按钮
。在主容器上调用
removeAll()

4.在main的contentPanel中添加新内容,并调用
revalidate()
,更新main


注意:对于此

,您不需要单独的jframe实例。要更快获得更好的帮助,请发布一个。使用f.setVisible(true)而不是f.show();
JButton a = new JButton("a. Bit");
p2.add(a);

JButton b = new JButton("b. Byte");
// ..
f.show();
Try this.. 


   public class test {
    public static void main(String[] args) {
        final JFrame f = new JFrame("Pop Quiz");
        f.setSize(400, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);
        f.setResizable(false);

        final JPanel p1 = new JPanel();
        p1.setSize(400, 100);
        p1.setLocation(0, 0);
        p1.setLayout(new GridLayout(3, 1));
        f.add(p1);

        JLabel l1 = new JLabel("Welcome to POP Quiz!");
        p1.add(l1);

        JLabel l2 = new JLabel("Enter your name:");
        p1.add(l2);

        final JTextField name = new JTextField();
        p1.add(name);

        final JPanel p2 = new JPanel();
        p2.setSize(400, 50);
        p2.setLocation(0, 225);
        f.add(p2);

        JButton start = new JButton("Start");
        p2.add(start);

        start.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String player = name.getText();
               p1.setVisible(false);
               p2.setVisible(false);
                JPanel testPanel = new JPanel();
                testPanel.setSize(400, 100);
                testPanel.setBackground(Color.PINK);
                f.add(testPanel);

                JLabel question = new JLabel();
                question.setText("<html>In computers, what is the smallest and basic unit<br/> of information storage?</html>");
                testPanel.add(question);

                JPanel p2 = new JPanel();
                p2.setSize(400, 175);
                p2.setLocation(0, 100);
                p2.setLayout(new GridLayout(2, 4));
                f.add(p2);

                JButton a = new JButton("a. Bit");
                p2.add(a);

                JButton b = new JButton("b. Byte");
                p2.add(b);

                JButton c = new JButton("c. Data");
                p2.add(c);

                JButton d = new JButton("d. Newton");
                p2.add(d);

                f.show();
            }
        });

        f.show();
    }
}