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 BankersAlgorithm banker;
private int resourceClassesCount;// 表示资源的个数
// Dialogs
private AddResource resourceDialog;
private AddProcess processDialog;
private RequestResource request;
public BankerMainUI() {
super("Bankers Algorithm");
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();
}
setBounds(200, 200, 800, 400);// 面板size
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(100);
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));
// splitInfo.setBorder(BorderFactory.createTitledBorder("系统信息"));
// splitInfo.setOneTouchExpandable(true);
result = new JTextArea(2, 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);
// splitCenter.add(textScroll, JSplitPane.BOTTOM);//执行结果那一栏界面
// splitCenter.setOneTouchExpandable(true); // 点击一下就可以扩展分割开来的控件.
panel.add(splitCenter);
// east
JPanel east = new JPanel();
addProcess = new JButton("输入进程");
addProcess.addActionListener(this);
requestResource = new JButton("请求资源");
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(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);
requestResource.setEnabled(b);
currentSecured.setEnabled(b);
exit.setEnabled(b);
}
public BankersAlgorithm 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=\"pink\" bordercolor=\"pink\">\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 = \"0\" bgcolor=\"white\" bordercolor=\"white\">\n");
content.append("<tr><td>资源情况</td><td align = \"left\" colspan = "
+ resourceClassesCount
+ ">Max</td><td align = \"left\" colspan = "
+ resourceClassesCount
+ ">Allocated</td><td align = \"left\" colspan = "
+ resourceClassesCount
+ ">Need</td><td align = \"left\" 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<SystemProcess> processes = banker.getProcesses();
for (int i = 0; i < processes.size(); i++) {
SystemProcess p = processes.get(i);
content.append("<tr>" + p.makeHtml());
if (i == 0) {
int[] avilable = banker.getAvilable();
for (int j = 0; j < avilable.length; j++)
content.append("<td>" + avilable[j] + "</td>");
}
if (i == 1)
content.append("<td rowspan ="
+ String.valueOf(processes.size() - 1)
+ " colspan = \"3\"></td>");
content.append("</tr>");
}
content.append("</table>\n");
content.append("</body>\n");
content.append("</html>");
processesInfo.setText(content.toString());
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == addResource) {
String resourceCount = textResour
评论4
最新资源