如何在Java Swing中找到最后按下的按钮

0 java swing

我现在正在 Java Swing 项目中使用 Java JDK 14.0.2。我正在使用 Eclipse(如果这很重要)。我正在尝试使用附加到我的 JButton 的动作侦听器来查看之前按下了哪个按钮,然后根据之前按下的按钮执行不同的动作。

我有以下项目:

测试框架

package TestSwing;

import javax.swing.JFrame;

public class TestFrame extends JFrame {

    public TestFrame() {
        super();
        
        this.add(new TestPanel());
        
        this.pack();
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setVisible(true);
    }

}
Run Code Online (Sandbox Code Playgroud)

TestFrame.java 正在主要方法所在的不同文件中实例化。

测试面板

package TestSwing;

import javax.swing.JPanel;

public class TestPanel extends JPanel {

    public TestPanel() {
        super();
        
        this.add(new TestButton1());
        this.add(new TestButton2());
    }

}
Run Code Online (Sandbox Code Playgroud)

现在问题的重要部分两个按钮:

测试按钮1.java

package TestSwing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class TestButton1 extends JButton {

    public TestButton1() {
        super();
        
        this.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (/*last button pressed before this one is TestButton2*/) {
                    System.out.println("Button 2 was pressed last");
                }
                else if (/*last button pressed before this one was itself*/) {  // It is important that this is else if
                    System.out.println("Button 1 was pressed last");
                }
                
            }
            
        });
        
        this.setText("Button 1");
    }

}
Run Code Online (Sandbox Code Playgroud)

这是按钮 2:

package TestSwing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class TestButton2 extends JButton {

    public TestButton2() {
        super();
        
        this.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ;
                
            }
            
        });
        
        this.setText("Buttton 2");
    }

}
Run Code Online (Sandbox Code Playgroud)

我已经尝试为此寻找解决方案,但找不到我要找的东西,我也不知道如何自己开始这样做。在此先感谢您的帮助。

Don*_*ter 5

  1. 不要扩展 JButton。没有必要,它只会混淆,因为您在 JButton 类中有内部侦听器。
  2. 将侦听器与按钮分开——将其置于 JButton 类之外。
  3. 为什么不给两个按钮相同的动作监听器?
  4. 在侦听器中添加一个 JButton 字段,该字段分配给 ActionEvent 的源引用(通过 获得e.getSource())。这将保留对最后按下的按钮的引用。

例如:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.*;

public class LastButtonPressed  {
    private JButton lastButton = null;
    private JButton previousButton = null;
    private JTextField lastButtonsText = new JTextField(10);
    private JTextField previousButtonsText = new JTextField(10);
    private JPanel mainPanel = new JPanel(new BorderLayout());
    
    public LastButtonPressed() {
        int sides = 8;
        JPanel buttonGridPanel = new JPanel(new GridLayout(sides, sides));
        ActionListener listener = e -> {
            previousButton = lastButton;
            lastButton = (JButton) e.getSource();
            previousButtonsText.setText(lastButtonsText.getText());
            lastButtonsText.setText(e.getActionCommand());
        };
        for (int i = 0; i < sides * sides; i++) {
            String text = "Button " + (i + 1);
            JButton button = new JButton(text);
            button.addActionListener(listener);
            buttonGridPanel.add(button);
        }
        
        JPanel topPanel = new JPanel();
        topPanel.add(new JLabel("Previous Button:"));
        topPanel.add(previousButtonsText);
        topPanel.add(Box.createHorizontalStrut(20));
        topPanel.add(new JLabel("Last Button:"));
        topPanel.add(lastButtonsText);
        
        
        mainPanel.add(topPanel, BorderLayout.PAGE_START);
        mainPanel.add(buttonGridPanel);
    }
    
    public JPanel getMainPanel() {
        return mainPanel;
    }
    
    
    public JButton getLastButton() {
        return lastButton;
    }

    public JButton getPreviousButton() {
        return previousButton;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Last Button Pressed");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new LastButtonPressed().getMainPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            
        });
    }
    
}
Run Code Online (Sandbox Code Playgroud)
  • 更新以添加以前的按钮信息

代码部分说明

保持最后一个按钮按下以及在最后一个按钮之前按下的前一个按钮的变量(字段):

private JButton lastButton = null;
private JButton previousButton = null;
Run Code Online (Sandbox Code Playgroud)

JTextFields 显示最后一个按钮上的文本:

private JTextField lastButtonsText = new JTextField(10);
private JTextField previousButtonsText = new JTextField(10);
Run Code Online (Sandbox Code Playgroud)

保存应用程序的主 JPanel:

private JPanel mainPanel = new JPanel(new BorderLayout());
Run Code Online (Sandbox Code Playgroud)

创建一个 JPanel,其中包含一个 8x8 的 JButton 网格:

int sides = 8;
JPanel buttonGridPanel = new JPanel(new GridLayout(sides, sides));
Run Code Online (Sandbox Code Playgroud)

创建一个添加到按钮的 ActionListener。在侦听器中,设置上一个按钮和最后一个按钮,并更新 JTextFields 保存的文本:

ActionListener listener = e -> {
    previousButton = lastButton;
    lastButton = (JButton) e.getSource();
    previousButtonsText.setText(lastButtonsText.getText());
    lastButtonsText.setText(e.getActionCommand());
};
Run Code Online (Sandbox Code Playgroud)

在 for 循环中,创建 8x8 JButton,将 ActionListener 添加到每个按钮并将每个按钮添加到 JPanel 网格:

for (int i = 0; i < sides * sides; i++) {
    String text = "Button " + (i + 1);
    JButton button = new JButton(text);
    button.addActionListener(listener);
    buttonGridPanel.add(button);
}
Run Code Online (Sandbox Code Playgroud)

将所有内容添加到子 JPanel 和主 JPanel:

JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Previous Button:"));
topPanel.add(previousButtonsText);
topPanel.add(Box.createHorizontalStrut(20));
topPanel.add(new JLabel("Last Button:"));
topPanel.add(lastButtonsText);


mainPanel.add(topPanel, BorderLayout.PAGE_START);
mainPanel.add(buttonGridPanel);
Run Code Online (Sandbox Code Playgroud)

以 Swing 线程安全的方式创建一个 JFrame,创建我们的 LastButtonPressed 实例并将其主 JPanel 添加到 JFrame,最后显示 JFrame:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        JFrame frame = new JFrame("Last Button Pressed");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new LastButtonPressed().getMainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
    });
}
Run Code Online (Sandbox Code Playgroud)