英文:
The method getText() is undifined for the type Button
问题
我正在尝试在Eclipse IDE中创建一个简单的计算器,并在设置按钮的动作时遇到了这个问题。
Button btn7 = new Button("7");
btn7.setFont(new Font("Arial", Font.BOLD, 18));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = btn7.getText();
txtDisplay.setText(EnterNumber);
}
});
我在第5行遇到问题,它显示'方法getText()在button类型中未定义'。
英文:
I am trying to create the simple calculator in eclipse ide and at the time to set the action to the buttons I m facing this problem
Button btn7 = new Button("7");
btn7.setFont(new Font("Arial", Font.BOLD, 18));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = btn7.getText();
txtDisplay.setText(EnterNumber);
}
});
I am facing problem in line no. 5, it is showing their that 'The method getText() is undefined for the type button'
答案1
得分: 1
Button
不要与 JButton
混淆。您可能在寻找 getLabel()
?
String EnterNumber = btn7.getLabel();
https://docs.oracle.com/javase/7/docs/api/java/awt/Button.html#getLabel()
JButton
继承了一个叫做 getText()
的方法,也许您把这两种类型的按钮弄混了?
https://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html
英文:
Button
not to be confused with JButton
. You might be looking for getLabel()
?
String EnterNumber = btn7.getLabel();
https://docs.oracle.com/javase/7/docs/api/java/awt/Button.html#getLabel()
JButton
inherits a method called getText()
, perhaps you have confused the two types of buttons? https://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论