Java 从 jButton 获取文本值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20082051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting text value from a jButton
提问by user3010613
So I need to simply check whether a clicked button's text is "X"
or "O"
(making tic tac toe)
This code doesn't work:
所以我需要简单地检查一个被点击的按钮的文本是否是"X"
或"O"
(使井字游戏)这段代码不起作用:
if (jButton1.getText()=="X")
However the following code does work:
但是,以下代码确实有效:
String jButText = jButton1.getText();
if (jButText=="X")
Why doesn't the first bit of code work when the second does? Does it need to be something more like if (jButton1.getText().toString=="X"
)? By the way, I don't think toString
exists in Java. That is just somewhat the equivalent in Visual Basic, which is what I normally use to create GUIs.
为什么第一个代码不起作用而第二个代码起作用?是否需要更像 if ( jButton1.getText().toString=="X"
)?顺便说一下,我认为toString
Java 中不存在。这在某种程度上与 Visual Basic 中的等价,这是我通常用来创建 GUI 的。
采纳答案by spydon
This behavior is not reproducible in java 1.7.0_45 or 1.7.0_25, it might be a weird occurrence of String interningfor your java version.
这种行为在 java 1.7.0_45 或 1.7.0_25 中是不可重现的,对于您的 java 版本来说,这可能是一个奇怪的字符串实习。
In order for your code to work properly on all java versions you have to use equals()
为了让您的代码在所有 Java 版本上正常工作,您必须使用 equals()
==
compares objects meanwhile .equals()
compares the content of the string objects.
==
比较对象同时.equals()
比较字符串对象的内容。
jButton1.getText().equals("X")
回答by Victor Ribeiro da Silva Eloy
in java when you compare strings using == , you're comparing their memory addresses, to check if two strings contains same text you should call .equals()
在java中,当您使用 == 比较字符串时,您正在比较它们的内存地址,以检查两个字符串是否包含相同的文本,您应该调用 .equals()
if ("X".equals(jButton1.getText()))
回答by Roy Slezak
This was also driving me nuts when using AWT Button class... Here's the answer: There is no .getText() method for a Button... you need to use .getLabel()
这也让我在使用 AWT Button 类时发疯......这是答案:按钮没有 .getText() 方法......你需要使用 .getLabel()
Now, the story for JButtons: Depending on your version of java, getLabel() was deprecated, and finally replaced by getText... Aren't namespaces wonderful?
现在,关于 JButtons 的故事:根据您的 Java 版本,不推荐使用 getLabel(),最终被 getText 取代...命名空间不是很好吗?
回答by Suhad Mendis
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyFrame extends JFrame{
JButton equalsButton;
JLabel ansLabel;
JLabel addLabel;
JTextField text1;
JTextField text2;
MyFrame (){
setSize(300,300);
setDefaultCloseOperation(3);
setLayout(new FlowLayout());
text1=new JTextField(10);
add(text1);
addLabel=new JLabel("+");
add(addLabel);
text2=new JTextField(10);
add(text2);
equalsButton=new JButton("=");
equalsButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
int num1=Integer.parseInt(text1.getText());
int num2=Integer.parseInt(text2.getText());
int tot=num1+num2;
ansLabel.setText(Integer.toString(tot));
}
});
add(equalsButton);
ansLabel=new JLabel(" ");
add(ansLabel);
pack();
setVisible(true);
}
}
class Demo{
public static void main(String args[]){
MyFrame f1=new MyFrame();
}
}