| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 921 人关注过本帖
标题:一个小问题
只看楼主 加入收藏
风月_无边
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:60
帖 子:2960
专家分:652
注 册:2007-1-19
收藏
得分: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);
}
}

}

}



我的网站 http://www.
2007-01-28 12:03
快速回复:一个小问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016672 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved