📄 personbuildwindow.java
字号:
package face;
import java.awt.Color;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import thread.PersonThread;
public class PersonBuildWindow extends JFrame {
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 int jPanelForBarHEIGHT = 500;
private final int jPanelForButtonHEIGHT = HEIGHT - jPanelForBarHEIGHT;
private final int barStartLocation = 50;
private final int barLength = 500;
private final int heightofBarAndLabel = 20;
private final int spaceBetweenBarAndLabel = 20;
private final int labelLength = 200;
private JScrollBar[] bar;
private JLabel[] label;
private PersonThread[] person;
private JButton signButton, preButton, runButton, exitButton;
private JPanel jPanelForButton, jPanelForBar;
private JTextField count;
private int countNum, spaceBetweenBars;
private static int order=0;
void init() {
this.setSize(WIDTH, HEIGHT); // 设置窗口大小
this.setLocation(LEFTMARGIN, TOPMARGIN);
this.setTitle("Race between Persons");
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
count = new JTextField(10);
count.setText("赛跑人数(2~25)");
count.setSelectionStart(0);
count.setSelectionEnd(count.getText().length());
signButton = new JButton("报名");
signButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
countNum = Integer.parseInt(count.getText());
if (countNum < 2) {
JOptionPane.showConfirmDialog(null, "两个人以上才能赛跑啊", "警告",
2);
} else if (countNum > 25) {
JOptionPane.showConfirmDialog(null, "人数太多,请减少人数", "警告",
2);
} else {
preButton.setEnabled(true);
count.setEnabled(false);
signButton.setEnabled(false);
}
} catch (NumberFormatException e1) {
JOptionPane.showConfirmDialog(null, "必须输入数字", "警告", 2);
}
}
});
preButton = new JButton("预备");
preButton.setEnabled(false);
preButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bar = new JScrollBar[countNum];
label = new JLabel[countNum];
person=new PersonThread[countNum];
spaceBetweenBars = (jPanelForBarHEIGHT - heightofBarAndLabel
* countNum)
/ countNum;
drawIn();
runButton.setEnabled(true);
preButton.setEnabled(false);
}
});
runButton = new JButton("砰~~跑");
runButton.setEnabled(false);
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i=0;i<countNum;i++){
person[i].start();
}
runButton.setEnabled(false);
}
});
exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jPanelForBar = new JPanel();
jPanelForBar.setBounds(0, 0, WIDTH, jPanelForBarHEIGHT);
jPanelForBar.setBackground(Color.CYAN);
jPanelForBar.setLayout(null);
jPanelForButton = new JPanel();
jPanelForButton.setBackground(Color.GRAY);
jPanelForButton.setBounds(0, jPanelForBarHEIGHT + 1, WIDTH,
jPanelForButtonHEIGHT);
jPanelForButton.add(count);
jPanelForButton.add(signButton);
jPanelForButton.add(preButton);
jPanelForButton.add(runButton);
jPanelForButton.add(exitButton);
this.add(jPanelForBar, "Center");
this.add(jPanelForButton, "South");
this.setVisible(true);
}
public void drawIn() {
for (int i = 0; i < countNum; i++) {
bar[i] = new JScrollBar(0, 0, 1, 0, 100);
bar[i]
.setBounds(
barStartLocation,
(int) ((spaceBetweenBars + heightofBarAndLabel) * i + spaceBetweenBars * 0.5),
barLength, heightofBarAndLabel);
label[i] = new JLabel("Person" + (i + 1) + "等待结果");
label[i]
.setBounds(
barStartLocation + barLength
+ spaceBetweenBarAndLabel,
(int) ((spaceBetweenBars + heightofBarAndLabel) * i + spaceBetweenBars * 0.5),
labelLength, heightofBarAndLabel);
person[i]=new PersonThread(i);
this.jPanelForBar.add(bar[i]);
this.jPanelForBar.add(label[i]);
}
this.setVisible(false);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == preButton) {
runButton.setEnabled(true);
preButton.setEnabled(false);
}
if (e.getSource() == runButton) {
// JOptionPane.showMessageDialog(null,"Run");
// preButton.setEnabled(true);
runButton.setEnabled(false);
}
if (e.getSource() == exitButton) {
// JOptionPane.showMessageDialog(null,"ByeBye!");
new Factory().setVisible(true);
}
}
private static PersonBuildWindow pbw;
static {
pbw = new PersonBuildWindow();
}
public JButton getPreButton() {
return preButton;
}
public static PersonBuildWindow getPersonBuildWindow() {
return pbw;
}
public int getBarValue(int barNum){
return this.bar[barNum].getValue();
}
public void setBarValue(int barNum,int value){
this.bar[barNum].setValue(value);
}
public void setLabelText(int labelNum,String text){
this.label[labelNum].setText(text);
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
PersonBuildWindow.order = order;
}
public int getCountNum() {
return countNum;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -