簡體   English   中英

如何使用CardLayout顯示另一張卡?

[英]How to show another card using CardLayout?

這個問題可能已經得到了解答,但是我試圖使菜單欄與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();
            }
        });
    }

}

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") ,然后將顯示我的紅色面板的代碼執行完畢,但是,該卡不會徹底改變嗎?

我一直在努力嘗試讓我的菜單欄可以更換卡片。 如何修正我的代碼,以便可以正確顯示我想要的卡?

先感謝您。

問題出在您的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:
          }
      }
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM