我对Java编码相对较新,刚刚涉及GUI和用户生成事件的主题。我试图制作一个简单的基于按钮的叙述,但是这个错误会继续弹出:
线程“AWT-EventQueue-0”中的异常java.lang.Error:未解决的编译问题: careerNew无法解决变量 responseText无法解析为变量
at SalutonFrame.actionPerformed(SalutonFrame.java:56)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这是我的源代码:
>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编程的职业生涯。谢谢!
答案 0 :(得分:1)
您已将careerNew
声明为本地变量
JButton careerNew = new JButton("Begin your programming career!");
其范围仅限于构造函数。您不能像在actionPerformed()
方法中那样在构造函数体外部引用它。你应该把它变成一个实例变量。您将能够以任何实例方法访问它。
同样适用于responseText
。
答案 1 :(得分:0)
Java变量具有范围,这意味着它们只能在它们所在的括号集中访问。现在,Java无法找到变量responseText和careerNew。要解决这个问题,只需将它们公之于众。