主方法类
package com.company;
public class Main {
public static void main(String[] args) {
new Frame();
}
}
Frame类
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.util.Timer;
public class Frameextends JFrameimplements EventListener, ActionListener {
private JFrameframe;
private JPanelpanel1;
private JPanelpanel2;
private JPanelpanel3;
private JPanelpanel31,panel33;
private JPanelpanel311,panel312;
private JLabeljLabel,jLabe2;
public static JTextAreaarea,you,area1,tishi1,tishi2;
private JButtonjiayou,qidong,zhidong;
private JRadioButtonrb1,rb2,rb3,rb4;
private JToggleButtonbarke;
private JSliders2;
// private MytomaticCar car;
private int s;
public Frame(){
// JFrame frame = new JFrame();frame.setBackground(Color.blue);
setTitle("自动汽车模拟系统");
setLocation(300,200);
setSize(500,500);
this.panel1=new JPanel(new CardLayout());
add(this.panel1,BorderLayout.NORTH);
this.panel1.setBorder(BorderFactory.createTitledBorder("汽车状态显示"));
this.panel2=new JPanel();
this.panel2.setBorder(BorderFactory.createTitledBorder("数据显示"));
add(this.panel2,BorderLayout.CENTER);
this.jLabel=new JLabel("油量(L):");
this.area =new JTextArea("500",1, 7);
this.area.setFont(new Font("华文行楷",Font.BOLD,20));
this.area.setEditable(false);
area.setBackground(Color.yellow);
panel2.add(jLabel);
panel2.add(area);
this.jiayou=new JButton("加油");
this.jiayou.addActionListener(this);
this.you =new JTextArea("一键加满",1, 7);
this.you.setFont(new Font("华文行楷",Font.LAYOUT_LEFT_TO_RIGHT,18));
this.you.setEditable(false);
you.setBackground(Color.LIGHT_GRAY);
panel2.add(this.jiayou);
panel2.add(you);
this.jLabe2=new JLabel("位移(KM):");
panel2.add(jLabe2);
this.area1 =new JTextArea("0",1, 7);
this.area1.setFont(new Font("华文行楷",Font.BOLD,20));
this.area1.setEditable(false);
area1.setBackground(Color.yellow);
panel2.add(area1);
this.panel3 =new JPanel();
this.panel3.setBorder(BorderFactory.createTitledBorder("汽车驾驶室"));
add(this.panel3,BorderLayout.SOUTH);
panel3.setLayout(new BoxLayout(panel3,BoxLayout.Y_AXIS));
this.panel311=new JPanel();
this.panel3.add(panel311,panel3);
this.panel31=new JPanel();
panel31.setLayout(new BoxLayout(panel31,BoxLayout.Y_AXIS));
this.panel311.add(panel31);
this.qidong=new JButton("启动器");
this.qidong.addActionListener(this);
this.panel31.add(qidong,panel31);
this.panel31.add(new JLabel(" "));
this.zhidong=new JButton("刹车器");
this.zhidong.addActionListener(this);
this.panel31.add(zhidong,panel31);
this.panel311.add(new JLabel(" "));
JPanel panel32=new JPanel();
panel32.setBorder(BorderFactory.createTitledBorder("档位控制器"));
panel32.setLayout(new GridLayout(2,3));
JPanel m33=new JPanel();
m33.setLayout(new GridLayout(1,16));
this.rb1 =new JRadioButton("前进挡");
this.rb2 =new JRadioButton("后退档");
ButtonGroup grp1 =new ButtonGroup();//单选按钮
panel32.add(new JLabel(" "));
grp1.add(this.rb1); grp1.add(this.rb2);
m33.add(this.rb1);m33.add(new JLabel(""));m33.add(this.rb2);
panel32.add(m33);
this.panel311.add(panel32);
JPanel m4 =new JPanel();
m4.setBorder(BorderFactory.createTitledBorder("油门控制器"));
m4.setLayout(new GridLayout(1, 1));
this.s2 =new JSlider(0, 200, 50);
this.s2.setForeground(Color.ORANGE);
this.s2.setMajorTickSpacing(50);
this.s2.setMinorTickSpacing(10);
this.s2.setPaintTicks(true); //刻度线的绘画
this.s2.setPaintLabels(true); //标签的绘画
// this.s2.setPaintTrack(true);
// this.s2.setSnapToTicks(true);
m4.add(s2);
this.panel3.add(m4,panel3);
this.tishi1=new JTextArea("模拟自动挡汽车操作说明");
this.tishi2=new JTextArea("1.当油门为0时启动后油量和位移不会发生变化;\n" +
"2.未选择档位油门变化,位移不变。前进挡位移增加\n" +
" 后退档位移减小;\n" +
"3.点击加油一件加满500油,汽车启动中不能加油;\n" +
"4.点击刹车,汽车停止启动,油门变为0。");
this.tishi1.setFont(new Font("华文行楷",Font.BOLD,20));
this.tishi2.setFont(new Font("宋体",Font.BOLD, 16));
this.panel3.add(tishi1,panel3);
this.panel3.add(tishi2,panel3);
pack();
setVisible(true);
setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String name =qidong.getText();
String name1 =zhidong.getText();
if(e.getSource()==this.jiayou){
if(name.equals("启动器")){
JOptionPane.showMessageDialog(this,"油以加满");
area.setText("500");}
else {
JOptionPane.showMessageDialog(this,"启动中加油危险,请熄火");
}
}else if(e.getSource()==this.qidong){
if(name.equals("启动器")) {
qidong.setText("已启动");
// while(true) {
fanhui(e);
// }
}/*else {
qidong.setText("启动器");
}
*/
}else if(e.getSource()==this.zhidong){
if(name1.equals("刹车器")) {
JOptionPane.showMessageDialog(this,"已刹车");
// zhidong.setText("以刹车");
qidong.setText("启动器");
this.s2.setValue(0);
}
// else{
//// zhidong.setText("刹车器");
//// }
}
}
public void fanhui(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
while (true){
String sss =area.getText();
int n = Integer.parseInt(sss);
int youmen =s2.getValue();
String ss =area1.getText();
int m = Integer.parseInt(ss);
if (0 < youmen && youmen <=50) {
if (n >=0) {
n -=1;
}
if (rb1.isSelected()) {
m +=1;
}else if (rb2.isSelected()) {
m -=1;
}
}else if (50 < youmen && youmen <=100) {
if (n >=0) {
n -=2;
}
if (rb1.isSelected()) {
m +=2;
}else if (rb2.isSelected()) {
m -=2;
}
}else if (100 < youmen && youmen <=150) {
if (n >=0) {
n -=3;
}
if (rb1.isSelected()) {
m +=3;
}else if (rb2.isSelected()) {
m -=3;
}
}else if (150 < youmen && youmen <=200) {
if (n >=0) {
n -=4;
}
if (rb1.isSelected()) {
m +=4;
}else if (rb2.isSelected()) {
m -=4;
}
}
area.setText(""+n);
area1.setText("" + m);
try {
Thread.sleep(800);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start(); //
}
}