如何通过ActionPerformed获取JButton标签文本?
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());
}
作为JFRAME的初学者,我尝试使用循环创建一些JButton。但是,我不知道如何处理通过相应按钮的操作,因为它们具有相同的变量名称“ tmp”,所以if(event.getSource()== tmp)
可能不适合这种情况。 。
在ActionPerformed中,我尝试通过单击不同的按钮来打印源,结果是: javax.swing.jbutton [,0,0,75x29,alignmentX = 0.0,Alignmenty = 0.5,border = com.apple.apple.laf.aquabuttonborder $ dynamic@4BE40942,flags = 288,maximimsize =,maximimSize =,最小size =,prefimumsize =,preferredsizesize = ,defaulticon =,disabledicon =,disabledsectedicon =,m argin = javax.swing.plaf.insetsuiresource [top = 0,左= 2,底部= 0,右= 2],paintborder = true,paintfocus = true,prastedicon =,prolovereNabled = falsoverenabled = false,frolovericon =,lolloverectecon =,lolloversectedicon =,selectedicon = ,text = 9,defaultcapable = true]
似乎可以在我单击“ 9”按钮时正确获取按钮标签text = 9
。那么,我可以做之类的事情,如果(event.getSource().getText()==“ 9”)
?
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")
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上范围
tmp
是键>键盘
类构造函数。因此,您无法在方法ActionPerformed
中引用它。您将相同的
ActionListener
分配给所有jbutton
s,并且需要一种方法来确定Action Perperformed
方法,哪个jbutton
单击。由于您创建的每个jbutton
都有不同的文本,因此您获得jbutton
文本的想法似乎是一种做到这一点的方法。那么,如何获取在您的Action Performed
方法中单击的jbutton
的文本?A
JButton
具有 buttonmodel 具有 action命令是一个字符串。默认情况下,jbutton
的文本也是其 Action命令。ActionEvent
,即方法ActionPerformed
的参数,声明方法 getActionCommand 返回单击的jbutton
的 Action命令。因此,在actionperformed
方法中获取jbutton
的文本的最简单方法是调用方法getActionCommand
action> action Performed << /代码>方法参数。
此网页也可能有帮助:
如何编写动作
听众分开
actionListener
与每个jbutton
。自Java 8以来,这很容易使用ActionListener
是一个功能接口。为了成为有效的方法参考,方法
hellybutton1
必须返回与方法Action Performed
返回的相同值 -void
,必须具有与方法ActionPerformed
相同的参数。Actually the scope of
tmp
is thefor
loop inKeypad
class constructor. Hence you can't refer to it in methodactionPerformed
.You are assigning the same
ActionListener
to all yourJButton
s and you want a way to determine, in theactionPerformed
method, whichJButton
was clicked. Since everyJButton
you create has a different text, your idea of getting theJButton
text seems like one way to do this. So how can you get the text of theJButton
that was clicked in youractionPerformed
method?A
JButton
has a ButtonModel which has an action command which is a string. By default, the text of theJButton
is also its action command. TheActionEvent
, i.e. the parameter of methodactionPerformed
, declares method getActionCommand which returns the action command of theJButton
that was clicked. Hence the simplest way to get the text of theJButton
, in youractionPerformed
method, is to call methodgetActionCommand
on theactionPerformed
method parameter.This Web page may also be helpful:
How to Write an Action Listener
Alternatively, you can assign a separate
ActionListener
to eachJButton
. Since Java 8, this is easily done using method references becauseActionListener
is a functional interface.In order to be a valid method reference, method
handleButton1
must return the same value that methodactionPerformed
returns – which isvoid
and it must have the same parameters as methodactionPerformed
.