如何使用 CardLayout 显示另一张卡片?

How to show another card using CardLayout?

这个问题可能已经回答了很多,但我正在尝试让我的菜单栏与我的 CardLayout 一起工作,这与其他问题的按钮不同;我已经坚持了很长时间。

我目前正在尝试让三个独立的 classes 一起工作,

  1. CardLayout class - 设置框架并将必要的面板添加到框架中。这个class也是为了展示不同的牌。
  2. MenuBar class - 这个 class 设置了一个非常小的菜单栏,我将其附加到 CardLayout class 中的框架。我只是从这里选择一个菜单项并为我的第三个 class.
  3. 添加一个动作侦听器
  4. MenuActionListener - 此 class 负责侦听当我 select 我的菜单栏中的菜单项时创建的操作事件。当某个item被select编辑时,会显示对应的卡片,其中是交还给我的CardLayoutclass来切换卡片。

我的 CardLayout class:

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();
            }
        });
    }

}

菜单栏class:

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 class:

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:
        }
    }
}

当我从菜单栏中 select 红色项时,会触发以下代码行:System.out.println("Selected Red Item"),然后显示我的红色面板的代码是 运行 通过,然而,卡片根本没有改变?

我一直在努力尝试让我的菜单栏与更换我的卡片一起工作;我怎样才能修复我的代码,以便我可以正确显示我想要的卡片?

提前谢谢你。

问题出在您的 MenuActionListener.redActionPerformed 方法中。您正在创建一个全新的 CardLayoutExample 对象并使用它来代替代表实际 UI 的现有对象。解决此问题的最简单方法是使您的菜单 class 嵌套,以便它们获得对外部 CardLayoutExample class 的隐式引用。然后在 redActionPerformed 中你可以直接调用 redCard() 。否则,您将需要将对 CardLayoutExample 对象的引用传递给 MenuActionListener class。请参阅下面的完整示例:

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:
          }
      }
  }

}