我的actionListener调用有什么问题

时间:2012-02-14 02:16:10

标签: java swing actionlistener paintcomponent

我收到错误:类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();
    }
}

2 个答案:

答案 0 :(得分:3)

你有这个:

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

即。你颠倒了参数的顺序。

答案 1 :(得分:2)

比较构造函数参数

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

你传递的论据

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

绝对没有理由不能调试此问题。

相关问题