package wsk;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.*;
public class Disk extends JFrame implements ActionListener
{
JRadioButton jrbRandom=new JRadioButton("随机生成磁道");
JRadioButton jrbDecide=new JRadioButton("指定磁道");
ButtonGroup bg=new ButtonGroup();
JLabel jlDiskNum=new JLabel("磁道数量(5~20)");
JButton jbCreate=new JButton("生成");
JButton jbEnter=new JButton("确定");
JButton jbEnter2=new JButton("确定");
JTextField jtfDiskNum=new JTextField("10");
JTextField jtfDiskList=new JTextField("55,58,39,18,90,160,150,38,184");
JTextField jtfInitDisk=new JTextField("100");
JLabel jlRandom=new JLabel("随机生成磁道");
JLabel jlText=new JLabel("注意:请用逗号隔开。");
JLabel jlInitDisk=new JLabel("磁道初始位置");
JPanel jpRandom=new JPanel();
JPanel jpDecide=new JPanel();
JLabel[] jl=
{
new JLabel("FCFS"),
new JLabel("SSTF"),
new JLabel("SCAN"),
new JLabel("CSCAN"),
};
int[] disk;
int diskNum=0;
int initDisk=0;
boolean drew=false;
Control diskClass;
DrawTable dt;
public Disk()
{
this.setTitle("磁盘寻道计算器");
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
jpRandom.setLayout(null);
jpRandom.setBounds(10,10,235,70);
jpRandom.setBorder(new TitledBorder(""));
jrbRandom.setBounds(10,10,150,20);
jrbRandom.setSelected(true);
setState(true);
jrbRandom.addActionListener(this);
jpRandom.add(jrbRandom);
jlDiskNum.setBounds(10,40,95,20);
jpRandom.add(jlDiskNum);
jtfDiskNum.setBounds(105,40,40,20);
jpRandom.add(jtfDiskNum);
jbCreate.setBounds(155,40,60,20);
jbCreate.addActionListener(this);
jpRandom.add(jbCreate);
jpDecide.setLayout(null);
jpDecide.setBounds(255,10,335,70);
jpDecide.setBorder(new TitledBorder(""));
jrbDecide.setBounds(10,10,80,20);
jrbDecide.addActionListener(this);
jpDecide.add(jrbDecide);
jlText.setBounds(135, 10, 200, 20);
jpDecide.add(jlText);
jtfDiskList.setBounds(10,40,250,20);
jpDecide.add(jtfDiskList);
jbEnter.setBounds(265, 40, 60, 20);
jbEnter.addActionListener(this);
jpDecide.add(jbEnter);
for(int i=0;i<jl.length;i++)
{
jl[i].setBounds(20+185*i,145,140,20);
this.add(jl[i]);
}
bg.add(jrbRandom);
bg.add(jrbDecide);
jlInitDisk.setBounds(10,95,80,20);
this.add(jlInitDisk);
jtfInitDisk.setBounds(95,95,50,20);
this.add(jtfInitDisk);
jbEnter2.setBounds(155,95,60,20);
jbEnter2.addActionListener(this);
this.add(jbEnter2);
setInitButtonState(false);
this.add(jpDecide);
this.add(jpRandom);
this.setBounds(120,20,770,620);
this.setVisible(true);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jrbRandom)
{
setState(true);
setInitButtonState(false);
}
else if(e.getSource()==jrbDecide)
{
setState(false);
setInitButtonState(false);
}
else if(e.getSource()==jbCreate)
{
try{
diskNum=Integer.parseInt(this.jtfDiskNum.getText().trim());
}catch(Exception ex)
{
JOptionPane.showMessageDialog(this, "请输入正确的数字。","输入错误",JOptionPane.ERROR_MESSAGE);
}
if(diskNum<5||diskNum>20)
{
JOptionPane.showMessageDialog(this, "请输入5~20的数字。","输入错误",JOptionPane.ERROR_MESSAGE);
}
random(diskNum);
setInitButtonState(true);
}
else if(e.getSource()==jbEnter)
{
String diskList=this.jtfDiskList.getText().trim();
String[] diskStr=diskList.split(",");
diskNum=diskStr.length;
if(diskNum<5||diskNum>20)
{
JOptionPane.showMessageDialog(this, "磁道的数量应为5~20个。","输入错误",JOptionPane.ERROR_MESSAGE);
return;
}
disk=new int[diskNum];
try
{
for(int i=0;i<diskNum;i++)
{
disk[i]=Integer.parseInt(diskStr[i]);
}
}catch(Exception ex)
{
JOptionPane.showMessageDialog(this, "请输入正确的磁道数值。","输入错误",JOptionPane.ERROR_MESSAGE);
return;
}
setInitButtonState(true);
}
else if(e.getSource()==jbEnter2)
{
try{
initDisk=Integer.parseInt(this.jtfInitDisk.getText().trim());
}catch(Exception ex)
{
JOptionPane.showMessageDialog(this, "请输入正确的初始磁道位置(0~200)。","输入错误",JOptionPane.ERROR_MESSAGE);
}
if(initDisk<0||initDisk>200)
{
JOptionPane.showMessageDialog(this, "请输入正确的初始磁道位置(0~200)。","输入错误",JOptionPane.ERROR_MESSAGE);
}
diskClass=new Control(disk,initDisk);
if(drew)
{
for(int i=0;i<4;i++)
{
this.remove(dt.jsp[i]);
}
}
dt=new DrawTable(this,diskClass);
dt.draw();
}
}
public void setState(boolean flag)
{
jtfDiskNum.setEnabled(flag);
jbCreate.setEnabled(flag);
jtfDiskList.setEnabled(!flag);
jbEnter.setEnabled(!flag);
}
public void setInitButtonState(boolean flag)
{
jbEnter2.setEnabled(flag);
jtfInitDisk.setEnabled(flag);
}
public void random(int num)
{
disk=new int[num];
Random ran=new Random();
int i;
for(i=0;i<num;i++)
{
disk[i]=(int)(ran.nextFloat()*200);
}
}
public static void main(String[] args)
{
new Disk();
}
}