package bankerUI;
import banker.Process;
import banker.BankerAlgorithm;
import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BankerMainUI extends JFrame implements ActionListener {
private static final long serialVersionUID = -8004544916653049326L;
private JPanel panel;
// north
private JButton addResource;
private JTextField textResourceCount;
// east
private JButton addProcess;
private JButton deleteProcess;
private JButton changeProcess;
private JButton requestResource;
private JButton currentSecured;
private JButton exit;
// center
private JEditorPane resourcesInfo;
private JEditorPane processesInfo; // 用html格式显示进程需要资源个数.
private JTextArea result;
private JSplitPane splitCenter;
// data
private BankerAlgorithm banker;
private int resourceClassesCount;// 表示资源的个数
// Dialogs
private AddResourceDialog resourceDialog;
private AddProcessDialog processDialog;
private ChangeProcessDialog change;
private DeleteProcessDialog delete;
private RequestResourceDialog request;
public BankerMainUI() {
super("银行家算法");
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Toolkit tool = this.getToolkit();
// Image ico =
// tool.getImage("G:\\workspace\\BankerAlgorithm\\exe\\img\\iconImage.png");
// setIconImage(ico);
setBounds(100, 100, 800, 600);
panel = new JPanel(new BorderLayout());
// north
JPanel north = new JPanel();
FlowLayout flowLayout = (FlowLayout) north.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
JLabel label = new JLabel("请输入系统资源种类个数:");
north.add(label);
textResourceCount = new JTextField(20);
north.add(textResourceCount);
addResource = new JButton("添加系统资源个数");
addResource.addActionListener(this);
north.add(addResource);
panel.add(north, BorderLayout.NORTH);
// center
resourcesInfo = new JEditorPane("text/html", "<html></html>");
resourcesInfo.setEditable(false);
processesInfo = new JEditorPane("text/html", "<html></html>"); // 以html格式显示进程信息.
processesInfo.setEditable(false);
JSplitPane splitInfo = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitInfo.add(new JScrollPane(resourcesInfo), JSplitPane.TOP);
splitInfo.add(new JScrollPane(processesInfo), JSplitPane.BOTTOM);
splitInfo.setBorder(BorderFactory.createTitledBorder("系统信息"));
splitInfo.setOneTouchExpandable(true);
result = new JTextArea(5, 30);
result.setEditable(false);
result.setWrapStyleWord(true); // 按单词换行,即所有单词都不会打断.
result.setLineWrap(true); // 换行.
JScrollPane textScroll = new JScrollPane(result);
textScroll.setBorder(BorderFactory.createTitledBorder("执行结果"));
splitCenter = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitCenter.setResizeWeight(1.0);
splitCenter.add(splitInfo, JSplitPane.TOP);
splitCenter.add(textScroll, JSplitPane.BOTTOM);
splitCenter.setOneTouchExpandable(true); // 点击一下就可以扩展分割开来的控件.
panel.add(splitCenter, BorderLayout.CENTER);
// east
JPanel east = new JPanel();
addProcess = new JButton("添加进程");
addProcess.addActionListener(this);
changeProcess = new JButton("更改进程信息");
changeProcess.addActionListener(this);
deleteProcess = new JButton("删除进程");
deleteProcess.addActionListener(this);
requestResource = new JButton("请求资源");
// requestResource.setAlignmentY(Component.BOTTOM_ALIGNMENT);
requestResource.addActionListener(this);
currentSecured = new JButton("当前系统是否安全");
currentSecured.addActionListener(this);
exit = new JButton("退出");
exit.addActionListener(this);
east.setLayout(new BoxLayout(east, BoxLayout.Y_AXIS));
east.add(addProcess);
east.add(changeProcess);
east.add(deleteProcess);
east.add(currentSecured);
east.add(requestResource);
east.add(exit);
panel.add(east, BorderLayout.EAST);
this.getContentPane().add(new JScrollPane(panel));
setEastButtonEnabled(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setResourceClassesCount(int count) {
resourceClassesCount = count;
}
public void setEastButtonEnabled(boolean b) {
// east
addProcess.setEnabled(b);
changeProcess.setEnabled(b);
deleteProcess.setEnabled(b);
requestResource.setEnabled(b);
currentSecured.setEnabled(b);
exit.setEnabled(b);
}
public BankerAlgorithm getBanker() {
return banker;
}
// 一个数组小于另一个数组,两个数组大小相等.
public boolean aLowerB(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
if (a[i] > b[i])
return false;
}
return true;
}
// 在resourceInfoz中显示系统资源的信息.
private void updateTotalResourcesInfo() {
StringBuffer html = new StringBuffer(100);
html.append("<html><body>");
html.append("<table width = \"100%\" border = \"1\" bgcolor=\"#C0C0C0\" bordercolor=\"#000000\">\n");
StringBuffer resourceNames = new StringBuffer("<tr><td>资源名</td>");
StringBuffer resourceCounts = new StringBuffer("<tr><td>资源个数</td>");
int[] totalResource = banker.getTotalResource();
for (int i = 0; i < totalResource.length; i++) {
resourceNames.append("<td>");
resourceNames.append("R" + String.valueOf(i));
resourceNames.append("</td>");
resourceCounts.append("<td>");
resourceCounts.append(String.valueOf(totalResource[i]));
resourceCounts.append("</td>");
}
resourceNames.append("</tr>");
resourceCounts.append("</tr>");
html.append(resourceNames);
html.append(resourceCounts);
html.append("</table>\n</body>\n</html>");
resourcesInfo.setText(html.toString());
}
private void updateProcessInfo() {
StringBuffer content = new StringBuffer("<html>\n");
content.append("<body>\n");
content.append("<table width = \"100%\" border = \"1\" bgcolor=\"#C0C0C0\" bordercolor=\"#000000\">\n");
content.append("<tr><td>资源情况</td><td align = \"center\" colspan = "
+ resourceClassesCount
+ ">Max</td><td align = \"center\" colspan = "
+ resourceClassesCount
+ ">Allocated</td><td align = \"center\" colspan = "
+ resourceClassesCount
+ ">Need</td><td align = \"center\" colspan = "
+ resourceClassesCount + ">Avilable</td></tr>");
content.append("<tr>");
content.append("<td>进程名</td>");
StringBuffer processNames = new StringBuffer(40);
for (int i = 0; i < resourceClassesCount; i++) {
processNames.append("<td>R" + i + "</td>");
}
content.append(processNames); // Max
content.append(processNames); // Allocated
content.append(processNames); // Need
content.append(processNames); // Avilable
content.append("</tr>");
ArrayList<Process> processes = banker.getProcesses();
for (int i = 0; i < processes.size(); i++) {
Process p = processes.get(i);
content.
银行家算法java实现带UI界面各个类文件
5星 · 超过95%的资源 需积分: 0 79 浏览量
更新于2012-06-19
4
收藏 14KB RAR 举报
这个文件为各个类的源代码文件,因为在银行家算法java实现带UI界面.docx文件中将BankMainUI.java文件写错了(由yu114提出的BankerMainUI.java与AddProcessDialog.java文件相同的问题),所以发布一下源代码一下。
coofee
- 粉丝: 3
- 资源: 7
最新资源
- 基于django+mysql的商品推荐系统全部资料+详细文档+高分项目.zip
- 基于django-wechat-base的微信信息管理模块全部资料+详细文档+高分项目.zip
- 基于django-xadmin的idc资产管理应用全部资料+详细文档+高分项目.zip
- 基于Django带支付宝支付电商购物商城网站设计毕业源码案例设计全部资料+详细文档+高分项目.zip
- 基于django的电影票比价网全部资料+详细文档+高分项目.zip
- 基于django的简单的cms系统全部资料+详细文档+高分项目.zip
- 基于Django的任务管理追踪平台全部资料+详细文档+高分项目.zip
- 基于Flask+Yolov5+Redis的深度学习在线监测网站详细文档+全部资料+高分项目.zip
- 基于flask-socketio的一个在线答题详细文档+全部资料+高分项目.zip
- 基于flask的后端敏捷开发框架详细文档+全部资料+高分项目.zip
- 基于flask的51商城应用系统设计详细文档+全部资料+高分项目.zip
- 基于flask的记账管理系统后端详细文档+全部资料+高分项目.zip
- 基于flask的人力资源管理系统详细文档+全部资料+高分项目.zip
- 基于flask的企业级开发模板,集成了flask-socketio,mongodb等等详细文档+全部资料+高分项目.zip
- 基于flask的随机故事网站详细文档+全部资料+高分项目.zip
- 基于Django的视频弹幕双创性教育网站:伊甸园的烦恼全部资料+详细文档+高分项目.zip
- 1
- 2
- 3
- 4
- 5
前往页