程序无法识别java

时间:2016-04-09 22:01:24

标签: java

我想在同一JFrame上使用2个按钮分别执行不同的任务。一个用于更改右侧的标签,另一个用于更改中间圆圈的颜色。 (随机变化的颜色在另一个类上。)

由于某些未知原因,程序似乎无法识别主类(类TwoButtons)中存在的内部类。我对java很新,我找不到我做错了什么....你能帮我解决一下我的问题吗?

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

public class TwoButtons {
    JFrame frame;
    JLabel label;


    public static void main(String[] args) {
        TwoButtons gui = new TwoButtons();
        gui.go();

    }
    public void go(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton labelButton = new JButton("Change label");
        labelButton.addActionListener(new LabelListener());

        JButton colorButton = new JButton("Change cirle");
        colorButton.addActionListener(new ColorListener());

        label = new JLabel("I'm a labele");
        MyDrawPanel drawPanel = new MyDrawPanel();

        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.getContentPane().add(BorderLayout.WEST, labelButton);
        frame.getContentPane().add(BorderLayout.EAST, label);
        frame.setSize(300, 300);
        frame.setVisible(true);

        class LabelListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
            label.setText("Ouch!");

            }
        }
        class ColorListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
            frame.repaint();
            }
        }


        }


    }

我在

上收到错误
labelButton.addActionListener(new LabelListener());

colorButton.addActionListener(new ColorListener());

在两种情况下都说LabelListener和ColorListener都无法解析为类型 非常感谢你提前.. !!

2 个答案:

答案 0 :(得分:2)

您需要将类 LabelListener ColorListener 移出public void go()方法

class LabelListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        label.setText("Ouch!");
    }
}

class ColorListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        frame.repaint();
    }
}

答案 1 :(得分:1)

在java中,您不能在使用它们之后在方法中定义类(就像方法中的变量一样),因此,尝试在ColorListener中定义类LabelListenerclass TwoButtons而不是在go方法中定义它们,如下所示:(这通常是更好的做法)

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

public class TwoButtons {
    JFrame frame;
    JLabel label;

    public static void main(String[] args) {
        TwoButtons gui = new TwoButtons();
        gui.go();
    }

    public void go(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton labelButton = new JButton("Change label");
        labelButton.addActionListener(new LabelListener());

        JButton colorButton = new JButton("Change cirle");
        colorButton.addActionListener(new ColorListener());

        label = new JLabel("I'm a labele");
        MyDrawPanel drawPanel = new MyDrawPanel();

        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.getContentPane().add(BorderLayout.WEST, labelButton);
        frame.getContentPane().add(BorderLayout.EAST, label);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    class LabelListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            label.setText("Ouch!");
        }
    }

    class ColorListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            frame.repaint();
        }
    }
}