简体   繁体   English

如何用Java中的另一个JPanel替换两个JPanel之一?

[英]How can I replace one of two JPanels with another JPanel in Java?

I designed an interface for the welcome screen with one JFrame included two JPanels (JPanel1 on right and JPanel2 on left). 我设计了一个欢迎屏幕界面,其中一个JFrame包含两个JPanels(右侧为JPanel1,左侧为JPanel2)。 The buttons on the left is to switch the Panels in JPanel1. 左侧的按钮用于切换JPanel1中的面板。 I want to press on a button to replace JPanel1 content with another JPanel but I don`t know how. 我想按一个按钮,用另一个JPanel替换JPanel1内容,但我不知道如何。 Please help. 请帮忙。

Here is a very simple example of something that should approximate your description. 这是一个非常简单的示例,它应近似于您的描述。 On the left, we have a hug button to toggle the content of the right panel. 在左侧,我们有一个拥抱按钮可以切换右侧面板的内容。 On the right, you have a panel with a given border and a label. 在右边,您有一个带有给定边框和标签的面板。 When you press the button, the content on the right is swapped with the other panel. 当您按下按钮时,右侧的内容将与另一个面板交换。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestCardLayout2 {

    protected void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel leftPanel = new JPanel(new BorderLayout());
        JLabel label = new JLabel("Left panel");
        leftPanel.add(label, BorderLayout.NORTH);
        JButton button = new JButton("Toggle right panel");
        leftPanel.add(button);
        frame.add(leftPanel, BorderLayout.WEST);

        final CardLayout cardLayout = new CardLayout();
        final JPanel rightPanel = new JPanel(cardLayout);
        rightPanel.setPreferredSize(new Dimension(200, 500));

        JPanel rightPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        rightPanel1.setBorder(BorderFactory.createLineBorder(Color.RED));
        JPanel rightPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        rightPanel2.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        JLabel label1 = new JLabel("Right panel 1 with a red border");
        JLabel label2 = new JLabel("Right panel 2 with a blue borer");
        rightPanel1.add(label1);
        rightPanel2.add(label2);

        rightPanel.add(rightPanel1, "panel1");
        rightPanel.add(rightPanel2, "panel2");
        frame.add(rightPanel, BorderLayout.EAST);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.next(rightPanel);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestCardLayout2().initUI();
            }
        });
    }

}

An alternative to CardLayout would be JRootPane and its JRootPane.setContentPane() method. CardLayout的替代方法是JRootPane及其JRootPane.setContentPane()方法。 Here's an example: 这是一个例子:

final JPanel panel1 = ...;
final JPanel panel2 = ...;
boolean showingPanel1 = true;
final JRootPane rootPane = new JRootPane();
rootPane.setContentPane(panel1);
JButton switchButton = new JButton("Switch");
switchButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        if (showingPanel1) {
            rootPane.setContentPane(panel2);
        } else {
            rootPane.setContentPane(panel1);
        }
        showingPanel = !showingPanel;
    }
});

Add the rootPane and switchButton components to your window, and then clicking switchButton will switch out the panels. rootPaneswitchButton组件添加到您的窗口中,然后单击switchButton将切换面板。

Here's a tutorial . 这是一个教程 You should mostly be concerned with JRootPane.setContentPane , the other stuff in the tutorial isn't relevant. 您应该主要关注JRootPane.setContentPane ,本教程中的其他内容JRootPane.setContentPane

The best answer I found is that I will create one JFrame only and gonna make one big JPanel include two JPanels (JPanelLeft include the buttons and JPanelRight include what the button do) then I will copy the main JPanel for each JButton. 我发现的最佳答案是,我将只创建一个JFrame,然后使一个大的JPanel包含两个JPanels(JPanelLeft包含按钮,而JPanelRight包含按钮的作用),然后为每个JButton复制主JPanel。 When I press on any button I will do (JFrame.getContentPane.removeAll) to remove the old JPanel then (JFrame.getContentPane.Add(NewJPanel). 当我按下任何按钮时,我将(JFrame.getContentPane.removeAll)删除旧的JPanel,然后再删除(JFrame.getContentPane.Add(NewJPanel)。

This works for me and keep my design as I Want. 这对我有用,并保持我的设计不变。 Thanks for every body. 谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM