动态温度显示——Java Swing 应用案例
在文章《温度显示界面——Java Swing 应用简介》中,我们通过温度显示界面的应用来演示Java Swing 各个部分的用法。本节我们在此基础之上,编写动态温度显示界面,代码如下:
package damuchacha.window;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
/*
* 绘制标签类
*/
public class textLabel extends JLabel {
private String str; //字符串
private Color color; //标签颜色
private int fontSize; //字体大小
public textLabel(){
}
public textLabel(String s, Color c, int f){
this.str = s;
this.color = c;
this.fontSize = f;
setText(str);
setFont(new Font(null,Font.PLAIN,fontSize));
setBackground(Color.BLACK);
setForeground(color);
}
}
package damuchacha.window;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.text.DateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class WindowMenu extends JFrame {
private static final int frameWidth = 1000; //frame宽度 常量
private static final int frameHeight = 1000; //frame高度 常量
private static final int MAX_SAMPLES = 1000; //温度曲线长度
private int time = 0;
private int[] highTemp = new int[MAX_SAMPLES]; //存储最高温度数值
private int[] lowTemp = new int[MAX_SAMPLES]; //存储最低温度数值
private static JLabel highTempValue; //显示温度数值
private static JLabel lowTempValue;
private textLabel tempName1; //显示温度名称
private textLabel tempUnit1; //显示温度单位
private textLabel tempName2;
private textLabel tempUnit2;
private JPanel highTempValuePanel; //最高气温Panel
private JPanel lowTempValuePanel; //最低气温Panel
private JPanel tempValuePanel; //温度Panel
private JPanel curveTempPanel; //温度曲线Panel
public WindowMenu() {
initComponents();
}
public void addData(int hT,int lT) {
time = time + 1;
highTemp[time] = hT; //添加最高温度
lowTemp[time] = lT; //添加最低温度
if(time>9){ //如果温度超过了七天,则将第一天的温度覆盖
for (int i = 0;i<time;i++){
highTemp[i] = highTemp[i+1];
lowTemp[i] = lowTemp[i+1];
}
}
repaint();
}
public void paint(Graphics g) {
super.paint(g);
int left = curveTempPanel.getX() + 30;
int top = curveTempPanel.getY() + 30;
int right = left + curveTempPanel.getWidth() - 30;
int bottom = top + curveTempPanel.getHeight() - 30;
int y0 = bottom - 20;
int yn = top;
int x0 = left + 33;
int xn = right;
int yScale = (int) ((yn - y0) /40.0); // y轴比例尺,最大数值 40
int xScale = (int) (xn - x0)/10; // x轴比例尺,最大数值10
/*
* 绘制x,y坐标轴
*/
g.setColor(Color.BLACK); //坐标轴颜色
g.drawLine(x0, yn, x0, y0); //y坐标轴
g.drawLine(x0, y0, xn, y0); //x坐标轴
/*
* 绘制x坐标轴,刻度,数值
*/
for (int xt = x0 + xScale; xt < xn; xt += xScale) {
g.drawLine(xt, y0 + 1, xt, y0 - 1);
int min = (xt - x0) / (xScale);
g.drawString(Integer.toString(min), xt - (min < 10 ? 3 : 7) , y0 + 20);
}
/*
* 绘制y坐标轴,刻度,数值
*/
for (int vt = 40; vt > 0; vt -= 5) { // tick every 200
int v = y0 + (int)(vt * yScale);
g.drawLine(x0 - 1, v, x0 + 1, v); //y坐标轴刻度
g.drawString(Integer.toString(vt), x0 - 38 , v + 5); //y坐标轴数值
}
/*
* 绘制温度变化曲线
*
*/
int xp = -1;
int yp1 = -1;
int yp2 = -1;
if(time>9){
time = 9;
}
for (int i = 0; i < time; i++) {
int x = x0 + (int)(i * xScale);
int v1 = y0 + (int)(highTemp[i] * yScale);
int v2 = y0 + (int)(lowTemp[i] * yScale);
if (xp > 0) {
g.setColor(Color.RED);
g.drawLine(xp, yp1, x, v1);
g.setColor(Color.BLUE);
g.drawLine(xp, yp2, x, v2);
}
xp = x;
yp1 = v1;
yp2 = v2;
}
}
private void initComponents() {
this.setTitle("温度显示界面");
Toolkit kit = Toolkit.getDefaultToolkit(); //定义工具包
Dimension screenSize = kit.getScreenSize(); //获取屏幕的尺寸
int screenWidth = screenSize.width; //获取屏幕的宽
int screenHeight = screenSize.height; //获取屏幕的高
this.setLocation(screenWidth/2-frameWidth/2, screenHeight/2-frameHeight/2);//设置窗口居中显示
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(this.frameWidth,this.frameHeight);
this.setVisible(true);
/*
* Panel1 格式设置
*/
tempName1 = new textLabel(" 当天最高温度",Color.RED,40);
tempUnit1 = new textLabel(" °C",Color.RED,40);
highTempValue = new JLabel();
highTempValue.setFont(new Font(null,Font.PLAIN,50));
highTempValue.setForeground(Color.RED);
highTempValuePanel = new JPanel();
highTempValuePanel.setLayout(new GridLayout(1,3,1,1));
highTempValuePanel.add(tempName1);
highTempValuePanel.add(highTempValue);
highTempValuePanel.add(tempUnit1);
/*
* Panel2 格式设置
*/
tempName2 = new textLabel(" 当天最低温度",Color.BLUE,40);
tempUnit2 = new textLabel(" °C",Color.BLUE,40);
lowTempValue = new JLabel();
lowTempValue.setFont(new Font(null,Font.PLAIN,50));
lowTempValue.setForeground(Color.BLUE);
lowTempValuePanel = new JPanel();
lowTempValuePanel.setLayout(new GridLayout(1,2,1,1));
lowTempValuePanel.add(tempName2);
lowTempValuePanel.add(lowTempValue);
lowTempValuePanel.add(tempUnit2);
/*
* Panel3格式设置
* 将Panel1和Panel2放在一起构成Panel3
*/
tempValuePanel = new JPanel();
tempValuePanel.setLayout(new GridLayout(2,1,1,1));
tempValuePanel.add(highTempValuePanel);
tempValuePanel.add(lowTempValuePanel);
/*
* 温度曲线
*/
curveTempPanel = new JPanel();
curveTempPanel.setMinimumSize(new Dimension(400, 250));
curveTempPanel.setPreferredSize(new Dimension(400, 250));
this.setLayout(new GridLayout(0,1,5,5));
this.add(tempValuePanel);
this.add(curveTempPanel);
pack();
}
public static void main (String args[]) {
WindowMenu dw = new WindowMenu();
EventQueue.invokeLater(new Runnable() {
public void run() {
dw.setVisible(true);
}
}
);
Thread thread = new Thread() {
public void run() {
while (true) {
int hT = new Random().nextInt(40); //获得实时最高温度
int lT = new Random().nextInt(40); //获得实时最低温度
highTempValue.setText(" "+hT);
lowTempValue.setText(" "+lT);
dw.addData(hT,lT); // 将温度数值添加到画板上
dw.repaint();
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.run();
}
}
动态显示结果如下:
完
2018年10月2日