问题
I'm creating a keyboard using buttons with java, when a user clicks on a button labelled from A to Z it will set a JTextField text to A or whatever button they pressed.
I have a seperate class for each button so for A its public class listenser1 implements ActionListener
, B its public class listenser2 implements ActionListener
is this a good way of doing it?
Also I tried to do do it under one class and used if and if else statements buy using
if(a.getText().equals("A"))
{
input1.setText(input.getText() + "A");
}
.
.
.
And this doesn't work, it prints out ABCDEFGHIJKLMNOPQRSTUVWXYZ instead of just the one letter.
回答1:
No, that is not the most efficient way. That takes way too long to write. Instead, try this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class Example extends JFrame implements ActionListener {
private final String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
private JButton[] buttons = new JButton[26];
private JTextArea text = new JTextArea();
public Example() {
super("Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < letters.length; i++) {
buttons[i] = new JButton(letters[i]);
buttons[i].addActionListener(this);
add(buttons[i]);
}
add(text);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
text.append(event.getActionCommand());
}
public static void main(String[] args) {
Example ex = new Example();
}
}
回答2:
I'd suggest creating one instance of the listener per action. Anonymous inner classes can easily be parameterised by reading an final
local.
void addButton(final char letter) {
JButton button = new JButton(Character.toString(letter));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
input.setText(input.getText() + letter);
}
});
panel.add(button);
}
(I'd suggest going straight for a Document
rather than JtextComponent.getText
/setText
.)
回答3:
Why not you use a single class say like this :
ActionListener commonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o instanceof JButton) {
JButton button = (JButton)o;
String command = (String) button.getActionCommand()();
input1.setText(command);
}
}
};
And you can add this listener to your buttons as :
buttonA = new JButton();
buttonA.addActionListener(commonListener);
buttonA.setActionCommand("A"); // same for others till Z.
Hope this might help you in some way.
回答4:
You can write one parametrized listener:
private class LetterActionListener implements ActionListener {
private final char letter;
public LetterActionListener(char letter) {
this.letter = letter;
}
@Override
public void actionPerformed(ActionEvent e) {
input1.setText(input1.getText() + letter);
}
}
回答5:
Create one instance of an ActionListener
and add it to each button in a loop:
final JTextField input1;
...
final ActionListener l = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input1.setText(input1.getText() + ((JButton) e.getSource()).getText());
}
};
for (char c = 'A'; c <= 'Z'; c++) {
final JButton jButton = new JButton(String.valueOf(c));
panel.add(jButton);
jButton.addActionListener(l);
}
来源:https://stackoverflow.com/questions/9007648/should-i-use-separate-actionlistener-for-each-similar-action-or-generic-one