标签:style io color ar os java sp for on
GUI Graphics User Interrface图形化的用户界面,让用户通过点击鼠标就能完成想要做的事情;
主要是练习界面设置 界面布局 添加组件 设置组件 接口 重写 监听器
制作一个信号灯,点击下面的单选按钮 指定的信号灯亮起,代码如下:
package com.lovo.homework1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
/**
* 类:红绿灯
* @author Abe
* 点击按钮改变颜色
*/
@SuppressWarnings("serial")
public class TrafficLight extends JFrame implements ItemListener{
private JRadioButton[] lights = new JRadioButton[3];
private String[] lightColor = {"红灯","黄灯","绿灯"};
private Color[] colors = {Color.red,Color.yellow,Color.green};
private int n = 0;
/**
* 构造器
*/
public TrafficLight(){
//窗口构造
this.setSize(200, 400);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
//按钮组构造
ButtonGroup group = new ButtonGroup();
for(int i = 0 ; i < lights.length; i++){
lights[i] = new JRadioButton(lightColor[i],true);
group.add(lights[i]);
this.add(lights[i]);
lights[i].addItemListener(this);
lights[i].setBounds(10 + 60 * i, 280, 60, 100);
}
}
/**
* 重写:paint回调方法
*/
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(colors[n]);
g.fillOval(70, 80 + n * 70, 60, 60);
g.setColor(Color.BLACK);
g.drawRect(45, 60, 110, 240);
for(int i = 0; i < 3;i++){
g.drawOval(70, 80 + i * 70, 60, 60);
}
}
/**
* main方法:设置窗口可见
*/
public static void main(String[] args) {
new TrafficLight().setVisible(true);
}
/**
* 方法:监听按钮改变
*/
@Override
public void itemStateChanged(ItemEvent e) {
Object obj = e.getSource();
if(obj == lights[0]){
n = 0;
}else if(obj == lights[1]){
n = 1;
}else if(obj == lights[2]){
n = 2;
}
repaint();
}
}
JAVA程序设计(15.1)----- 图形用户界面GUI 制作红绿灯~
标签:style io color ar os java sp for on
原文地址:http://blog.csdn.net/anubies/article/details/40909389