public Keypad() {
setLayout(new GridLayout(4,3));
for(int i=9; i>=0; i--){
JButton tmp = new JButton(Integer.toString(i));
add(tmp);
tmp.addActionListener(this);
}
JButton btnPoint = new JButton(".");
JButton btnEqual = new JButton("=");
add(btnPoint);
btnPoint.addActionListener(this);
add(btnEqual);
btnEqual.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
public void actionPerformed(ActionEvent event) {
System.out.println(event.getSource());
}
As a beginner of JFrame, I try to create some JButton with for loop. However, I have no idea how to handle the actionPerformed by corresponding button as they have the same variable name "tmp", so if(event.getSource() == tmp)
may not suitable for this case.
In the actionPerformed, I try to print out the source by clicking different button, the result is: javax.swing.JButton[,0,0,75x29,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@4be40942,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=9,defaultCapable=true]
It seem that it can get the button label text=9
correctly when I click button "9". so can I do something like if(event.getSource().getText() == "9")
?
CodePudding user response:
how to handle the actionPerformed by corresponding button as they have the same variable name "tmp"
Actually the scope of tmp
is the for
loop in Keypad
class constructor. Hence you can't refer to it in method actionPerformed
.
You are assigning the same ActionListener
to all your JButton
s and you want a way to determine, in the actionPerformed
method, which JButton
was clicked. Since every JButton
you create has a different text, your idea of getting the JButton
text seems like one way to do this. So how can you get the text of the JButton
that was clicked in your actionPerformed
method?
A JButton
has a ButtonModel which has an action command which is a string. By default, the text of the JButton
is also its action command. The ActionEvent
, i.e. the parameter of method actionPerformed
, declares method getActionCommand which returns the action command of the JButton
that was clicked. Hence the simplest way to get the text of the JButton
, in your actionPerformed
method, is to call method getActionCommand
on the actionPerformed
method parameter.
public void actionPerformed(java.awt.event.ActionEvent event) {
String buttonText = event.getActionCommand();
switch (buttonText) {
case "1":
// Handle button "1".
break;
// Remaining cases...
}
}
This Web page may also be helpful:
How to Write an Action Listener
Alternatively, you can assign a separate ActionListener
to each JButton
. Since Java 8, this is easily done using method references because ActionListener
is a functional interface.
public Keypad() {
setLayout(new GridLayout(4,3));
for(int i=9; i>=0; i--){
JButton tmp = new JButton(Integer.toString(i));
add(tmp);
switch (i) {
case 1:
tmp.addActionListener(this::handleButton1);
break;
// Remaining cases...
}
tmp.addActionListener(this);
}
}
private void handleButton1(java.awt.event.ActionEvent event) {
// No need to check the "event" source since the source is always the same.
}
In order to be a valid method reference, method handleButton1
must return the same value that method actionPerformed
returns – which is void
and it must have the same parameters as method actionPerformed
.