9

我在两个不同的面板中添加了两个按钮,如果单击第一个按钮,则需要进入下一个面板,其中包含第二个按钮。但是当我单击第一个按钮时,该按钮没有被替换。

/*Java GUI*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class TestFrame extends JFrame{

    private JPanel panel1, panel2;
    private JButton but,but2; 

    public TestFrame()
    {
        createPanel();
        addPanel();
    }

    private void createPanel()
    {
        panel1 = new JPanel();
        but = new JButton("TestButton");
        but.addActionListener(new addButtonListener());

        panel2 = new JPanel();
        but2 = new JButton("TestButton2");

    }

    private void addPanel()
    {
        panel1.add(but);
        panel2.add(but2);

        add(panel1);

    }

    class addButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent ae) 
        {
            getContentPane().removeAll();
            add(panel2);

            repaint();
        }
    }


    public static void main(String args[])
    {
        JFrame frame = new TestFrame();
        frame.setTitle("Test Software");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);

        frame.setVisible(true);
    }



}
4

2 回答 2

5

您需要进行验证,然后重新绘制。

validate();
repaint();
于 2012-11-26T09:59:27.477 回答
4

从 contentPane 中删除所有内容后,尝试将面板添加到 ContentPane。第二件事是重新粉刷。如果您不更新面板内容,它将在调整大小后绘制。这是示例解决方案:



    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.*;

    public class Frame extends JFrame{
        private JPanel panel1, panel2;
        private JButton but,but2; 
        public Frame()
        {
           createPanel();
           addPanel();
        }
        private void createPanel()
        {
            panel1 = new JPanel();
            but = new JButton("TestButton");
            but.addActionListener(new addButtonListener());
            but.setBounds(50, 90, 190, 30);//There are example values but remember about setting size
            panel2 = new JPanel();
            but2 = new JButton("TestButton2");
            but2.setBounds(50, 50, 90, 30);//There are example values but remember about setting size
        }
        private void addPanel()
        {
            panel1.add(but);
            panel2.add(but2);
            add(panel1);
        }

        class addButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent ae) 
            {
                getContentPane().removeAll();
                getContentPane().add(panel2);//Adding to content pane, not to Frame
                repaint();
                printAll(getGraphics());//Extort print all content
            }
        }

        public static void main(String args[])
        {
            Frame frame = new Frame();
            frame.setTitle("Test Software");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.setVisible(true);
        }

    }

Oracle 文档解释了添加到 contentPane 或直接添加到 Frame 的差异。
http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

于 2012-11-26T10:28:02.690 回答