简体   繁体   English

具有三个按钮的Java程序,每个按钮显示一个不同的文本,按下该按钮将在文本框中显示按钮上的文本

[英]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.

相关问题 按下多个按钮时如何在屏幕上显示文本 - How to display text onscreen when multiple buttons are pressed 在Java中按下按钮时如何设置文本的文本颜色? - How to set text color of a text when a button is pressed in Java? 如果选中单选按钮,则在 Java 中显示一个文本框 - If radio button is checked, display a text box in Java 按下按钮时显示数组中的随机文本 - Display Random Text From Array when Button is Pressed 按下按钮后在文本小部件中显示分数 - Display score in text widget after button pressed Android Studio - 如果按下按钮且文本框中没有文本,则显示一个字符串,输入文本时显示另一个字符串 - Android Studio- Display one string if a button is pressed with no text in the textbox and another when text is inputted 如果文本在排序程序中有投票则显示文本 - Displaying text if it has votes in sort program 按下按钮后,检查文本框中的文本是否与字符串匹配 - Checking if text in a textbox matches a string once a button has been pressed 当按钮具有特定的当前文本时,如何更改按钮上的文本(Android Studio / Java和XML) - How to change text on button when the button has a particular current text (Android Studio/Java&XML) 在Android中按下按钮时如何设置单选按钮的文本颜色? - How to set text color of a radio button when a button is pressed in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM