import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.UIManager;
public class Design extends JFrame {
private static final long serialVersionUID = 1L;
private int[] queue = new int[10];
private int startNum;
private JPanel contentPane;
private JLabel lblStartTrack;
private JLabel lblQueue;
private JLabel lblNextTrack;
private JLabel lblDistance;
private JLabel lblSelect;
private JLabel lblAverDistance;
private JTextArea taStartTrack;
private JTextArea taQueue;
private JTextArea taNextTrack;
private JTextArea taDistance;
private JTextArea taAverDistance;
private JButton btnReset;
private JButton btnRandom;
private JButton btnQuit;
private JButton btnCommit;
private JRadioButton rdbtnFCFS;
private JRadioButton rdbtnSSTF;
private JRadioButton rdbtnSCAN_1;
private JRadioButton rdbtnCSCAN_1;
private JRadioButton rdbtnSCAN_2;
private JRadioButton rdbtnCSCAN_2;
private ButtonGroup gb;
private ImageIcon icon;
private JLabel lblBackground;
public Design() {
setTitle("磁盘调度");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 50, 410, 650);
contentPane = new JPanel();
contentPane.setForeground(Color.WHITE);
contentPane.setBackground(UIManager.getColor("Button.background"));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setOpaque(false);
lblStartTrack = new JLabel("\u5F00\u59CB\u78C1\u9053\u53F7:");
lblStartTrack.setBounds(33, 10, 70, 21);
contentPane.add(lblStartTrack);
lblQueue = new JLabel("\u8BF7\u6C42\u961F\u5217");
lblQueue.setBounds(33, 29, 66, 21);
contentPane.add(lblQueue);
lblNextTrack = new JLabel("\u88AB\u8BBF\u95EE\u7684\u4E0B\u4E00\u78C1\u9053\u53F7");
lblNextTrack.setBounds(143, 32, 130, 15);
contentPane.add(lblNextTrack);
lblDistance = new JLabel("\u79FB\u52A8\u8DDD\u79BB");
lblDistance.setBounds(304, 32, 54, 15);
contentPane.add(lblDistance);
lblSelect = new JLabel("\u8BF7\u9009\u62E9\u4E00\u79CD\u7B97\u6CD5\uFF1A");
lblSelect.setBounds(10, 311, 121, 21);
contentPane.add(lblSelect);
lblAverDistance = new JLabel("\u5E73\u5747\u5BFB\u9053\u957F\u5EA6:");
lblAverDistance.setBounds(164, 10, 86, 21);
contentPane.add(lblAverDistance);
taStartTrack = new JTextArea();
taStartTrack.setBounds(106, 10, 48, 20);
contentPane.add(taStartTrack);
taQueue = new JTextArea();
taQueue.setBounds(47, 60, 39, 184);
contentPane.add(taQueue);
taNextTrack = new JTextArea();
taNextTrack.setBounds(181, 57, 55, 184);
contentPane.add(taNextTrack);
taDistance = new JTextArea();
taDistance.setBounds(314, 57, 39, 184);
contentPane.add(taDistance);
taAverDistance = new JTextArea();
taAverDistance.setBounds(253, 10, 130, 21);
contentPane.add(taAverDistance);
btnReset = new JButton("\u91CD\u7F6E");
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
taStartTrack.setText(null);
taAverDistance.setText(null);
taQueue.setText(null);
taNextTrack.setText(null);
taDistance.setText(null);
btnCommit.setEnabled(false);
}
});
btnReset.setBounds(10, 278, 93, 23);
contentPane.add(btnReset);
btnRandom = new JButton("\u968F\u673A\u521D\u59CB\u5316");
btnRandom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
taStartTrack.setText(null);
taAverDistance.setText(null);
taQueue.setText(null);
taNextTrack.setText(null);
taDistance.setText(null);
btnCommit.setEnabled(true);
for (int i = 0; i < 10; i++) {
queue[i] = (int) (Math.random() * 100);
for (int j = 0; j < i; j++) {
while (queue[i] == queue[j]) {
queue[i] = (int) (Math.random() * 100);
}
}
}
startNum = (int) (Math.random() * 100);
for (int k = 0; k < 10; k++) {
while (startNum == queue[k]) {
startNum = (int) (Math.random() * 100);
}
}
taStartTrack.append(String.valueOf(startNum));
for (int h = 0; h < 10; h++) {
taQueue.append(String.valueOf(queue[h]) + "\n");
}
}
});
btnRandom.setBounds(134, 278, 121, 23);
contentPane.add(btnRandom);
btnQuit = new JButton("\u9000\u51FA");
btnQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnQuit.setBounds(290, 278, 93, 23);
contentPane.add(btnQuit);
rdbtnFCFS = new JRadioButton("\u5148\u6765\u5148\u670D\u52A1");
rdbtnFCFS.setBounds(33, 338, 121, 23);
rdbtnFCFS.setOpaque(false);
contentPane.add(rdbtnFCFS);
rdbtnSSTF = new JRadioButton("\u6700\u77ED\u5BFB\u9053\u65F6\u95F4\u4F18\u5148");
rdbtnSSTF.setBounds(208, 338, 150, 23);
rdbtnSSTF.setOpaque(false);
contentPane.add(rdbtnSSTF);
rdbtnSCAN_1 = new JRadioButton("\u626B\u63CF\uFF08\u81EA\u91CC\u5411\u5916\uFF09");
rdbtnSCAN_1.setBounds(33, 365, 132, 23);
rdbtnSCAN_1.setOpaque(false);
contentPane.add(rdbtnSCAN_1);
rdbtnCSCAN_1 = new JRadioButton("\u5FAA\u73AF\u626B\u63CF\uFF08\u81EA\u91CC\u5411\u5916\uFF09");
rdbtnCSCAN_1.setBounds(208, 365, 159, 23);
rdbtnCSCAN_1.setOpaque(false);
contentPane.add(rdbtnCSCAN_1);
rdbtnSCAN_2 = new JRadioButton("\u626B\u63CF\uFF08\u81EA\u5916\u5411\u91CC\uFF09");
rdbtnSCAN_2.setBounds(33, 393, 132, 23);
rdbtnSCAN_2.setOpaque(false);
contentPane.add(rdbtnSCAN_2);
rdbtnCSCAN_2 = new JRadioButton("\u5FAA\u73AF\u626B\u63CF\uFF08\u81EA\u5916\u5411\u91CC\uFF09");
rdbtnCSCAN_2.setBounds(210, 393, 173, 23);
rdbtnCSCAN_2.setOpaque(false);
contentPane.add(rdbtnCSCAN_2);
gb = new ButtonGroup();
gb.add(rdbtnFCFS);
gb.add(rdbtnSSTF);
gb.add(rdbtnSCAN_1);
gb.add(rdbtnCSCAN_1);
gb.add(rdbtnSCAN_2);
gb.add(rdbtnCSCAN_2);
btnCommit = new JButton("\u786E\u5B9A");
btnCommit.setEnabled(false);
btnCommit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!rdbtnFCFS.isSelected() && !rdbtnSSTF.isSelected() && !rdbtnSCAN_1.isSelected()
&& !rdbtnCSCAN_1.isSelected() && !rdbtnSCAN_2.isSelected() && !rdbtnCSCAN_2.isSelected()) {
JOptionPane.showMessageDialog(btnRandom, "请选择一种算法!","提示",JOptionPane.ERROR_MESSAGE);
} else {
taNextTrack.setText(null);
taDistance.setText(null);
taAverDistance.setText(null);
while (Algorithm.finishedList.isEmpty() == false) {
Algorithm.finishedList.remove(0);
}
ArrayList<Track> list = new ArrayList<Track>();
for (int i = 0; i < 10; i++) {
list.add(new Track(queue[i]));
}
if (rdbtnFCFS.isSelected()) {
Algorithm.fcfs(list, startNum);
} else if (rdbtnSSTF.isSelected()) {
Algorithm.sstf(list, startNum);
} else if (rdbtnSCAN_1.isSelected()) {
Algorithm.scan_1(list, startNum);
} else if (rdbtnCSCAN_1.isSelected()) {
Algorithm.cscan_1(list, startNum);
} else if (rdbtnSCAN_2.isSelected()) {
Algorithm.scan_2(list, startNum);
} else if (rdbtnCSCAN_2.isSelected()) {
Algorithm.cscan_2(list, startNum);
}
for (int j = 0; j < 10; j++) {
taNextTrack.append(Algorithm.finishedList.get(j).num + "\n");
taDistance.append(Algorithm.finishedList.get(j).distance + "\n");
}
taAverDistance.append(Algorithm.averDistance + "");
}
}
});
btnCommit.setBounds(143, 433, 93, 23);
contentPan