简体   繁体   English

无法使CardLayout /按钮ActionListeners正常工作

[英]Can't get CardLayout/button ActionListeners to Work

I know it's something to do with how I've set it up and the actionlistener not being correctly set to the frame or something but I just can't get my hear around it. 我知道这与我的设置方式以及动作侦听器未正确设置到框架有关,但我只是听不清它。 If someone could point me in the right direction I'd be much obliged. 如果有人能指出我正确的方向,我将很有义务。 Sorry for noob question. 对不起,菜鸟问题。

Here's what I have: 这是我所拥有的:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Main implements ActionListener {

    JPanel cardHolder;
    public static final String HOME_CARD = "Home";
    public static final String BLUE_PANEL = "Blue Panel";
    public static final String RED_PANEL = "Red Panel";
    public static final String ORANGE_PANEL = "Orange Panel";

    public static JButton home = new JButton("Home");
    public static JButton bluePanel = new JButton("Blue Card");
    public static JButton redPanel = new JButton("Red Panel");
    public static JButton orangePanel = new JButton("Orange Panel");

    public static JPanel createCardHolderPanel() {
        JPanel cardHolder = new JPanel(new CardLayout());
        cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));

        cardHolder.add(createHomeCard(), HOME_CARD);
        cardHolder.add(createBluePanel(), BLUE_PANEL);
        cardHolder.add(createRedPanel(), RED_PANEL);
        cardHolder.add(createOrangePanel(), ORANGE_PANEL);

        return cardHolder;
    }

    private static JPanel createOrangePanel() {
        JPanel orangePanel = new JPanel();

        orangePanel.setBackground(Color.orange);
        return orangePanel;
    }

    private static Component createRedPanel() {
        JPanel redPanel = new JPanel();

        redPanel.setBackground(Color.red);
        return redPanel;
    }

    private static Component createBluePanel() {
        JPanel bluePanel = new JPanel();

        bluePanel.setBackground(Color.blue);
        return bluePanel;
    }

    private static Component createHomeCard() {
        JPanel homePanel = new JPanel();

        homePanel.setBackground(Color.GRAY);
        return homePanel;
    }

    public static JPanel createButtonPanel() {
        JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
        buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));

        buttonPanel.add(home);
        buttonPanel.add(bluePanel);
        buttonPanel.add(redPanel);
        buttonPanel.add(orangePanel);

        return buttonPanel;
    }

    public static JPanel createContentPane() {
        JPanel contentPane = new JPanel(new BorderLayout());

        contentPane.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
        contentPane.setBackground(Color.WHITE);
        contentPane.setPreferredSize(new Dimension(499, 288));

        contentPane.add(createButtonPanel(), BorderLayout.WEST);
        contentPane.add(createCardHolderPanel(),BorderLayout.CENTER);

        return contentPane;
    }

    public static JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        JMenu file = new JMenu("File");
        JMenu users = new JMenu("Users");
        JMenu options = new JMenu("Options");
        JMenu help = new JMenu("Help");

        menuBar.add(file);
        menuBar.add(users);
        menuBar.add(options);
        menuBar.add(help);

        return menuBar;
    }

    public static void createAndShowGUI() {
        JFrame frame = new JFrame("Simple CardLayout Program");
        frame.setContentPane(createContentPane());
        frame.setJMenuBar(createMenuBar());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == home) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            cardLayout.show(cardHolder, HOME_CARD);
        }

        if (e.getSource() == bluePanel) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            cardLayout.show(cardHolder, BLUE_PANEL);
        }

        if (e.getSource() == redPanel) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            cardLayout.show(cardHolder, RED_PANEL);
        }

        if (e.getSource() == orangePanel) {
            CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
            cardLayout.show(cardHolder, ORANGE_PANEL);
        }
    }
}

Others have suggested listening to the buttons; 其他人则建议听按钮。 in addition: 此外:

  • Prefer the lowest accessibility consistent with use, eg private rather than public . 优先考虑与使用情况保持一致的最低可访问性,例如private而不是public
  • Don't make everything static . 不要使一切static
  • Use static for immutable constants used throughout the class. 对于整个类中使用的不可变常量,请使用static
  • Use class variables rather than static members for content. 使用类变量而不是静态成员作为内容。
  • Don't repeat your self, eg initialize cardLayout just once in your actionPerformed)() . 不要重复自己,例如,在actionPerformed)()只初始化一次cardLayout
  • Use parameters rather than separate methods for each color, eg 对每种颜色使用参数而不是单独的方法,例如

     private JPanel createColorPanel(Color color) {...} 

Revised code: 修改后的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Main implements ActionListener {

    private static final String HOME_CARD = "Home";
    private static final String BLUE_PANEL = "Blue Panel";
    private static final String RED_PANEL = "Red Panel";
    private static final String ORANGE_PANEL = "Orange Panel";
    private JPanel cardHolder;
    private JButton homeButton = new JButton("Home");
    private JButton blueButton = new JButton("Blue Card");
    private JButton redButton = new JButton("Red Panel");
    private JButton orangeButton = new JButton("Orange Panel");

    public JPanel createCardHolderPanel() {
        cardHolder = new JPanel(new CardLayout());
        cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
        cardHolder.add(createColorPanel(Color.gray), HOME_CARD);
        cardHolder.add(createColorPanel(Color.blue), BLUE_PANEL);
        cardHolder.add(createColorPanel(Color.red), RED_PANEL);
        cardHolder.add(createColorPanel(Color.orange), ORANGE_PANEL);

        return cardHolder;
    }

    private JPanel createColorPanel(Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        return panel;
    }

    public JPanel createButtonPanel() {
        JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
        buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
        buttonPanel.add(homeButton);
        buttonPanel.add(blueButton);
        buttonPanel.add(redButton);
        buttonPanel.add(orangeButton);
        homeButton.addActionListener(this);
        blueButton.addActionListener(this);
        redButton.addActionListener(this);
        orangeButton.addActionListener(this);
        return buttonPanel;
    }

    public JPanel createContentPane() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
        panel.setBackground(Color.WHITE);
        panel.setPreferredSize(new Dimension(499, 288));
        panel.add(createButtonPanel(), BorderLayout.WEST);
        panel.add(createCardHolderPanel(), BorderLayout.CENTER);
        return panel;
    }

    public JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenu users = new JMenu("Users");
        JMenu options = new JMenu("Options");
        JMenu help = new JMenu("Help");
        menuBar.add(file);
        menuBar.add(users);
        menuBar.add(options);
        menuBar.add(help);
        return menuBar;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
        if (e.getSource() == homeButton) {
            cardLayout.show(cardHolder, HOME_CARD);
        }
        if (e.getSource() == blueButton) {
            cardLayout.show(cardHolder, BLUE_PANEL);
        }
        if (e.getSource() == redButton) {
            cardLayout.show(cardHolder, RED_PANEL);
        }
        if (e.getSource() == orangeButton) {
            cardLayout.show(cardHolder, ORANGE_PANEL);
        }
    }

    public static void createAndShowGUI() {
        JFrame frame = new JFrame("Simple CardLayout Program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Main main = new Main();
        frame.setJMenuBar(main.createMenuBar());
        frame.add(main.createContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

You have not set an action listener for any of the buttons. 您尚未为任何按钮设置动作侦听器。 Implementing the actionPerformed method of the interface does not automatically set an action listener for the buttons. 实现接口的actionPerformed方法不会自动为按钮设置操作侦听器。 You have to call the addActionListener method which in your case would look like the following since your class implements the ActionListener Interface. 您必须调用addActionListener方法,因为您的类实现了ActionListener接口,因此在您的情况下,它应类似于以下内容。

public static JButton home = new JButton("Home").addActionListener(this); 
public static JButton bluePanel = new JButton("Blue Card").addActionListener(this); 
public static JButton redPanel = new JButton("Red Panel").addActionListener(this); 
public static JButton orangePanel = new JButton("Orange Panel").addActionListener(this); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM