java实现银行家算法(Swing界面)
java代码实现了银行家算法,界面写的个人认为还是较为细致的,完整的实现了找安全序列等算法功能,可作为参考学习银行家算法。
直接上代码:①界面展示方法:
publicvoidShowFrame()
{
this.setSize(500,350);//大小
this.setAlwaysOnTop(true);
this.setResizable(false);//不可拖动
this.setLayout(newBorderLayout());
this.setTitle("lly_banktest");
jp1=newJPanel();
Strings[]={"Allocation","Max","Available","Request"};
jcb1=newJComboBox(s);
jp1.add(jcb1);
jp1.add(newJLabel("PID:"));
jtf1=newJTextField(3);
jp1.add(jtf1);
jp1.add(newJLabel("A:"));
jtf2=newJTextField(3);
jp1.add(jtf2);
jp1.add(newJLabel("B:"));
jtf3=newJTextField(3);
jp1.add(jtf3);
jp1.add(newJLabel("C:"));
jtf4=newJTextField(3);
jp1.add(jtf4);
jb1=newJButton("确定");
jp1.add(jb1);
jb1.addActionListener(this);
this.add(jp1,"South");
jta1=newJTextArea();//显示文件本域
ShowData();//显示数据
jta1.setLineWrap(true);//自动适应
intr,g,b;
jta1.setBackground(Color.white);
jta1.setEditable(false);
this.add(jta1,"Center");
//Fontf=newFont("Dialog",Font.BOLD,12);//
jp1.setBackground(newjava.awt.Color(128,255,128));
}
publicvoidShowData(){
jta1.setText("Max\tAllocationNeed\tAvailable\n");
jta1.append("\n"+"资源:"+"ABC"+"ABC"+
"ABC"+"ABC");
jta1.append("\n进程\n"+pname[0]+""+
+Max[0][0]+""+Max[0][1]+""
+Max[0][2]+""+
""+Allocation[0][0]+""+Allocation[0][1]
+""+Allocation[0][2]+""+
""+Need[0][0]+""+Need[0][1]
+""+Need[0][2]+""+
""+Available[0]+""+Available[1]+
""+Available[2]);
for(inti=1;i<5;i++)
{
jta1.append("\n\n"+pname[i]+""+
""+Max[i][0]+""+Max[i][1]+""+Max[i][2]+""+
""+Allocation[i][0]+""+Allocation[i][1]
+""+Allocation[i][2]+""+
""+Need[i][0]+""+Need[i][1]
+""+Need[i][2]+"");
}
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
jtf4.setText("");
}
截图:
②算法实现代码:
publicvoidmyAllocation(inti)//分配资源
{
for(intj=0;jNeed[i][j])
break;
if(Request[j]>Available[j])
break;
elsechoice=true;
}
returnchoice;
}
publicbooleanSaftyCheck(intp)//安全性检查
{
intk=0;
booleanb=true;
Work=newint[sno];//定义工作向量并赋初值
Finish=newboolean[pno];
for(inti=0;iWork[j])//资源不足,退出
{
flag=false;
break;
}
}
if(flag)//找到资源
{
temp[k++]=i;//存储安全序列
Finish[i]=true;
found=true;
for(intj=0;j
截图:
③事件响应函数:
publicvoidactionPerformed(ActionEvente){
//事件响应函数
if(e.getSource()==jb1){//按下“确定”
if(jcb1.getSelectedItem()=="Request"){
intp=0;
try{
p=Integer.parseInt(jtf1.getText());
Request[0]=Integer.parseInt(jtf2.getText());
Request[1]=Integer.parseInt(jtf3.getText());
Request[2]=Integer.parseInt(jtf4.getText());
}catch(Exceptiond)
{
JOptionPane.showMessageDialog(this,"您输入有误!请重新输入!");
ShowData();
return;
}
if(p>4)//限定输入进程ID范围
{
JOptionPane.showMessageDialog(this,"PID在0-4之间!");
jtf1.setText("");
return;
}
if(judge(p,Request))//初步分配检查
{
if(SaftyCheck(p)){//安全性检查
ShowData();
jta1.append("\n\n通过安全性检查!安全序列为:");
for(inti=0;i4)
{
JOptionPane.showMessageDialog(this,"进程ID在0-4之间!");
jtf1.setText("");
return;
}
intMaxago[][]=newint[pno][sno];//暂存最大需求
Max[p][0]=Integer.parseInt(jtf2.getText());
Max[p][1]=Integer.parseInt(jtf3.getText());
Max[p][2]=Integer.parseInt(jtf4.getText());
for(intj=0;j=0)
Need[p][j]=temp;
else
{
JOptionPane.showMessageDialog(this,"最大需求过小!请重新输入!");
Max=Maxago;
ShowData();
return;
}
}
}catch(Exceptiond)
{
JOptionPane.showMessageDialog(this,"输入有误!请重新输入!");
ShowData();
return;
}
ShowData();
jta1.append("\n\n最大需求设置成功!");
}
/**
*选择Allocation时!
*/
elseif(jcb1.getSelectedItem()=="Allocation"){//设置已分配资源
intp=0;
try{
p=Integer.parseInt(jtf1.getText());
if(p>4)
{
JOptionPane.showMessageDialog(this,"进程ID在0-4之间!");
jtf1.setText("");
return;
}
Allocation[p][0]=Integer.parseInt(jtf2.getText());
Allocation[p][1]=Integer.parseInt(jtf3.getText());
Allocation[p][2]=Integer.parseInt(jtf4.getText());}
catch(Exceptiond)
{
JOptionPane.showMessageDialog(this,"输入有误!请重新输入!");
ShowData();
return;
}
ShowData();
jta1.append("\n\n已分配资源设置成功!");
}
}
截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。