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.
coofee
- 粉丝: 3
- 资源: 7
最新资源
- 数据库课程设计-基于的个性化购物平台的建表语句.sql
- 数据库课程设计-基于的图书智能一体化管理系统的建表语句.sql
- Java 代码覆盖率库.zip
- Java 代码和算法的存储库 也为该存储库加注星标 .zip
- 免安装Windows10/Windows11系统截图工具,无需安装第三方截图工具 双击直接使用截图即可 是一款免费可靠的截图小工具哦~
- Libero Soc v11.9的安装以及证书的获取(2021新版).zip
- BouncyCastle.Cryptography.dll
- 5.1 孤立奇点(JD).ppt
- 基于51单片机的智能交通灯控制系统的设计与实现源码+报告(高分项目)
- 什么是 SQL 注入.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
前往页