这个问题可能已经得到了很多答案,但我正在尝试让我的菜单栏与我的 CardLayout 一起使用,这与其他问题的按钮不同;我已经在这个问题上坚持了很长时间了。
我目前正在尝试让三个不同的类一起工作,
- CardLayout 类 - 设置框架并向框架添加必要的面板。该类还旨在显示不同的卡片。
- MenuBar 类 - 该类设置了一个非常小的菜单栏,我将其附加到 CardLayout 类中的框架。我只需从此处选择一个菜单项并为我的第三个类添加一个操作监听器。
- MenuActionListener - 此类负责监听当我从菜单栏中选择菜单项时创建的操作事件。当选择某个项目时,就会显示相应的卡片,其中交回给我的CardLayout类来切换卡片。
我的 CardLayout 类:
public class CardLayoutExample {
private CardLayout cardLayout = new CardLayout(20, 20);
private JPanel contentPane = new JPanel(cardLayout);
private MyPanel panel1;
private MyPanel panel2;
private MyPanel panel3;
private void displayGUI()
{
MenuBar menuBar = new MenuBar();
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.add(createPanel(Color.BLACK), "Panel 1");
contentPane.add(createPanel(Color.RED), "Panel 2");
frame.setContentPane(contentPane);
frame.setJMenuBar(menuBar.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JPanel createPanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
public void redCard() {
System.out.println("Selected Red Item");
cardLayout.show(contentPane, "Panel 2");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
}
菜单栏类:
public class MenuBar {
private JMenuBar menuBar;
private MenuActionListener mal;
public MenuBar() {
mal = new MenuActionListener();
System.out.println("menuBar");
//Creates a menubar for a JFrame
menuBar = new JMenuBar();
//Define and add drop down menu to the menubar
JMenu mainMenu = new JMenu("Main Menu");
menuBar.add(mainMenu);
//Define addMenu items
JMenuItem addRedItem = new JMenuItem("Red");
addRedItem.addActionListener(mal);
//Add main menu items/menu
mainMenu.add(addRedItem);
}
public JMenuBar getMenuBar()
{
return menuBar;
}
}
还有我的 MenuActionListener 类:
public class MenuActionListener implements ActionListener {
public void redActionPerformed() {
new CardLayoutExample().redCard();
}
@Override
public void actionPerformed(final ActionEvent e) {
String command = e.getActionCommand();
System.out.println(command);
switch (command) {
case "Red":
redActionPerformed();
break;
default:
}
}
}
当我从菜单栏中选择红色项目时,会触发以下代码行:System.out.println("Selected Red Item")
,然后是显示红色面板的代码跑完了,但是卡根本没有变化?
我一直在尝试让我的菜单栏与更换我的卡片一起工作;如何修复我的代码以便我可以正确显示我想要的卡片?
提前谢谢您。
最佳答案
问题出在您的 MenuActionListener.redActionPerformed
方法中。您正在创建一个全新的 CardLayoutExample
对象并使用它来代替表示实际 UI 的现有对象。解决此问题的最简单方法是使您的 Menu 类嵌套,以便它们获得对外部 CardLayoutExample
类的隐式引用。然后在 redActionPerformed
中,您可以直接调用 redCard()
。否则,您需要将对 CardLayoutExample 对象的引用传递给 MenuActionListener 类。请参阅下面的完整示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardLayoutExample {
private CardLayout cardLayout = new CardLayout(20, 20);
private JPanel contentPane = new JPanel(cardLayout);
private final static String p1 = "Panel 1";
private final static String p2 = "Panel 2";
private void displayGUI()
{
MenuBar menuBar = new MenuBar();
JFrame frame = new JFrame("Card Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.add(createPanel(Color.BLACK), p1);
contentPane.add(createPanel(Color.RED), p2);
frame.setContentPane(contentPane);
frame.setJMenuBar(menuBar.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JPanel createPanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
public void redCard() {
System.out.println("Selected Red Item ");
((CardLayout)contentPane.getLayout()).show(contentPane, p2);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CardLayoutExample().displayGUI();
}
});
}
// Inner Menu Bar class
class MenuBar {
private JMenuBar menuBar;
private MenuActionListener mal;
public MenuBar() {
mal = new MenuActionListener();
System.out.println("menuBar");
//Creates a menubar for a JFrame
menuBar = new JMenuBar();
//Define and add drop down menu to the menubar
JMenu mainMenu = new JMenu("Main Menu");
menuBar.add(mainMenu);
//Define addMenu items
JMenuItem addRedItem = new JMenuItem("Red");
addRedItem.addActionListener(mal);
//Add main menu items/menu
mainMenu.add(addRedItem);
}
public JMenuBar getMenuBar()
{
return menuBar;
}
}
//Inner MenuActionListener class
class MenuActionListener implements ActionListener {
public void redActionPerformed() {
// Call the redCard() method in outer object.
redCard();
}
@Override
public void actionPerformed(final ActionEvent e) {
String command = e.getActionCommand();
System.out.println(command);
switch (command) {
case "Red":
redActionPerformed();
break;
default:
}
}
}
}
关于java - 如何使用CardLayout显示另一张卡片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43917301/