如何将 actionlistener 和 actioncommand 放到多个 jbuttons
how to put actionlistenerand actioncommand to multiple 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 的新手所以它仍然有点混乱
只需添加执行主要操作的方法。
示例如下:
public void actionPerformed(ActionEvent e){
// your todo code here
}
确保导入适当的包。
I cannot use add.ActionListener(this) so what can i use
您创建了一个实现 ActionListener 的 class。
或者更好的是创建一个 class 来实现 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();
}
});
}
}
现在您可以单击按钮或键入数字,值将插入到文本字段中。
您必须实现 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);
}
}
现在看没有错误了。
and also I cannot use add.ActionListener(this) so what can i use
我会把你这里的意思解释为"you are not allowed to let the container class implements ActionListener, but still allowed to use ActionListener"。
如果是这样,您至少还有 2 个选择:
- 为 ActionListener
创建一个匿名 class
- 为 ActionListener
创建一个内部 class
使用 GridLayout with inner-class Actionlistener 的示例:
how to put actionlistenerand actioncommand to multiple jbuttons
下面使用内部 class 来处理按钮的动作。
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<btns.length; x++){
btns[x] = new JButton(Integer.toString(x+1));
btns[x].addActionListener(bh); //NOT using addActionListener(this)
}
}
private void addComponents(){
for(int x=0; x<btns.length; x++)
add(btns[x]); //Add in sequential order into grid layout
}
private class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e){
String s = ((JButton)e.getSource()).getText();
JOptionPane.showMessageDialog(null, "Button " + s + " was clicked.");
}
}
}
最后,运行 你在 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);
}
});
}
}
所以我希望我的按钮被标记为 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 的新手所以它仍然有点混乱
只需添加执行主要操作的方法。
示例如下:
public void actionPerformed(ActionEvent e){
// your todo code here
}
确保导入适当的包。
I cannot use add.ActionListener(this) so what can i use
您创建了一个实现 ActionListener 的 class。
或者更好的是创建一个 class 来实现 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();
}
});
}
}
现在您可以单击按钮或键入数字,值将插入到文本字段中。
您必须实现 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);
}
} 现在看没有错误了。
and also I cannot use add.ActionListener(this) so what can i use
我会把你这里的意思解释为"you are not allowed to let the container class implements ActionListener, but still allowed to use ActionListener"。
如果是这样,您至少还有 2 个选择:
- 为 ActionListener 创建一个匿名 class
- 为 ActionListener 创建一个内部 class
使用 GridLayout with inner-class Actionlistener 的示例:
how to put actionlistenerand actioncommand to multiple jbuttons
下面使用内部 class 来处理按钮的动作。
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<btns.length; x++){
btns[x] = new JButton(Integer.toString(x+1));
btns[x].addActionListener(bh); //NOT using addActionListener(this)
}
}
private void addComponents(){
for(int x=0; x<btns.length; x++)
add(btns[x]); //Add in sequential order into grid layout
}
private class ButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e){
String s = ((JButton)e.getSource()).getText();
JOptionPane.showMessageDialog(null, "Button " + s + " was clicked.");
}
}
}
最后,运行 你在 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);
}
});
}
}