【问题标题】:How to show another card using CardLayout?如何使用 CardLayout 显示另一张卡片?
【发布时间】:2017-05-11 13:46:36
【问题描述】:

这个问题可能已经得到了很多回答,但我正在尝试让我的菜单栏与我的 CardLayout 一起使用,这与其他问题中的按钮不同;我已经被困了很长时间了。

我目前正在尝试让三个不同的类一起工作,

  1. CardLayout 类 - 设置框架并将必要的面板添加到框架中。此类也用于显示不同的卡片。
  2. MenuBar 类 - 该类设置了一个非常小的菜单栏,我将其附加到 CardLayout 类中的框架。我只需从这里选择一个菜单项,然后为我的第三堂课添加一个动作监听器。
  3. 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"),然后运行显示我的红色面板的代码,但是卡片根本没有变化?

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

提前谢谢你。

【问题讨论】:

    标签: java menubar cardlayout


    【解决方案1】:

    问题出在您的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:
              }
          }
      }
    
    }
    

    【讨论】:

    • 啊,我现在明白了。我真的很想利用我的课程并将它们分散开来,同时避免这种类型的课程整合。将我的所有类嵌套到一个主类中对我来说似乎有点混乱,但是当我阅读有关嵌套类的文档时,它说如果类与另一个类一起工作是合乎逻辑的原因,这就是正在发生的事情这里。感谢您的启发,我一定会接受这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多