我的 actionListener 调用出了什么问题

发布于 2025-01-05 09:29:52 字数 4016 浏览 1 评论 0原文

我收到错误:类 RedListener 中的构造函数 RedListener 无法应用于给定类型; jrbRed.addActionListener(new RedListener(canvas, canvas2));

我为每位听众准备了一份。该程序应该是一个交通灯,当我单击一个单选按钮时,它会“亮起”。如果它没有被点击那么它只是应该用颜色勾勒出来

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



public class Lab4Frame extends JFrame {
    //public boolean red, yellow, green;
    Lab4Frame(){
        this.setLayout(new BorderLayout());
        setTitle("Lab 4 - Application #1");
        Lab4Panel p = new Lab4Panel();
        Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();
        add(p, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
    }

    public static void main(String[] args){

            Lab4Frame frame = new Lab4Frame();
            frame.setTitle("Lab4 Application # 1");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 600);
            frame.setVisible(true);
    }

}

class Lab4RadioButtonPanel extends JPanel {
        Lab4Panel canvas = new Lab4Panel();
        Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();
    public Lab4RadioButtonPanel() {
        boolean red, green, yellow;



        this.setLayout(new FlowLayout());
        JRadioButton jrbRed = new JRadioButton("Red", true);
        JRadioButton jrbYellow = new JRadioButton("Yellow");
        JRadioButton jrbGreen = new JRadioButton("Green");

        this.setBorder(BorderFactory.createLineBorder(Color.black));

        ButtonGroup group = new ButtonGroup();
        group.add(jrbRed);
        group.add(jrbYellow);
        group.add(jrbGreen);

        this.add(jrbRed);
        this.add(jrbYellow);
        this.add(jrbGreen);

        jrbRed.addActionListener(new RedListener(canvas, canvas2));
        jrbYellow.addActionListener(new YellowListener(canvas, canvas2));
        jrbGreen.addActionListener(new GreenListener(canvas, canvas2));

    }
}










class Lab4Panel extends JPanel{


    public Lab4Panel(){}



    boolean red, green, yellow;
    int radius = 5;
    int x = -1;
    int y = -1;

    public void setRed(){
        red = true;
        repaint();
    }

    protected void paintComponent(Graphics g){
        if (x<0 || y<0) {
            x = getWidth() / 2 - radius;
            y = getHeight() / 2 - radius;
        }
        super.paintComponent(g);
        g.drawRect(x - 10,y - 90, 40, 120);
        g.drawOval(x,y - 80, 4 * radius, 4 * radius);
        g.drawOval(x,y - 40, 4 * radius, 4 * radius);
        g.drawOval(x,y, 4 * radius, 4 * radius);
        g.drawRect(x - 5,y - 90, 40, 120);

        if(red){
            g.setColor(Color.RED);
            g.fillOval(x,y - 80, 4 * radius, 4 * radius);
            repaint();
        }

        else if (yellow){
            g.setColor(Color.YELLOW);
            g.fillOval(x,y - 40, 4 * radius, 4 * radius);
            repaint();
        }

        if(green){
            g.setColor(Color.GREEN);
            g.fillOval(x,y, 4 * radius, 4 * radius);
            repaint();
        }

    }


}


class RedListener implements ActionListener{
    private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas2;

    RedListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas2.setRed();
    }
}

class YellowListener implements ActionListener{
    private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas2;

    YellowListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas2.setRed();
    }
}

class GreenListener implements ActionListener{
    private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas2;

    GreenListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas2.setRed();
    }
}

I am getting an error: constructor RedListener in class RedListener cannot be applied to given types;
jrbRed.addActionListener(new RedListener(canvas, canvas2));

I am getting one for each of the listeners. The program is supposed to be a traffic light that when I click on a radio button that light "lights" up. if its not clicked then its just supposed to be outlined in the color

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



public class Lab4Frame extends JFrame {
    //public boolean red, yellow, green;
    Lab4Frame(){
        this.setLayout(new BorderLayout());
        setTitle("Lab 4 - Application #1");
        Lab4Panel p = new Lab4Panel();
        Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();
        add(p, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
    }

    public static void main(String[] args){

            Lab4Frame frame = new Lab4Frame();
            frame.setTitle("Lab4 Application # 1");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 600);
            frame.setVisible(true);
    }

}

class Lab4RadioButtonPanel extends JPanel {
        Lab4Panel canvas = new Lab4Panel();
        Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();
    public Lab4RadioButtonPanel() {
        boolean red, green, yellow;



        this.setLayout(new FlowLayout());
        JRadioButton jrbRed = new JRadioButton("Red", true);
        JRadioButton jrbYellow = new JRadioButton("Yellow");
        JRadioButton jrbGreen = new JRadioButton("Green");

        this.setBorder(BorderFactory.createLineBorder(Color.black));

        ButtonGroup group = new ButtonGroup();
        group.add(jrbRed);
        group.add(jrbYellow);
        group.add(jrbGreen);

        this.add(jrbRed);
        this.add(jrbYellow);
        this.add(jrbGreen);

        jrbRed.addActionListener(new RedListener(canvas, canvas2));
        jrbYellow.addActionListener(new YellowListener(canvas, canvas2));
        jrbGreen.addActionListener(new GreenListener(canvas, canvas2));

    }
}










class Lab4Panel extends JPanel{


    public Lab4Panel(){}



    boolean red, green, yellow;
    int radius = 5;
    int x = -1;
    int y = -1;

    public void setRed(){
        red = true;
        repaint();
    }

    protected void paintComponent(Graphics g){
        if (x<0 || y<0) {
            x = getWidth() / 2 - radius;
            y = getHeight() / 2 - radius;
        }
        super.paintComponent(g);
        g.drawRect(x - 10,y - 90, 40, 120);
        g.drawOval(x,y - 80, 4 * radius, 4 * radius);
        g.drawOval(x,y - 40, 4 * radius, 4 * radius);
        g.drawOval(x,y, 4 * radius, 4 * radius);
        g.drawRect(x - 5,y - 90, 40, 120);

        if(red){
            g.setColor(Color.RED);
            g.fillOval(x,y - 80, 4 * radius, 4 * radius);
            repaint();
        }

        else if (yellow){
            g.setColor(Color.YELLOW);
            g.fillOval(x,y - 40, 4 * radius, 4 * radius);
            repaint();
        }

        if(green){
            g.setColor(Color.GREEN);
            g.fillOval(x,y, 4 * radius, 4 * radius);
            repaint();
        }

    }


}


class RedListener implements ActionListener{
    private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas2;

    RedListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas2.setRed();
    }
}

class YellowListener implements ActionListener{
    private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas2;

    YellowListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas2.setRed();
    }
}

class GreenListener implements ActionListener{
    private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas2;

    GreenListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas2.setRed();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城仅有旧梦在 2025-01-12 09:29:52

你有这个:

Lab4Panel canvas = new Lab4Panel();
Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

和这个:

jrbRed.addActionListener(new RedListener(canvas, canvas2));

但是你的构造函数是这样的:

RedListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2)

你可能想要:

jrbRed.addActionListener(new RedListener(canvas2, canvas));

即你颠倒了参数的顺序。

You have this:

Lab4Panel canvas = new Lab4Panel();
Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

and this:

jrbRed.addActionListener(new RedListener(canvas, canvas2));

but your constructor is this:

RedListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2)

You probably want:

jrbRed.addActionListener(new RedListener(canvas2, canvas));

i.e. you reversed the order of parameters.

花辞树 2025-01-12 09:29:52

将构造函数参数

RedListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {...}

与您传递给它的参数

Lab4Panel canvas = new Lab4Panel();
Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();
...
jrbRed.addActionListener(new RedListener(canvas, canvas2));

进行比较绝对没有理由不能调试此问题。

Compare the constructor parameters

RedListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {...}

to the arguments that you're passing it

Lab4Panel canvas = new Lab4Panel();
Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();
...
jrbRed.addActionListener(new RedListener(canvas, canvas2));

There is absolutely no reason why you shouldn't have been able to debug this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文