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

一个小问题

热度:111   发布时间:2007-01-28 12:03:00.0

帮你改了一下,勉强可以实现了.

package p1;

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;
private boolean red=true;
private boolean yellow=false;
private boolean green=false;

panel p1;

public static void main(String args[]){
RadioButtonDemo frame = new RadioButtonDemo();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(250,170);
frame.setVisible(true);
}

public RadioButtonDemo(){ //构造函数
// this.setTitle("RadioButton demo");

p1 = new panel();
p1.setSize(200,200);
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
Thread t=new Thread(p1);
t.start();

// light=new Light();

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


JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jrbRed=new JRadioButton("Red",true));
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.setLayout(new BorderLayout());
this.add(p1,BorderLayout.CENTER);
this.add(p2,BorderLayout.SOUTH);

jrbRed.addItemListener(this); //为三个单选框加个监听
jrbYellow.addItemListener(this);
jrbGreen.addItemListener(this);

}

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 Dimension getpreferredSize(){
return new Dimension(40,90);
}

public void itemStateChanged(ItemEvent e){
if(jrbRed.isSelected()) turnonRed();
if(jrbYellow.isSelected()) turnonYellow();
if(jrbGreen.isSelected()) turnonGreen();
}
class panel extends JPanel implements Runnable
{

public void run()
{
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
System.out.println(red);
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(red){
System.out.println("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){
System.out.println("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){
System.out.println("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 {
System.out.println("black");
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);
}
}

}

}



----------------解决方案--------------------------------------------------------
  相关解决方案