Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在AWT中定义变量_Java_Awt - Fatal编程技术网

Java 无法在AWT中定义变量

Java 无法在AWT中定义变量,java,awt,Java,Awt,我对Java编码比较陌生,刚刚谈到了GUI和用户生成事件。我试图制作一个简单的基于按钮的叙述,但是这个错误仍然会出现: 线程“AWT-EventQueue-0”java.lang中出现异常。错误:未解决的编译问题: careerNew无法解析为变量 responseText无法解析为变量 以下是我的源代码: >import java.awt.*; import javax.swing.*; import java.awt.event.*; >public class Saluton

我对Java编码比较陌生,刚刚谈到了GUI和用户生成事件。我试图制作一个简单的基于按钮的叙述,但是这个错误仍然会出现:

线程“AWT-EventQueue-0”java.lang中出现异常。错误:未解决的编译问题: careerNew无法解析为变量 responseText无法解析为变量

以下是我的源代码:

>import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

>public class SalutonFrame extends JFrame implements ActionListener {
    >public SalutonFrame() {
        super("Saluton Mondo!");
        setLookAndFeel();
        setSize(1000, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flow = new FlowLayout(FlowLayout.CENTER, 10, 10);
        setLayout(flow);
        >//set row 1
            >JButton careerNew = new JButton("Begin your programming career!");
            careerNew.addActionListener(this);
            JLabel responseLabel = new JLabel("YourResponse:", JLabel.RIGHT);
            JComboBox choiceResponse = new JComboBox();
            choiceResponse.addItem("Yes");
            choiceResponse.addItem("No, I'm an idiot");
            JTextField responseText = new JTextField(20);
            add(careerNew);
            add(choiceResponse);
            add(responseLabel);
            add(responseText);
        //set row 2
        JPanel badCareer = new JPanel();
            JButton startBad = new JButton("Start a life of misery");
            JLabel startBadLabel = new JLabel("Your Response, Not that it Matters though:", JLabel.RIGHT);
            JTextField startBadText = new JTextField (20);
            JComboBox startBadCombo = new JComboBox();
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("You dont have a choice");
            startBadCombo.addItem("Dafuq u still looking?");
            badCareer.add(startBad);
            badCareer.add(startBadLabel);
            badCareer.add(startBadCombo);
            badCareer.add(startBadText);
        add(badCareer);




        setVisible(true);
    }
    // sets User-generated event from button careerNew
        public void actionPerformed(ActionEvent event) {
            Object source = event.getSource();
            if (source.equals (careerNew)) {
            CareerGood career = new CareerGood();
            }
            else if (source == responseText) {
                JLabel fart = new JLabel("Idiot");

            }
        }



    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch (Exception exc) {


        }
    }
        public static void main(String[] args) {
            SalutonFrame frame = new SalutonFrame();        }
    }

是的,我确实忽略了Eclipse发送给我的关于运行的消息,尽管存在错误。请帮助我,因为我只是一个初学者,我想从事Java编程的职业。谢谢大家!

您已将
careerNew
声明为局部变量

JButton careerNew = new JButton("Begin your programming career!");
其范围仅限于构造函数。您不能在构造函数主体之外引用它,因为您正试图在
actionPerformed()
方法中引用它。您应该改为将其作为实例变量。您将能够在任何实例方法中访问它


这同样适用于
responseText

Java变量有作用域,这意味着它们只能在括号中访问。现在,Java找不到变量responseText和careerNew。要解决这个问题,只需将其公开。

等等,请原谅我的“无趣”,但我如何将其作为实例变量(我读了罗杰斯·卡德黑德的《24小时内学会Java编程》一书,这是一本好书,只是有点模糊xD)@用户看看关于对象和类的教程。再次,请原谅我的无趣,但这是否意味着,在这种情况下,将careerNew转换为SaleonFrame.careerNew将解决问题吗?还是我把教程看错了?同样,请原谅我的问题,因为我刚刚开始学习该语言。@user不是在构造函数中声明变量,而是在类的主体中声明它。这使得它成为一个实例变量,可以从类的任何其他实例成员访问。在中,
gear
是类
Bicycle
的实例变量。你也需要为
问候语框中的
careerNew
做同样的事情。我爱你,感谢你花时间和精力回答我的问题,因为我被这么多的bug和错误所困扰,我开始感到沮丧,但是有人请你帮忙真的帮助我回到了编程的美妙世界:)
JButton careerNew = new JButton("Begin your programming career!");