帮我编写一个基于Java GUI 的长方体液面槽内液位变化可视化显示界面系统设计的代码。 所需具备功能: 1.在界面右侧上方要显示三个值包括液面最小值,最大值和当前值,设定液面最小值为20,液面最高值90,设定液面槽内液面20-90为正常范围,超过为异常。 2.设定液面当前值的初始值为50,做个循环嵌套:液面当前值从初始值50开始液面值先降低,每秒降低6,当降到或低于20时液面开始增加,液面值每秒升高6;液面值升高到达或高于90时液面停止增加,开始降低液面,液面也是每秒降低6,如此循环。 3.设置2个按钮 例如(开始监控)(停止监控),按(开始监控)就开始判断液位变化,进行第3步中的循环,按(停止监控),就停止进行判断。 界面左侧画一个长方体液面槽,动画显示长方体液面槽液面动态变化。液面槽高设定为100。槽内液面模拟实现以上循环嵌套中液面的动态变化
时间: 2023-05-18 12:07:27 浏览: 129
期末大作业基于Java的GUI界面开发的可视化学生管理系统源码.zip
以下是基于Java GUI的长方体液面槽内液位变化可视化显示界面系统设计的代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LiquidLevelMonitor extends JFrame implements ActionListener {
private JLabel minLabel, maxLabel, currentLabel;
private JTextField minField, maxField, currentField;
private JButton startButton, stopButton;
private JPanel controlPanel, liquidPanel;
private int currentLevel = 50;
private boolean isMonitoring = false;
public LiquidLevelMonitor() {
setTitle("Liquid Level Monitor");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Control panel
minLabel = new JLabel("Min Level:");
maxLabel = new JLabel("Max Level:");
currentLabel = new JLabel("Current Level:");
minField = new JTextField("20", 5);
maxField = new JTextField("90", 5);
currentField = new JTextField("50", 5);
currentField.setEditable(false);
startButton = new JButton("Start Monitoring");
startButton.addActionListener(this);
stopButton = new JButton("Stop Monitoring");
stopButton.addActionListener(this);
stopButton.setEnabled(false);
controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(4, 2));
controlPanel.add(minLabel);
controlPanel.add(minField);
controlPanel.add(maxLabel);
controlPanel.add(maxField);
controlPanel.add(currentLabel);
controlPanel.add(currentField);
controlPanel.add(startButton);
controlPanel.add(stopButton);
// Liquid panel
liquidPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int height = getHeight();
int width = getWidth();
int x = 50;
int y = height - 100 - (currentLevel - 20) * 100 / 70;
int w = width - 100;
int h = 100;
g.drawRect(x, y, w, h);
g.setColor(Color.BLUE);
g.fillRect(x + 1, y + 1, w - 1, h - 1);
}
};
// Add panels to frame
setLayout(new BorderLayout());
add(controlPanel, BorderLayout.NORTH);
add(liquidPanel, BorderLayout.CENTER);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
isMonitoring = true;
startButton.setEnabled(false);
stopButton.setEnabled(true);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (isMonitoring) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
if (currentLevel <= 20) {
currentLevel = 20;
liquidPanel.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} else if (currentLevel >= 90) {
currentLevel = 90;
liquidPanel.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} else {
currentLevel -= 6;
liquidPanel.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
if (currentLevel <= 20) {
currentLevel = 20;
liquidPanel.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} else if (currentLevel >= 90) {
currentLevel = 90;
liquidPanel.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} else {
currentLevel += 6;
liquidPanel.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}
});
t.start();
} else if (e.getSource() == stopButton) {
isMonitoring = false;
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
public static void main(String[] args) {
new LiquidLevelMonitor();
}
}
```
该程序实现了一个基于Java GUI的长方体液面槽内液位变化可视化显示界面系统,具备以下功能:
1.在界面右侧上方显示三个值包括液面最小值,最大值和当前值,设定液面最小值为20,液面最高值90,设定液面槽内液面20-90为正常范围,超过为异常。
2.设定液面当前值的初始值为50,做个循环嵌套:液面当前值从初始值50开始液面值先降低,每秒降低6,当降到或低于20时液面开始增加,液面值每秒升高6;液面值升高到达或高于90时液面停止增加,开始降低液面,液面也是每秒降低6,如此循环。
3.设置2个按钮 例如(开始监控)(停止监控),按(开始监控)就开始判断液位变化,进行第3步中的循环,按(停止监控),就停止进行判断。
界面左侧画一个长方体液面槽,动画显示长方体液面槽液面动态变化。液面槽高设定为100。槽内液面模拟实现以上循环嵌套中液面的动态变化。
阅读全文