在充满其他面板的卡片布局顶部显示一个 JPanel
Show a JPanel on top of the cardlayout filled with other panels
所以我正在为学生管理创建一个 java swing 程序,但我遇到了一个问题 way.Below 是一张 link 的图片,它提供了如何真正的程序是,在左侧我们有菜单面板,在右侧我们有带有卡片布局的主面板,当单击左侧的特定菜单时,主面板会在卡片布局的顶部添加相应的面板,就像在图片中,如果我单击红色按钮,右侧显示红色面板,我想做的是当我单击设置按钮以在活动面板顶部显示面板时,我尝试设置可见但逻辑上只适用于那个面板,我能想到的唯一方法是在每个面板上添加不同的设置面板,并在每次单击按钮设置时将它们的可见性设置为 true 但这不是一个好方法,因为如果我们有 10 个菜单,我们将创建 10 个设置面板,我已经在互联网上检查了任何 Z 组件订单,没有找到任何有用的东西,你可以在 Microso 看到ft 邮件应用程序我想做什么,他们的设置如何在左侧打开,这就是我的意图。
您需要使用glass pane。这是我给你的例子。
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());
}
}
所以我正在为学生管理创建一个 java swing 程序,但我遇到了一个问题 way.Below 是一张 link 的图片,它提供了如何真正的程序是,在左侧我们有菜单面板,在右侧我们有带有卡片布局的主面板,当单击左侧的特定菜单时,主面板会在卡片布局的顶部添加相应的面板,就像在图片中,如果我单击红色按钮,右侧显示红色面板,我想做的是当我单击设置按钮以在活动面板顶部显示面板时,我尝试设置可见但逻辑上只适用于那个面板,我能想到的唯一方法是在每个面板上添加不同的设置面板,并在每次单击按钮设置时将它们的可见性设置为 true 但这不是一个好方法,因为如果我们有 10 个菜单,我们将创建 10 个设置面板,我已经在互联网上检查了任何 Z 组件订单,没有找到任何有用的东西,你可以在 Microso 看到ft 邮件应用程序我想做什么,他们的设置如何在左侧打开,这就是我的意图。
您需要使用glass pane。这是我给你的例子。
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());
}
}