| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 921 人关注过本帖
标题:一个小问题
取消只看楼主 加入收藏
wuzhong
Rank: 1
等 级:新手上路
帖 子:233
专家分:0
注 册:2006-10-23
收藏
 问题点数:0 回复次数:2 
一个小问题

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

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 
2007-01-27 10:08
wuzhong
Rank: 1
等 级:新手上路
帖 子:233
专家分:0
注 册:2006-10-23
收藏
得分:0 
没有编译错误,所以找不到.

2007-01-27 11:21
wuzhong
Rank: 1
等 级:新手上路
帖 子:233
专家分:0
注 册:2006-10-23
收藏
得分:0 

再次置顶,大家帮忙.


2007-01-27 16:02
快速回复:一个小问题
数据加载中...
 
   



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

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