如何将actionlistenerand actioncommand放到多个jbuttons

所以我希望我的按钮标记为1-9,但我不想列出每个按钮的所有动作监听器和动作命令。 我怎样才能做到这一点

而且我也不能使用add.ActionListener(this)所以我可以使用什么

JButton[] button = new JButton[9]; panel.setLayout(new GridLayout(3,3)); for (int i = 0; i < button.length; i++) { button[i] = new JButton(); panel.add(button[i]); String bu = Integer.toString(i); button[i].setActionCommand(bu); button[i].addActionListener(new ActionListener()); 

对不起,我刚接触java swing,所以它的abit仍然令人困惑

我不能使用add.ActionListener(this)所以我可以使用什么

您创建一个实现ActionListener的类。

或者更好的是创建一个实现Action的类。 Action与ActionListener相同。 好处是Action可以与Key Bindings一起使用。

这是一个基本的例子:

 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class CalculatorPanel extends JPanel { private JTextField display; public CalculatorPanel() { Action numberAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { // display.setCaretPosition( display.getDocument().getLength() ); display.replaceSelection(e.getActionCommand()); } }; setLayout( new BorderLayout() ); display = new JTextField(); display.setEditable( false ); display.setHorizontalAlignment(JTextField.RIGHT); add(display, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout( new GridLayout(0, 5) ); add(buttonPanel, BorderLayout.CENTER); for (int i = 0; i < 10; i++) { String text = String.valueOf(i); JButton button = new JButton( text ); button.addActionListener( numberAction ); button.setBorder( new LineBorder(Color.BLACK) ); button.setPreferredSize( new Dimension(30, 30) ); buttonPanel.add( button ); InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(KeyStroke.getKeyStroke(text), text); inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text); button.getActionMap().put(text, numberAction); } } private static void createAndShowUI() { JFrame frame = new JFrame("Calculator Panel"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.add( new CalculatorPanel() ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } 

现在,您可以单击按钮或键入数字,值将插入文本字段。

只需添加主要操作执行方法。

示例如下:

 public void actionPerformed(ActionEvent e){ // your todo code here } 

确保导入适当的包。

您必须实现actionListener

public class Butt实现ActionListener {

 public JPanel method() { JPanel panel = new JPanel(); JButton[] button = new JButton[9]; panel.setLayout(new GridLayout(3, 3)); for (int i = 0; i < button.length; i++) { button[i] = new JButton(""+i); panel.add(button[i]); String bu = Integer.toString(i); button[i].setActionCommand(bu); button[i].addActionListener(this); } return panel; } @Override public void actionPerformed(ActionEvent e) { } public static void main(String[] args) { JFrame frame = new JFrame(); frame.add(new Butt().method()); frame.setVisible(true); frame.setSize(500, 500); } 

现在看,没有错误。

结果

而且我也不能使用add.ActionListener(this)所以我可以使用什么

我将在这里解释你的意思是“你不允许容器类实现ActionListener,但仍允许使用ActionListener”。

如果是这种情况,您至少还有2个选择:

  • 为ActionListener创建一个匿名类
  • 为ActionListener创建一个内部类

使用GridLayout和内部类Actionlistener的示例:

在此处输入图像描述

如何将actionlistenerand actioncommand放到多个jbuttons

以下使用内部类来处理按钮的操作。

 class MainPanel extends JPanel //not implementing ActionListener here { private JButton[] btns; public MainPanel(){ setPreferredSize(new Dimension(150, 150)); setLayout(new GridLayout(3, 3)); initComponents(); addComponents(); } private void initComponents(){ btns = new JButton[9]; ButtonHandler bh = new ButtonHandler(); for(int x=0; x 

最后,在EDT中运行您的代码:

 class TestRunner { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ @Override public void run(){ JFrame frame = new JFrame("Buttons Pad"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MainPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }