我得到一個錯誤:構造函數類RedListener中的RedListener不能應用於給定的類型; jrbRed.addActionListener(new RedListener(canvas,canvas2));我的actionListener調用有什麼問題
我收到每個聽衆一個。該程序應該是一個紅綠燈,當我點擊一個點亮「點亮」的單選按鈕時。如果沒有,那麼點擊它只是應該在顏色被概括
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();
}
}
謝謝你的幫助,但我仍然是一個學生學習這個東西的學生。我一直在尋找20分鐘的textpad編譯器試圖找出我做錯了什麼。 – Robert 2012-02-14 02:29:52
@ user512915,熟悉更復雜的專業IDE(例如Eclipse,NetBeans等)。這些編譯器將這些20分鐘的無意義調試減少到幾個納秒。 ;] – mre 2012-02-14 02:31:27
我們不允許在課堂上使用IDE。我忘了把它放在頂部抱歉。 – Robert 2012-02-14 02:42:47