JButtons的范围混乱

时间:2013-12-05 21:58:36

标签: java swing events scope actionlistener

好的,所以我修改了JButtons的范围,我将它们放在类的主体而不是方法的主体中。那么,为什么我会收到此错误?

  

线程“main”中的异常java.lang.Error:未解决的编译问题:

                          at SalutonFrame.main(SalutonFrame.java:78)

这是我的源代码:

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

public class SalutonFrame extends JFrame implements ActionListener {
    public JButton careerNew = new JButton("Begin your programming career!");
    careerNew.addActionListener(this);
    JTextField responseText = new JTextField(20);
    JTextField.addActionListener(this);
    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
            JLabel responseLabel = new JLabel("YourResponse:", JLabel.RIGHT);
            JComboBox choiceResponse = new JComboBox();
            choiceResponse.addItem("Yes");
            choiceResponse.addItem("No, I'm an idiot");
            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();        }
    }

帮助?

1 个答案:

答案 0 :(得分:0)

移动这两条指令

careerNew.addActionListener(this);

JTextField.addActionListener(this);

在您构造函数中调用super之后。

这是一个完整的版本:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class SalutonFrame extends JFrame implements ActionListener {
    public JButton careerNew = new JButton("Begin your programming career!");
    JTextField responseText = new JTextField(20);

    public SalutonFrame() {
        super("Saluton Mondo!");
        careerNew.addActionListener(this);
        responseText.addActionListener(this);
        setLookAndFeel();
        setSize(1000, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flow = new FlowLayout(FlowLayout.CENTER, 10, 10);
        setLayout(flow);
        // set row 1
        JLabel responseLabel = new JLabel("YourResponse:", JLabel.RIGHT);
        JComboBox choiceResponse = new JComboBox();
        choiceResponse.addItem("Yes");
        choiceResponse.addItem("No, I'm an idiot");
        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();
    }
}