为 Jbutton 设置 ActionListener "class"

Setting an ActionListener for a Jbutton "class"

我正在为我的一款游戏重新制作菜单,我使用一种名为 addButton**(text, Image, pressedImage, container) 的方法制作按钮。我的问题是我想使用 addActionListener 方法并允许每个按钮有自己的操作,但我无法这样做。有任何想法吗?

这是菜单代码:

public Menu(Component component) {

        JFrame frame = new JFrame("CityBlock 2d Launcher");
        frame.setSize(new Dimension(1050, 700));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(component);

        // Set up the content pane.
        try {
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO
                    .read(new File("res/menuBackground.png")))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        addComponentsToPane(frame.getContentPane());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void addComponentsToPane(Container pane) {
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

        addAButton("SP", "res/Singleplayer.png",
                "res/Singleplayer_pressed.png", pane);

        addAButton("MP", "res/Multiplayer.png", "res/Multiplayer_Pressed.png",
                pane);

        addAButton("HTP", "res/HowToPlay.png", "res/HowToPlay_Pressed.png",
                pane);

        addAButton("O", "res/Options.png", "res/Options_Pressed.png", pane);

        addAButton("Q", "res/Quit.png", "res/Quit_Pressed.png", pane);

    }

private static void addAButton(final String text, String BtnIcon,
            String PressBtnIcon, Container container) {

        ImageIcon myImage = new ImageIcon(BtnIcon);

        final JButton button = new JButton(/* myImage */text);

        button.setIcon(myImage);

        button.setOpaque(false);
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);

        button.setAlignmentX(Component.LEFT_ALIGNMENT);
        button.setAlignmentY(Component.TOP_ALIGNMENT);

        button.setPressedIcon(new ImageIcon(PressBtnIcon));
        container.add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (button.equals(text)) { <------ **Text** could probably be used to separate the different buttons????
                    // Execute when button is pressed
                    System.exit(0);
                }
            }

        });
    }
}

一种方法是将 ActionListener 传递给 addAButton(),即

private static void addAButton(final String text, String BtnIcon,
        String PressBtnIcon, Container container, ActionListener actionListener) {

然后用

打电话
addAButton("SP", "res/Singleplayer.png",
            "res/Singleplayer_pressed.png", pane, 
   new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           // do stuff
       }
   });

addAButton("MP", "res/Multiplayer.png", "res/Multiplayer_Pressed.png",, pane, 
   new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           // do different stuff
       }
   });

另一种方法是完全拥抱 Swing 并使用 java.swing.Action 通过使用它来定义图标、按钮文本等,然后只需使用 setAction()

在 JButton 上进行设置
JButton button = new JButton();
Action foo = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // do stuff
    }
};
foo.putValue(Action.NAME, "Button text");
foo.putValue(Action.LARGE_ICON_KEY, new ImageIcon("res/foo.png"));
button.setAction(foo);

您可以将按钮的名称作为参数传入。然后设置按钮的名称并使用它来比较它是否是 ActionEvent 的来源。然后为 ActionListener 使用私有 class 而不是匿名 class.

private static void addAButton(final String text, String BtnIcon,
        String PressBtnIcon, Container container, String name) {

  button.setName(name);
  button.addActionListener(new ButtonListener());
  ....
}

将您的 ActionListener class 放在方法之外 class。

// In the Menu class
private class ButtonListener implements ActionListener{
  @Override
  public void actionPerformed(ActionEvent e){
    if(((JButton) e.getSource()).getName().equals(nameOfButton)){
      //do something
    }
   else if(((JButton)e.getSource()).getName().equals(nameOfDifferentButton)){
      //do something different
    }
  }
}