Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的actionListener调用有什么问题_Java_Swing_Actionlistener_Paintcomponent - Fatal编程技术网

Java 我的actionListener调用有什么问题

Java 我的actionListener调用有什么问题,java,swing,actionlistener,paintcomponent,Java,Swing,Actionlistener,Paintcomponent,我得到一个错误:类RedListener中的构造函数RedListener不能应用于给定类型; addActionListener(新的RedListener(canvas,canvas2)) 我会给每个听众一个。这个程序应该是一个交通灯,当我点击单选按钮时,它会点亮。如果它没有被点击,那么它应该是用颜色勾勒出来的 import java.awt.event.*; import javax.swing.*; import java.awt.*; public class Lab4Frame

我得到一个错误:类RedListener中的构造函数RedListener不能应用于给定类型; addActionListener(新的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();
    }
}
导入java.awt.event.*;
导入javax.swing.*;
导入java.awt.*;
公共类Lab4Frame扩展了JFrame{
//公共布尔值为红色、黄色、绿色;
Lab4Frame(){
此.setLayout(新的BorderLayout());
setTitle(“实验4-应用程序1”);
Lab4Panel p=新的Lab4Panel();
Lab4RadioButtonPanel p2=新的Lab4RadioButtonPanel();
添加(p,BorderLayout.CENTER);
添加(p2,南部边界布局);
}
公共静态void main(字符串[]args){
Lab4Frame=新的Lab4Frame();
frame.setTitle(“Lab4应用程序#1”);
frame.setLocationRelativeTo(空);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(600600);
frame.setVisible(true);
}
}
类Lab4RadioButtonPanel扩展了JPanel{
Lab4Panel canvas=新的Lab4Panel();
Lab4RadioButtonPanel canvas2=新的Lab4RadioButtonPanel();
公共Lab4RadioButtonPanel(){
布尔红、绿、黄;
this.setLayout(新的FlowLayout());
JRadioButton jrreded=新的JRadioButton(“红色”,真);
JRadioButton jrbYellow=新的JRadioButton(“黄色”);
JRadioButton jrbGreen=新的JRadioButton(“绿色”);
此.setboorder(BorderFactory.createLineBorder(Color.black));
ButtonGroup=新建ButtonGroup();
添加组(jrreded);
group.add(jrbYellow);
添加(jrbGreen);
此.add(jrred);
this.add(jrbYellow);
添加(jrbGreen);
addActionListener(新的RedListener(canvas,canvas2));
addActionListener(新的YellowListener(canvas,canvas2));
jrbGreen.addActionListener(新的GreenListener(canvas,canvas2));
}
}
类Lab4Panel扩展了JPanel{
公共Lab4Panel(){}
布尔红、绿、黄;
int半径=5;
int x=-1;
int y=-1;
公共无效设置(){
红色=真;
重新油漆();
}
受保护组件(图形g){
如果(x你有:

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));

i、 e.您颠倒了参数的顺序。

比较构造函数参数

RedListener(Lab4RadioButtonPanel canvas, Lab4Panel canvas2) {...}
对于你正在传递它的论点

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

您完全没有理由不能够调试此问题。

谢谢您的帮助,但我仍然是一名学生,正在学习这方面的知识。我已经查看textpad编译器20分钟,试图找出我的错误。@user512915,熟悉一个更复杂、更专业的IDE(例如Eclipse、NetBeans等)。这些编译器会把20分钟的无意义调试缩短到几纳秒。。]我们不允许在课堂上使用IDE。我忘了把它放在最上面。对不起。@user512915,你选择的变量名让你感到困惑。但更重要的是:长得更厚。@user512915,你上的是哪种落后的大学?