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

时间:2017-05-11 13:46:37

标签: java menubar cardlayout

这个问题可能已经解决了,但我试图让我的菜单栏与我的CardLayout一起工作,不像其他问题的按钮;我已经坚持了很长时间。

我目前正在努力让三个单独的课程一起工作,

  1. CardLayout类 - 设置框架并将必要的面板添加到框架中。这堂课也是为了展示不同的牌。
  2. MenuBar类 - 此类设置一个非常小的菜单栏,我将其附加到我的CardLayout类中的框架。我只是从这里选择一个菜单项,并为我的第三个类添加一个动作监听器。
  3. MenuActionListener - 此类负责监听从菜单栏中选择菜单项时创建的操作事件。当选择某个项目时,将显示相应的卡片,其中交回我的CardLayout课程以切换卡片。
  4. 我的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();
                }
            });
        }
    
    }
    

    MenuBar类:

    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"),然后显示我的红色面板的代码,但是,卡片根本没有变化?

    我一直在尝试让我的菜单栏更换我的卡片;如何修复我的代码,以便我能正确显示我想要的卡?

    提前谢谢。

1 个答案:

答案 0 :(得分:1)

问题出在您的MenuActionListener.redActionPerformed方法中。您正在创建一个全新的CardLayoutExample对象并使用它而不是代表实际UI的现有对象。解决此问题的最简单方法是使您的Menu类嵌套,以便它们获得对外部CardLayoutExample类的隐式引用。然后在redActionPerformed中,您可以直接致电redCard()。否则,您需要将对Ca​​rdLayoutExample对象的引用传递给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:
          }
      }
  }

}