📄 disk.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class disk extends JFrame implements ActionListener
{
JPanel upper,lower,left,right;
JLabel first,second,third;
JTextField t1,t2,t3;
JButton ensure,clear,division,reclaim,close;
int s,total,cidao,record;//s: totaol: cidao:record:
int weishi[][];
int mark=0;
String f_s_name;
String f_c_name;
//---------distribute--------------------------
JLabel former,latter;
JButton exit;
JTable di1,di2;
JDialog division_dia,division_la;
JLabel l1;
JTextArea area1;
JScrollPane area1_sc;
JPanel pp1;
JButton continus,separated,fen_clear;
JLabel wenjian_label,count_label;
JTextField wenjian_field,count_field;
//--------------------------------------
//----------recall-----------------------
JDialog reclaim_dia,reclaim_la;
JTable re1,re2;
JButton ensure_re,clear_re,exit_re;
JLabel before_recall,after_recall;
JPanel p3;
JLabel kind,name;
JTextArea area2;
JLabel wen_name;
JTextField word;
JScrollPane sc2;
public class Process
{
String name;//name of process
int count;//need disk
//Vector hang,lie;
int hang[];
int lie[];//hang lie
}
//-------------------------------------
Vector vector=new Vector();
public disk()
{ super("disk");
this.setLayout(new FlowLayout(FlowLayout.CENTER,4,30));
this.setSize(450,250);
this.setLocation(390,150);
this.setBackground(Color.red);
//--------------------definition---------------
left=new JPanel();
left.setLayout(new GridLayout(3,2));
first=new JLabel("total",SwingConstants.RIGHT);
second=new JLabel("per_cidao",SwingConstants.RIGHT);
third=new JLabel("per_record",SwingConstants.RIGHT);
t1=new JTextField(4);
t2=new JTextField(4);
t3=new JTextField(4);
left.add(first);
left.add(t1);
left.add(second);
left.add(t2);
left.add(third);
left.add(t3);
right=new JPanel();
right.setLayout(new FlowLayout(FlowLayout.CENTER,10,3));
ensure=new JButton("sure");
clear=new JButton("clear");
right.add(ensure);
right.add(clear);
upper=new JPanel();
upper.setLayout(new GridLayout(1,2));
upper.add(left);
upper.add(right);
lower=new JPanel();
lower.setLayout(new FlowLayout(FlowLayout.CENTER,20,4));
division=new JButton("disk distribute");
reclaim=new JButton("disk recall");
close=new JButton("exit");
lower.add(division);
lower.add(reclaim);
lower.add(close);
ensure.addActionListener(this);
clear.addActionListener(this);
division.addActionListener(this);
close.addActionListener(this);
reclaim.addActionListener(this);
this.add(upper);
this.add(lower);
//-------distribute---------------------
former=new JLabel("before distribute chart");
latter=new JLabel("after distribute chart");
continus=new JButton("continuous distribute");
separated=new JButton(" separated distribute");
fen_clear=new JButton("clear");
exit=new JButton("exit");
l1=new JLabel("the process of assigment");
wenjian_label=new JLabel("name of program");
count_label=new JLabel("number distri_disk");
wenjian_field=new JTextField(8);
count_field=new JTextField(2);
continus.addActionListener(this);
separated.addActionListener(this);
fen_clear.addActionListener(this);
exit.addActionListener(this);
area1=new JTextArea(8,30);
area1_sc=new JScrollPane(area1);
//------------------------------
//---------------recall------------
ensure_re=new JButton("recall");
clear_re=new JButton("clear");
exit_re=new JButton("exit");
before_recall=new JLabel("before recall");
after_recall=new JLabel("after recall");
p3=new JPanel();
name=new JLabel("process of assigment");
area2=new JTextArea(8,30);
ensure_re.addActionListener(this);
clear_re.addActionListener(this);
exit_re.addActionListener(this);
wen_name=new JLabel("name of program");
word=new JTextField(4);
sc2=new JScrollPane(area2);
this.setVisible(true);
}//disk
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ensure)
{
if(t1.getText().equals("")||t2.getText().equals("")||t2.getText().equals(""))
JOptionPane.showMessageDialog(this,"your number is not full,please put again");
else
{
JOptionPane.showMessageDialog(this,"successful,you can distribute or recall");
total=Integer.parseInt(t1.getText());
cidao=Integer.parseInt(t2.getText());
record=Integer.parseInt(t3.getText());
mark=cidao*record;
s=total*mark;
}
weishi=new int[total][mark];
Random r=new Random();
for(int i=0;i<total;i++)//first difinity
{
for(int k=0;k<mark;k++)
{
weishi[i][k]=r.nextInt(2);
if(weishi[i][k]==1) s--;
}
}
}//sure
//---------------------------
if(e.getSource()==clear)
{
t1.setText("");
t2.setText("");
t3.setText("");
}
//-------------------
if(e.getSource()==division)
{
if(t1.getText().equals("")||t2.getText().equals("")||t2.getText().equals(""))
JOptionPane.showMessageDialog(this,"please found primitive chart!");
else
{
if(s==0)JOptionPane.showMessageDialog(this, "disk has no piece to distribute,please recall!");
else{
division_dia=new JDialog();
division_dia.setTitle("the chart before distribute");
division_dia.setSize(500,550);
division_dia.setLayout(new FlowLayout());
division_dia.setBackground(Color.red);
division_la=new JDialog();
division_la.setTitle("the chart after distribute");
division_la.setSize(500,850);
division_la.setLocation(520,1);
division_la.setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
di1=new JTable(total+1,mark+1);
JScrollPane dis1=new JScrollPane(di1);
di2=new JTable(total+1,mark+1);
JScrollPane dis2=new JScrollPane(di2);
di1.setValueAt("wei", 0, 0);
di1.setValueAt("zi0", 1, 0);
di2.setValueAt("wei", 0, 0);
di2.setValueAt("zi0", 1, 0);
for(int i=0;i<mark;i++)
{
di1.setValueAt(Integer.toString(i), 0, i+1);
di2.setValueAt(Integer.toString(i), 0, i+1);
}
for(int i=0;i<total-1;i++)
{
di1.setValueAt(Integer.toString(i+1), i+2, 0);
di2.setValueAt(Integer.toString(i+1), i+2, 0);
}
for(int i=0;i<total;i++)
{
for(int k=0;k<mark;k++)
{
di1.setValueAt(Integer.toString(weishi[i][k]), i+1, k+1);
}
}
division_dia.add(former);
division_dia.add(dis1);//before distribute
division_la.add(latter);
division_la.add(dis2);
division_la.add(wenjian_label);
division_la.add(wenjian_field);
division_la.add(count_label);
division_la.add(count_field);
division_la.add(fen_clear);
division_la.add(exit);
division_la.add(separated);
division_la.add(continus);// after distribute widown
pp1=new JPanel();
pp1.setSize(400,200);
pp1.setLocation(700,150);
pp1.setLayout(new BorderLayout());
pp1.add("North",l1);
pp1.add("Center",area1_sc);
division_la.add(pp1);
this.setVisible(false);
division_dia.setVisible(true);
division_la.setVisible(true);
}
}// else
}//
//----------------------------------------
if(e.getSource()== continus)//
{
for(int i=0;i<total;i++)
{
for(int k=0;k<mark;k++)
{
di1.setValueAt(Integer.toString(weishi[i][k]), i+1, k+1);
}
}
Process pro=new Process();
pro.count=Integer.parseInt(count_field.getText());
pro.name=wenjian_field.getText();
f_c_name=pro.name;
pro.hang=new int[pro.count];
pro.lie=new int[pro.count];;
boolean op=true,ot=true;
int zi_hao=0,wei_hao=0;
int relative_kuai,zhu_nu,cidao_nu,wuli_nu;
int limit=0;
int hang,lie;
int coun=0;
op=true;ot=true;
for(int j=0;j<pro.count;j++)
{
for(int i=0;(i<total)&&op;i++)
{
for(int k=0;(k<mark)&&ot;k++)
{
if(weishi[i][k]==0)
{
System.out.println(coun);
coun++;
}//if
else coun=0;
if(coun==pro.count)
{
op=false;
ot=false;
hang=i;
lie=k;
pro.hang[0]=hang+1;
pro.lie[0]=lie+1;
weishi[hang][lie]=1;
int s=mark*hang+lie;
System.out.println("hang:"+hang);
System.out.println("lie:"+lie);
for(int m=1;m<pro.count;m++)
{
s--;
hang=s/mark;
lie=s%mark;
weishi[hang][lie]=1;
pro.hang[m]=hang+1;
pro.lie[m]=lie+1;
System.out.println("hang:"+hang);
System.out.println("lie:"+lie);
}//for
}//if
}//for k
}//for i
}//for j
if(coun!=pro.count)
{
JOptionPane.showMessageDialog(division_la,"disk has no piece to distribute,please recall!");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -