当前位置: 代码迷 >> Java相关 >> 一个小问题
  详细解决方案

一个小问题

热度:112   发布时间:2007-01-27 10:08:00.0
一个小问题

实在找不到哪儿错了.大家帮帮忙了.

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

public class RadioButtonDemo extends JFrame implements ItemListener {
private JRadioButton jrbRed,jrbYellow,jrbGreen;
private ButtonGroup btg=new ButtonGroup();
private Light light;

public static void main(String args[]){
RadioButtonDemo frame = new RadioButtonDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
frame.setSize(250,170);
frame.setVisible(true);
}

public RadioButtonDemo(){
this.setTitle("RadioButton demo");

JPanel p1 = new JPanel();
p1.setSize(200,200);
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
light=new Light();

light.setSize(40,90);
p1.add(light);


JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jrbRed=new JRadioButton("Red",false));
p2.add(jrbYellow=new JRadioButton("Yellow",false));
p2.add(jrbGreen=new JRadioButton("Green",false));

jrbRed.setMnemonic('R');
jrbYellow.setMnemonic('Y');
jrbGreen.setMnemonic('G');

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

this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(p1,BorderLayout.CENTER);
this.getContentPane().add(p2,BorderLayout.SOUTH);

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

}


public void itemStateChanged(ItemEvent e){
if(jrbRed.isSelected()) light.turnonRed();
if(jrbYellow.isSelected()) light.turnonYellow();
if(jrbGreen.isSelected()) light.turnonGreen();
}

}

class Light extends JPanel{

private boolean red;
private boolean yellow;
private boolean green;

public Light(){
turnonGreen();
}

public void turnonRed(){
red=true;
yellow=false;
green=false;
repaint();

}

public void turnonYellow(){
red=false;
yellow=true;
green=false;
repaint();
}

public void turnonGreen(){
red=false;
yellow=false;
green=true;
repaint();
}

public void paintComponent(Graphics g){
super.paintComponent(g);
if(red){
g.setColor(Color.red);
g.fillOval(10, 10, 20, 20);
g.setColor(Color.black);
g.drawOval(10, 35, 20, 20);
g.drawOval(10, 60, 20, 20);
g.drawRect(5, 5, 30, 80);
}
else if(yellow){
g.setColor(Color.yellow);
g.fillOval(10, 35, 20, 20);
g.setColor(Color.black);
g.drawRect(5, 5, 30, 80);
g.drawOval(10, 10,20,20);
g.drawOval(10, 60, 20, 20);
}
else if(green){
g.setColor(Color.green);
g.fillOval(10, 60, 20, 20);
g.setColor(Color.black);
g.drawRect(5, 5, 30, 80);
g.drawOval(10, 10, 20, 20);
g.drawOval(10, 35, 20, 20);
}

else {
g.setColor(Color.black);
g.drawRect(5, 5, 30, 80);
g.drawOval(10, 10, 20, 20);
g.drawOval(10, 35, 20, 20);
g.drawOval(10, 60, 20, 20);
}
}

public Dimension getpreferredSize(){
return new Dimension(40,90);
}

}

搜索更多相关的解决方案: import  awt  java  CLOS  private  

----------------解决方案--------------------------------------------------------

是编译错误么?
有错误信息么?


----------------解决方案--------------------------------------------------------
没有编译错误,所以找不到.
----------------解决方案--------------------------------------------------------
那就是你程序逻辑错误呗
那只有自己找了
----------------解决方案--------------------------------------------------------
呵...
----------------解决方案--------------------------------------------------------

再次置顶,大家帮忙.


----------------解决方案--------------------------------------------------------
应该没什么大问题,是你画的问题,色彩,填充模式等.稍微改下就好!
----------------解决方案--------------------------------------------------------
repaint();
调用的是paint方法,而你写的一大段paintComponent怎么会被调用呢?
----------------解决方案--------------------------------------------------------

你都不说你想实现什么,这个程序会有什么异常,叫我们如何帮你排错?


----------------解决方案--------------------------------------------------------
你是不是想根据几个单选框来改变灯的颜色,好像要用线程吧
----------------解决方案--------------------------------------------------------
  相关解决方案