⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hand1.java

📁 操作系统中的移动臂调度算法
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Hand1 extends JFrame implements ActionListener,WindowListener
{ private int mess[]=new int[20];
  private JTextField text_cin,text_now;
  private JLabel label_1,label_2;
  private JRadioButton radiobutton_a,radiobutton_d;
  private JButton button_sure,button_cancel;
  private JTextArea text_show;
  private JDialog dialog;
  private JLabel label_dialog;
  
  public Hand1()
  { 
    super("移动臂调度算法演示");
    this.setSize(600,350);
    this.setLocation(350,300);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(new GridLayout(3,1));
    text_show=new JTextArea();
    text_show.setEditable(false);
    JPanel panel1=new JPanel(new GridLayout(2,1));
    JPanel panel2=new JPanel(new GridLayout(2,1));
    this.add(panel1);
    this.add(panel2);
    this.add(text_show);
    JPanel panel_1=new JPanel(new GridLayout(1,2));
    JPanel panel_2=new JPanel(new GridLayout(1,2));
    JPanel panel2_2=new JPanel(new GridLayout(1,4));
    JPanel panel2_1=new JPanel(new GridLayout(1,2));
    panel2.add(panel2_1);
    panel2.add(panel2_2);
    
    text_cin=new JTextField();
    label_1=new JLabel("输入访问磁道序列");
    panel_1.add(label_1);
    panel_1.add(text_cin);
    panel1.add(panel_1);
    text_now=new JTextField();
    label_2=new JLabel("输入起始磁道");
    panel_2.add(label_2);
    panel_2.add(text_now);
    panel1.add(panel_2);
    
    ButtonGroup buttongroup=new ButtonGroup();
    radiobutton_a=new JRadioButton("向外",false);
    radiobutton_d=new JRadioButton("向里",false);
    buttongroup.add(radiobutton_a);
    buttongroup.add(radiobutton_d);
    button_sure=new JButton("确定");
    button_cancel=new JButton("清除");
    panel2_1.add(radiobutton_a);
    panel2_1.add(radiobutton_d);
    panel2_2.add(button_sure);
    panel2_2.add(new JButton());
    panel2_2.add(button_cancel);
    dialog=new JDialog(this,"提示",true);
    dialog.setSize(240,80);
    label_dialog=new JLabel("",JLabel.CENTER);
    dialog.add(label_dialog);
    dialog.addWindowListener(this);
  
    button_sure.addActionListener(this);
    button_cancel.addActionListener(this);
    
    this.setVisible(true);
    for(int i=0;i<20;i++)
    mess[i]=-1;
    
  }
  
  public void str_int(String str)                //字符串转化成数字
  { int array=0;int i=0;
    String model="";
    while(i<str.length()&&array<20)
    { 
      while(i<str.length()&&str.charAt(i)>='0'&&str.charAt(i)<='9')
       { model+=str.charAt(i);
         i++;
       }
       if(model!="")
       { mess[array]=Integer.parseInt(model);
         array++;
       }
       i++; model="";
    }
    
  }
  
  public void swap(int messa[],int a,int b)
  { int temp;
    if(messa[a]>messa[b])
    { temp=messa[a];
      messa[a]=messa[b];
      messa[b]=temp;
    }
  }
  public void resort(int messa[])            //升序排序
  { int min;
    for(int j=0;messa[j]!=-1;j++)
    { min=j;
      for(int jj=j;messa[jj]!=-1;jj++)
      {
      	 if(messa[jj]<messa[min])
            min=jj;
      }
      if(min!=j) 
        swap(messa,j,min);
    } 
  }
  
  public int search(int a)                 //查找当前磁道号
  { 
    for(int k=0;mess[k]!=-1;k++)
    { if(a==mess[k]) return k;
    }
    return -1;
  }
  
  public void actionPerformed(ActionEvent e)
  {
  	 if(e.getSource()==button_cancel)
     { text_cin.setText("");
       text_now.setText("");
       text_show.setText("");
     }
     if(e.getSource()==button_sure)
     { if(text_cin.getText()!=""&&text_now.getText()!="")
       {
     	 if(radiobutton_a.isSelected())
          { String aline="",str=text_cin.getText();
            int number=0;
            try
            {
             int now=Integer.parseInt(text_now.getText());
            
          
            this.str_int(str);
            if(mess[0]==-1)
            {
            	label_dialog.setText("请输入正确的字符序列!");
            	dialog.setLocation(this.getX()+100,this.getY()+100);
            	dialog.setVisible(true);
            	return;
            }
            this.resort(mess);
            
            for(int ri=this.search(now);mess[ri]!=-1;ri++)
            { aline+=mess[ri];
              aline+=" , ";number++;
              if(number==5)
              { aline+="\n";
                number=0;
              }
            }
            for(int le=this.search(now)-1;le>=0;le--)
            { aline+=mess[le];
              aline+=" , ";number++;
              if(number>=5)
              { aline+="\n";
                number=0;
              }
            }
            text_show.append(aline);
             aline+="\n";
            
          }
          
            catch(NumberFormatException ee)
            {   label_dialog.setText("请输入正确的起始磁道号!");
            	dialog.setLocation(this.getX()+100,this.getY()+100);
            	dialog.setVisible(true);
            	return;
            }
            finally{}
           }
          
          
          if(radiobutton_d.isSelected())
          { String aline="",str=text_cin.getText();
            int number=0;
            try
            {
            int now=Integer.parseInt(text_now.getText());
            this.str_int(str);
            if(mess[0]==-1)
            {
            	label_dialog.setText("请输入正确的字符序列!");
            	dialog.setLocation(this.getX()+100,this.getY()+100);
            	dialog.setVisible(true);
            	return;
            }
            this.resort(mess);
            
            for(int le=this.search(now);le>=0;le--)
            { aline+=mess[le];
              aline+=" , ";number++;
              if(number>=5)
              { aline+="\n";
                number=0;
              }
            }
            for(int ri=this.search(now)+1;mess[ri]!=-1;ri++)
            { aline+=mess[ri];
              aline+=" , ";number++;
              if(number==5)
              { aline+="\n";
                number=0;
              }
            }
            
            text_show.append(aline);
            aline+="\n"; 
          }
          catch(NumberFormatException ee)
            {   label_dialog.setText("请输入正确的起始磁道号!");
            	dialog.setLocation(this.getX()+100,this.getY()+100);
            	dialog.setVisible(true);
            	return;
            }
            finally{}
           }
       }
          
     }
  }
  public void windowClosing(WindowEvent e)
  {
  	if(e.getSource()==dialog)
  	  dialog.setVisible(false);
  	else
  	  System.exit(0);
  }
  public void windowOpened(WindowEvent e){}
  public void windowActivated(WindowEvent e){}
  public void windowDeactivated(WindowEvent e){}
  public void windowClosed(WindowEvent e){}
  public void windowIconified(WindowEvent e){}
  public void windowDeiconified(WindowEvent e){}
  public static void main(String arg[])
  { new Hand1();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -