我在构建两个ContentPanes时遇到问题,其中一个位于起始页上,第二个将在按下按钮后显示。我的带有两个JPanel的测试代码正在工作:

public Main() {
    initComponents();
}
private void initComponents() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setBounds(new Rectangle(1280, 720));
    this.getContentPane().add(p1);

    p1.add(b1);
    b1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            getContentPane().add(p2);
            p2.add(l1);
            p1.setVisible(false);
        }
    });

}
JButton b1 = new JButton("click");
JLabel l1 = new JLabel("test123");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
public static void main(String args[]) {
    new Main().setVisible(true);
}


但是主要的麻烦是使用GroupLayout。我尝试使用setVisible()方法在ContentPane上分解JPanel,但无法正常工作-具有第二个组件的主组件同时显示(相同的内容窗格)。
代码如下:

 public void initComponents() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setBounds(new Rectangle(1280, 720));

    GroupLayout layout = new GroupLayout(getContentPane());
    this.getContentPane().setLayout(layout);
    panelStart = (JPanel) this.getContentPane();

    //design_code

    pack();

    buttonStart.addActionListener(new Gameplay());
}
private class Gameplay implements ActionListener{

    public Gameplay() throws HeadlessException {
        initComponents1();
    }

    public void initComponents1(){
        panelGameplay = (JPanel) getContentPane();

        javax.swing.GroupLayout layout1 = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout1);

    //design_code

    pack();
}

    @Override
    public void actionPerformed(ActionEvent e) {
        getContentPane().add(panelGameplay);
        panelStart.setVisible(false);
    }
}


如果您能帮助我,我将很高兴:D

最佳答案

下面的代码工作正常。您创建PanelsInternalFrames或任何其他容器,然后将其简单地添加到分层窗格中。我认为这种方法会成功。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class Main extends JFrame {
private JPanel contentPane;
private JButton toPanel2Button;
private JLayeredPane layeredPane;
private JPanel panel1;
private JPanel panel2;
private JLabel label2;
private JButton toPanel1Button;

/**
 * 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() {
    initComponents();
    createEvents();
}


private void initComponents() {
    //init frame
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 552, 410);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    //create a layered pane
    layeredPane = new JLayeredPane();
    layeredPane.setBounds(0, 0, 536, 371);
    contentPane.add(layeredPane);


    //create the panel as you like it
    panel1 = new JPanel();
    panel1.setBounds(0, 0, 536, 371);
    panel1.setLayout(null);


    //you can add everything you like
    toPanel2Button = new JButton("Change Panel");
    toPanel2Button.setBounds(142, 269, 216, 58);
    panel1.add(toPanel2Button);


    JLabel label1 = new JLabel("This is panel 1");
    label1.setBounds(142, 68, 216, 51);
    panel1.add(label1);


    //add it to the layered pane
    layeredPane.add(panel1);


    //create another panel
    panel2 = new JPanel();
    panel2.setBounds(0, 0, 536, 371);

    panel2.setLayout(null);


    //add the components you like
    label2 = new JLabel("This is another label");
    label2.setBounds(155, 73, 193, 42);
    panel2.add(label2);

    toPanel1Button = new JButton("GO BACK!");
    toPanel1Button.setBounds(160, 261, 89, 23);
    panel2.add(toPanel1Button);


    //add it to the layered pane

    layeredPane.add(panel2);
}


private void createEvents() {

    toPanel2Button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //remove all layeres
            layeredPane.removeAll();

            //add the one you like
            layeredPane.add(panel2);

            //must revalidate to show it.. see layeredPane.add method javadoc
            layeredPane.repaint();

            //and a revalidate for good measure
            layeredPane.revalidate();
        }
    });


    toPanel1Button.addActionListener(new ActionListener() {

        //same this for another button
        public void actionPerformed(ActionEvent e) {
            layeredPane.removeAll();
            layeredPane.add(panel1);
            layeredPane.repaint();
            layeredPane.revalidate();
        }
    });
}


}

关于java - JPanel与GroupManager,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61599161/

10-13 06:26