The method getText() is undefined for the type Button.

huangapple go评论151阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年8月12日 03:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63365084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定