3

这是我第一次看到 JFrames 和 JPannel,我有点卡住了。

我想要做的是,我希望有一个起始屏幕,然后根据用户按钮选择它切换到另一个屏幕。一开始我只有 2 个屏幕,但是一旦我继续前进,就会有多个屏幕。我看过 CardLayout 虽然这很好,但这不是我想要的方式,我希望能够首先做到这一点。这就是我所拥有的。

主.java

import java.awt.BorderLayout;
public class Main extends JFrame {

private JPanel contentPane;
protected boolean someCondition = false;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main frame = new Main();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    if( someCondition == false ){
        showTest();
        someCondition = test.needToReg();
    }else{
        showTest2();
    }
}

private void showTest(){
    contentPane.removeAll();
    contentPane.add(new test());
    setContentPane(contentPane);
    revalidate();
    repaint();
}

private void showTest2(){
    contentPane.removeAll();
    contentPane.add(new test2());
    setContentPane(contentPane);
    revalidate();
    repaint();
}

}

测试.java

import javax.swing.JPanel;


public class test extends JPanel {
    private JTextField textField;
    protected static boolean toReg = false;

    /**
     * Create the panel.
     */
    public test() {
        setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Mouse Clicked");
                System.out.println("Before " + toReg);
                toReg = true;
                System.out.println("After " + toReg);
            }
        });
        btnNewButton.setBounds(188, 166, 89, 23);
        add(btnNewButton);

        textField = new JTextField();
        textField.setBounds(150, 135, 86, 20);
        add(textField);
        textField.setColumns(10);

        JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
        rdbtnNewRadioButton.setBounds(6, 166, 109, 23);
        add(rdbtnNewRadioButton);
    }

    public static boolean needToReg(){
        return toReg;
    }
}

test2.java

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;


public class test2 extends JPanel {

    /**
     * Create the panel.
     */
    public test2() {
        setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(56, 59, 89, 23);
        add(btnNewButton);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBounds(122, 165, 46, 14);
        add(lblNewLabel);

    }
}

使用我包含的输出运行程序,我得到了这个。

Mouse Clicked
Before false
After true
Mouse Clicked
Before true
After true
Mouse Clicked
Before true
After true
Mouse Clicked
Before true
After true
Mouse Clicked
Before true
After true

我希望很清楚我想要做什么,我希望你能帮上忙。谢谢

4

1 回答 1

1

试试这个

单击主框架中的screenSwapper按钮时,会在主框架中添加一个新面板,该面板可以包含多个组件我只添加了一个按钮

在第二次单击时,此面板被删除,第二个面板被添加到主框架中,前一个被删除。

当您连续单击按钮时进行交换

如果您想在MyPanel1和 MyPanel2的情况下保留一次创建的面板,您可以使用两个单例

您可以在每个面板上添加更多组件并进行测试。

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame {

    public boolean switcher;
    public JPanel currentPanel;
    public JPanel panel1;
    public JPanel panel2;

    public Test() {

        this.switcher = false;
        this.currentPanel = null;
        this.setSize(200, 200);

        panel1 = new JPanel();

        JButton screenSwapper = new JButton("Screen Swapper");

        panel1.add(screenSwapper);

        panel2 = new JPanel();

        this.getContentPane().setLayout(new GridLayout(2, 2));
        this.getContentPane().add(panel1);
        this.getContentPane().add(panel2);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        screenSwapper.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                if (switcher == false) {
                    currentPanel = new MyPanel1();
                    switcher = true;

                    if (panel2.getComponentCount() != 0) {
                        panel2.removeAll();
                    }

                } else {

                    switcher = false;
                    currentPanel = new MyPanel2();

                    if (panel2.getComponentCount() != 0) {
                        panel2.removeAll();
                    }

                }

                panel2.add(currentPanel);
                panel2.repaint();
                panel2.revalidate();
            }

        });

    }

    public static void main(String[] args) {
        Test t = new Test();
    }

}

这是第一个面板

import java.awt.BorderLayout;
import java.awt.Button;

import javax.swing.JPanel;


public class MyPanel1 extends  JPanel{



     public MyPanel1() {
        // TODO Auto-generated constructor stub

        this.setLayout(new BorderLayout());

        this.add(new Button("Button1"));

    }




}

这是第二个面板

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JPanel;


public class MyPanel2 extends JPanel {



    public MyPanel2() {
        this.setLayout(new BorderLayout());

        this.add(new JButton("button2"));
    }
}
于 2013-05-13T15:15:35.690 回答