程序无法识别java中的内部类

问题描述 投票:0回答:2

我想在同一个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都无法解析为类型。 非常感谢你提前.. !!

java
2个回答
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
投票

在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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.