简体   繁体   English

在填充了其他面板的卡片布局顶部显示一个 JPanel

[英]Show a JPanel on top of the cardlayout filled with other panels

So I'm creating a java swing program for students management but I got into a problem along the way.Below is a link with a picture that gives a clue how the real program is, in the left side we have the menu panel in the right side we have main panel with the card layout, when a specific menu in the left side is clicked the main panel adds that corresponding panel on top of the card layout, like in the pic if I click red button the right sides shows red panel, what I wanna do is when I click the settings button to show a panel on top of the active panel, I have tried with set visible but logically that works just with that panel, only way I can think of this working is adding different setting panels at each of the panels and set their visibility true each time the button settings is clicked but that's not a good way because if we had 10 menus we would create 10 setting panels, i have checked around internet for any Z component order nothing helpful found, you can see at Microsoft mail所以我正在为学生管理创建一个 java swing 程序,但我在此过程中遇到了一个问题。下面是一个带有图片的链接,它提供了真实程序的线索,在左侧,我们有菜单面板右侧我们有带有卡片布局的主面板,当单击左侧的特定菜单时,主面板会在卡片布局的顶部添加相应的面板,就像在图片中单击红色按钮一样,右侧显示红色面板,我想要做的是当我单击设置按钮以在活动面板顶部显示一个面板时,我尝试设置可见但逻辑上只适用于该面板,我能想到的唯一方法是添加不同的设置每个面板上的面板,并在每次单击按钮设置时将它们的可见性设置为 true 但这不是一个好方法,因为如果我们有 10 个菜单,我们将创建 10 个设置面板,我已经在互联网上检查了任何 Z 组件顺序,但没有发现任何有用的东西,你可以在微软邮箱看到 app what I want to do, how their settings open in left side, that's my intention.应用程序我想做什么,他们的设置如何在左侧打开,这就是我的意图。 例子

You need to use the glass pane .您需要使用玻璃面板 Here is my example for you.这是我给你的例子。

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

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class RightSidePanel implements Runnable {

    @Override
    public void run() {
        JFrame frm = new JFrame("Right side panel");
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // next two lines are not required
        JPanel contentPanel = new JPanel(new BorderLayout());
        frm.setContentPane(contentPanel);
        JPanel mainPanel = new JPanel(new CardLayout());
        mainPanel.add(new JLabel("It's the first card panel"), "first");
        mainPanel.add(new JLabel("It's the second card panel"), "second");
        // add some components to provide some width and height for the panel.
        mainPanel.add(Box.createHorizontalStrut(600));
        mainPanel.add(Box.createVerticalStrut(300));
        mainPanel.setBackground(Color.CYAN);
        JPanel settingsPanel = new JPanel(new GridLayout(1, 1));
        settingsPanel.add(new JLabel("Here is the settings panel!"));
        settingsPanel.setPreferredSize(
                new Dimension(settingsPanel.getPreferredSize().width, 300));
        ((JComponent) frm.getGlassPane()).setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        ((JComponent) frm.getGlassPane()).add(settingsPanel, BorderLayout.EAST);
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        JButton settingsButton = new JButton("Show settings");
        settingsButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                frm.getGlassPane().setVisible(!frm.getGlassPane().isVisible());
                if (frm.getGlassPane().isVisible()) {
                    settingsButton.setText("Hide settings");
                } else {
                    settingsButton.setText("Show settings");
                }
            }
        });
        JButton switchButton = new JButton("Show second");
        switchButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) mainPanel.getLayout();
                if (mainPanel.getComponent(0).isVisible()) {
                    cl.show(mainPanel, "second");
                    switchButton.setText("Show first");
                } else {
                    cl.show(mainPanel, "first");
                    switchButton.setText("Show second");
                }
            }
        });
        buttonPanel.add(switchButton);
        buttonPanel.add(settingsButton);
        frm.add(mainPanel, BorderLayout.CENTER);
        frm.add(buttonPanel, BorderLayout.SOUTH);
        frm.pack();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new RightSidePanel());
    }
}

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

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