📄 trrbuildwindow.java
字号:
package face;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import thread.RabbitThread;
import thread.TortoiseThread;
public class TrrBuildWindow extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 800;
private final int HEIGHT = 600;
private final int LEFTMARGIN = 150;
private final int TOPMARGIN = 100;
private final Font font = new Font(null, 3, 80);
private final Font font1 = new Font(null, 3, 40);
private final Font font2 = new Font(null, 3, 22);
@SuppressWarnings("unused")
private boolean isWin = false;
private JScrollBar tortoiseBar, rabbitBar;
private JButton preButton, runButton, exitButton;
private JPanel jPanelForButton, jPanelForBar, jPanelForPic;
private JTextField tortoiseSpeedT, rabbitSpeedT;
private JLabel tortoiseSpeedL, rabbitSpeedL, tortoiseLocation,
rabbitLocation, result, rabbitResult, tortoiseResult,ruleLabel;
void init() {
this.setSize(WIDTH, HEIGHT); // 设置窗口大小
this.setLocation(LEFTMARGIN, TOPMARGIN);
this.setTitle("Race between Tortoise and Rabbit");
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tortoiseSpeedT = new JTextField(5);
tortoiseSpeedT.setText("20");
tortoiseSpeedT.setBounds(600, 80, 50, 30);
rabbitSpeedT = new JTextField(5);
rabbitSpeedT.setText("25");
rabbitSpeedT.setBounds(600, 160, 50, 30);
tortoiseSpeedL = new JLabel("m/s Speed of tortoise");
tortoiseSpeedL.setBounds(650, 80, 200, 30);
rabbitSpeedL = new JLabel("m/s Speed of rabbit");
rabbitSpeedL.setBounds(650, 160, 200, 30);
ruleLabel=new JLabel("兔子超过乌龟十米时就要休息1秒");
ruleLabel.setBounds(50,100,400,20);
ruleLabel.setFont(font2);
tortoiseLocation = new JLabel("0 m Tortoise");
tortoiseLocation.setBounds(660, 50, 100, 30);
rabbitLocation = new JLabel("0 m Rabbit");
rabbitLocation.setBounds(660, 150, 100, 30);
result = new JLabel("RESULT");
result.setFont(font);
result.setForeground(Color.BLUE);
result.setBounds(0, 0, 500, 100);
tortoiseResult = new JLabel("?");
tortoiseResult.setFont(font1);
tortoiseResult.setBounds(0, 101, 500, 100);
rabbitResult = new JLabel("?");
rabbitResult.setFont(font1);
rabbitResult.setBounds(0, 201, 500, 100);
tortoiseBar = new JScrollBar(0, 0, 1, 0, 100);
tortoiseBar.setSize(600, 50);
tortoiseBar.setBounds(50, 50, 600, 20);
rabbitBar = new JScrollBar(0, 0, 1, 0, 100);
rabbitBar.setSize(600, 50);
rabbitBar.setBounds(50, 150, 600, 20);
preButton = new JButton("Preparation");
preButton.setEnabled(false);
preButton.addActionListener(this);
runButton = new JButton("Run");
runButton.addActionListener(this);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
jPanelForPic = new JPanel();
jPanelForPic.setBackground(Color.YELLOW);
jPanelForPic.setLayout(null);
jPanelForPic.add(tortoiseSpeedT);
jPanelForPic.add(rabbitSpeedT);
jPanelForPic.add(tortoiseSpeedL);
jPanelForPic.add(rabbitSpeedL);
jPanelForPic.add(result);
jPanelForPic.add(tortoiseResult);
jPanelForPic.add(rabbitResult);
jPanelForPic.setBounds(0, 0, 800, 300);
jPanelForBar = new JPanel();
jPanelForBar.setLayout(null);
jPanelForBar.setBounds(0, 301, 800, 220);
jPanelForBar.add(tortoiseBar);
jPanelForBar.add(ruleLabel);
jPanelForBar.add(rabbitBar);
jPanelForBar.add(tortoiseLocation);
jPanelForBar.add(rabbitLocation);
jPanelForBar.setBackground(Color.CYAN);
jPanelForBar.setLayout(null);
jPanelForButton = new JPanel();
jPanelForButton.setBackground(Color.GRAY);
jPanelForButton.setBounds(0, 521, 800, 80);
jPanelForButton.add(preButton, "West");
jPanelForButton.add(runButton, "Center");
jPanelForButton.add(exitButton, "East");
this.add(jPanelForPic, "North");
this.add(jPanelForBar, "Center");
this.add(jPanelForButton, "South");
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == preButton) {
runButton.setEnabled(true);
preButton.setEnabled(false);
isWin = false;
tortoiseLocation.setText("0 m Tortoise");
rabbitLocation.setText("0 m Rabbit");
tortoiseBar.setValue(0);
rabbitBar.setValue(0);
}
if (e.getSource() == runButton) {
// JOptionPane.showMessageDialog(null,"Run");
//preButton.setEnabled(true);
runButton.setEnabled(false);
new TortoiseThread().start();
new RabbitThread().start();
}
if (e.getSource() == exitButton) {
// JOptionPane.showMessageDialog(null,"ByeBye!");
System.exit(0);
}
}
private static TrrBuildWindow tbw;
static {
tbw = new TrrBuildWindow();
}
public static TrrBuildWindow getTrrBuildWindow() {
return tbw;
}
public JScrollBar getRabbitBar() {
return rabbitBar;
}
public JScrollBar getTortoiseBar() {
return tortoiseBar;
}
public JLabel getRabbitLocation() {
return rabbitLocation;
}
public JLabel getTortoiseLocation() {
return tortoiseLocation;
}
public JTextField getRabbitSpeedT() {
return rabbitSpeedT;
}
public JTextField getTortoiseSpeedT() {
return tortoiseSpeedT;
}
public boolean isWin() {
return isWin;
}
public void setWin(boolean isWin) {
this.isWin = isWin;
}
public JLabel getRabbitResult() {
return rabbitResult;
}
public JLabel getTortoiseResult() {
return tortoiseResult;
}
public JButton getPreButton() {
return preButton;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -