[英]java program that has three buttons, each displaying a different text that when pressed will display the text on the button in a text box
I have an assignment to write a program that has three buttons, each displaying a different text that when pressed will display the text on the button in a text box. 我的任务是编写一个包含三个按钮的程序,每个按钮显示一个不同的文本,按下该按钮将在文本框中显示按钮上的文本。 I think I have the basics of the program down but I can't get it to run.
我认为我已经了解了该程序的基础知识,但无法运行它。 I've tried watching tutorials and reading up on the error I've been getting but I can't seem to figure it out.
我曾尝试观看教程并阅读我所遇到的错误,但似乎无法弄清楚。 I am new to programming in java and have been pretty confused throughout the whole course.
我是Java编程新手,在整个课程中一直很困惑。 Any help would be greatly appreciated!
任何帮助将不胜感激!
right now my errors are: 现在我的错误是:
non-static variable this cannot be referenced from a static context 非静态变量,不能从静态上下文中引用
objButton1.addActionListener(this); objButton1.addActionListener(this);
non-static variable this cannot be referenced from a static context 非静态变量,不能从静态上下文中引用
objButton2.addActionListener(this); objButton2.addActionListener(this);
non-static variable this cannot be referenced from a static context 非静态变量,不能从静态上下文中引用
objButton3.addActionListener(this); objButton3.addActionListener(this);
cannot find symbol 找不到标志
if (e.getSource()==objButton1) 如果(e.getSource()== objButton1)
cannot find symbol 找不到标志
else if (e.getSource()==objButton2) 否则(e.getSource()== objButton2)
import java.applet.Applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Option3 extends Frame implements ActionListener
{
Option3()
{
setTitle("Option 3");
setSize (300,300);
show();
}
public static void main (String args[])
{
Frame objFrame;
Button objButton1;
Button objButton2;
Button objButton3;
TextField objTextField;
objFrame = new Option3();
objButton1 = new Button("A");
objButton2 = new Button("B");
objButton3 = new Button("C");
objTextField = new TextField(100);
objButton1.addActionListener(this);
objButton2.addActionListener(this);
objButton3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == objButton1 )
System.out.println("A");
else if (e.getSource() == objButton2 )
System.out.println("B");
else
System.out.println("C");
}
}
There are a couple of things you need to change in order to make this work. 为了使此工作有效,您需要更改几件事。
1) If you want to access the buttons in the actionPerformed(...)
method, you need to increase their scope. 1)如果要访问
actionPerformed(...)
方法中的按钮,则需要扩大其范围。 As it is now, those variables can be accessed only in the main(...)
method. 现在,这些变量只能在
main(...)
方法中访问。
2) You need to pass an instance of ActionListener
to the addActionListener(...)
method and because main(...)
is a static
method, you can't use the this
keyword, what you can do is to use the Option3
instance that you've just created or a better solution will be to create those components inside the Option3
constructor where you can use this
. 2)您需要将
ActionListener
的实例传递给addActionListener(...)
方法,并且由于main(...)
是static
方法,因此不能使用this
关键字,您可以使用Option3
您刚刚创建的实例或更好的解决方案是在Option3
构造函数内创建那些组件,您可以在其中使用this
。
3) If you want to display the components you've created, you need to add them to the frame. 3)如果要显示已创建的组件,则需要将它们添加到框架中。
import java.applet.Applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Option3 extends Frame implements ActionListener {
Button objButton1;
Button objButton2;
Button objButton3;
TextField objTextField;
Option3() {
setTitle("Option 3");
setSize (300,300); // is better to control the frame's size by using panels with appropriate layout managers.
objButton1 = new Button("A");
objButton2 = new Button("B");
objButton3 = new Button("C");
objTextField = new TextField(100);
objButton1.addActionListener(this);
objButton2.addActionListener(this);
objButton3.addActionListener(this);
Panel panel = new Panel(); // set a layout to this panel based on how you want the components to be displayed.
panel.add(objButton1);
panel.add(objButton2);
panel.add(objButton3);
panel.add(objTextField);
add(panel);
show();
}
public static void main (String args[]) {
Frame objFrame = new Option3();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == objButton1 ) { //delimit this kind of statements using curly braces to avoid confusion and bugs.
System.out.println("A");
} else if (e.getSource() == objButton2 ) {
System.out.println("B");
} else {
System.out.println("C");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.