java --> GUI 红绿灯
`
package com.liu.lesson01;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Path;
public class WarningLight extends JFrame {
boolean flag = true;
public WarningLight() {
/**
* 绘制界面
/
this.setLayout(null);
JLabel jl = new JLabel();//实例化界面对象
setTitle("交通信号灯程序");//设置标题
this.setBounds(515, 98, 836, 908);//设置窗口宽为836像素,高为908像素
this.add(jl);//将其加入到窗口当中
setResizable(false);// 设置窗体是否可以调整大小,参数为布尔值
setDefaultCloseOperation(EXIT_ON_CLOSE);// 用户点击窗口关闭
/*
* 设置开始按钮
*/
JButton begin = new JButton("点击开始");//实例化开始按钮
begin.setBounds(610, 370, 140, 50);
begin.setFont(new java.awt.Font("Dialog", 1, 15));//设置字体样式
begin.setForeground(Color.white);//设置字体颜色
begin.setBackground(new Color(8, 163, 219));//设置按钮颜色
/**
* 警示灯切换
/
JLabel lb = new JLabel();//实例化对象
lb.setBounds(200, 50, 568, 798);//设置宽高
jl.add(lb);//添加到jn里
this.add(lb);//添加当前的lb
/*
* 文字提示
*/
JLabel ts = new JLabel("交通信号提示");
ts.setBounds(586, 280, 200, 30);
ts.setFont(new java.awt.Font("黑体", 1, 30));//设置字体样式
add(ts);
//监听器,事件处理
begin.addActionListener(new ActionListener() {
@Override//覆写方法
public void actionPerformed(ActionEvent e) {
//匿名实例化线程
new Thread(new Runnable() {
@Override//覆写run方法
public void run() {
//循环标记
while (flag) {
ts.setForeground(Color.white);//设置字体颜色
lb.setIcon(new ImageIcon("C:\Users\86134\Pictures\Camera Roll\hong.png"));//设置显示红灯png
lb.setSize(368, 698);//设置宽高
//area.setBackground(Color.RED);
try {
ts.setText("红灯/停一停");
Thread.sleep(5200);//设置睡眠
} catch (InterruptedException e) {
e.printStackTrace();
}
lb.setIcon(new ImageIcon("C:\Users\86134\Pictures\Camera Roll\huang.png"));//设置显示黄灯png
lb.setSize(368, 698);//设置宽高
ts.setText("黄灯/等一等");
//area.setBackground(Color.YELLOW);
try {
Thread.sleep(2000);//设置睡眠
} catch (InterruptedException e) {
e.printStackTrace();
}
lb.setIcon(new ImageIcon("C:\Users\86134\Pictures\Camera Roll\lv.png"));//设置显示绿灯png
lb.setSize(368, 698);//设置宽高
ts.setText("绿灯/可通行");
// area.setBackground(Color.GREEN);
try {
Thread.sleep(5000);//设置睡眠
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();//开启线程
}
});
this.add(begin);//添加当前按钮
/**
* 设置结束按钮
/
JButton end = new JButton("点击结束");//实例化结束按钮
end.setBounds(610, 432, 140, 50);
end.setFont(new java.awt.Font("Dialog", 1, 15));//设置字体样式
end.setForeground(Color.white);//设置字体颜色
end.setBackground(new Color(8, 163, 219));//设置按钮颜色
//事件处理
end.addActionListener(new ActionListener() {
@Override//覆写方法
public void actionPerformed(ActionEvent e) {
flag = false;//结束进程
}
});
this.add(end);//添加当前结束按钮
/*
* 设置窗口图标
/
Toolkit kit = Toolkit.getDefaultToolkit(); //创建窗口图标对象
Image image = kit.getImage("C:\Users\86134\Pictures\Camera Roll\xinhen\22.png"); //设置窗口图标路径
setIconImage(image); //换掉窗体样式
/*
* 设置背景图片
*
*/
setSize(839, 908);//设置大小
String path = "C:\Users\86134\Pictures\Camera Roll\timg.jpg";//设置背景图片的路径
ImageIcon background = new ImageIcon(path); // 背景图片
JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
label.setBounds(0, 0, this.getWidth(), this.getHeight()); // 把标签的大小位置设置为图片刚好填充整个面板
JPanel imagePanel = (JPanel) this.getContentPane(); // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗& 格透明
imagePanel.setOpaque(false);
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));// 把背景图片添加到分层窗格的最底层作为背景
this.setVisible(true);//设置是否窗口显示
}
public static void main(String[] args) {
new WarningLight();
}
}
`

浙公网安备 33010602011771号