java实现银行家算法界面
时间: 2023-08-14 15:38:39 浏览: 136
java银行家算法
4星 · 用户满意度95%
以下是一个简单的 Java 界面实现银行家算法的例子:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BankerAlgorithm implements ActionListener {
JFrame frame;
JLabel processesLabel, resourcesLabel, allocationLabel, maxLabel, availableLabel, resultLabel;
JTextField processesField, resourcesField, availableField;
JPanel inputPanel, allocationPanel, maxPanel, availablePanel, resultPanel, buttonPanel;
JButton runButton, resetButton;
JTextField[][] allocationFields, maxFields;
int[][] allocation, max, need, available;
int[] processes, resources;
public BankerAlgorithm() {
frame = new JFrame("银行家算法");
processesLabel = new JLabel("进程数:");
resourcesLabel = new JLabel("资源数:");
allocationLabel = new JLabel("已分配矩阵:");
maxLabel = new JLabel("最大需求矩阵:");
availableLabel = new JLabel("可用资源数:");
resultLabel = new JLabel("");
processesField = new JTextField(5);
resourcesField = new JTextField(5);
availableField = new JTextField(5);
runButton = new JButton("运行");
runButton.addActionListener(this);
resetButton = new JButton("重置");
resetButton.addActionListener(this);
inputPanel = new JPanel(new GridLayout(1, 4));
inputPanel.add(processesLabel);
inputPanel.add(processesField);
inputPanel.add(resourcesLabel);
inputPanel.add(resourcesField);
allocationPanel = new JPanel();
maxPanel = new JPanel();
availablePanel = new JPanel(new GridLayout(1, 2));
resultPanel = new JPanel();
buttonPanel = new JPanel(new GridLayout(1, 2));
frame.add(inputPanel, BorderLayout.NORTH);
frame.add(allocationPanel, BorderLayout.WEST);
frame.add(maxPanel, BorderLayout.EAST);
frame.add(availablePanel, BorderLayout.SOUTH);
frame.add(resultPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.setSize(600, 400);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == runButton) {
int n = Integer.parseInt(processesField.getText());
int m = Integer.parseInt(resourcesField.getText());
allocation = new int[n][m];
max = new int[n][m];
need = new int[n][m];
available = new int[1][m];
processes = new int[n];
resources = new int[m];
allocationPanel.removeAll();
allocationPanel.setLayout(new GridLayout(n + 1, m + 1));
allocationFields = new JTextField[n][m];
allocationPanel.add(new JLabel(""));
for (int j = 0; j < m; j++) {
allocationPanel.add(new JLabel("资源 " + (j + 1)));
}
for (int i = 0; i < n; i++) {
allocationPanel.add(new JLabel("进程 " + (i + 1)));
for (int j = 0; j < m; j++) {
allocationFields[i][j] = new JTextField(5);
allocationPanel.add(allocationFields[i][j]);
}
}
maxPanel.removeAll();
maxPanel.setLayout(new GridLayout(n + 1, m + 1));
maxFields = new JTextField[n][m];
maxPanel.add(new JLabel(""));
for (int j = 0; j < m; j++) {
maxPanel.add(new JLabel("资源 " + (j + 1)));
}
for (int i = 0; i < n; i++) {
maxPanel.add(new JLabel("进程 " + (i + 1)));
for (int j = 0; j < m; j++) {
maxFields[i][j] = new JTextField(5);
maxPanel.add(maxFields[i][j]);
}
}
available[0] = new int[m];
String[] availableValues = availableField.getText().split(",");
for (int i = 0; i < m; i++) {
available[0][i] = Integer.parseInt(availableValues[i]);
}
for (int i = 0; i < n; i++) {
processes[i] = i;
for (int j = 0; j < m; j++) {
allocation[i][j] = Integer.parseInt(allocationFields[i][j].getText());
max[i][j] = Integer.parseInt(maxFields[i][j].getText());
need[i][j] = max[i][j] - allocation[i][j];
}
}
for (int i = 0; i < m; i++) {
resources[i] = 0;
for (int j = 0; j < n; j++) {
resources[i] += allocation[j][i];
}
available[0][i] -= resources[i];
}
if (isSafeState(n, m)) {
resultLabel.setText("系统处于安全状态!");
} else {
resultLabel.setText("系统处于不安全状态!");
}
} else if (e.getSource() == resetButton) {
processesField.setText("");
resourcesField.setText("");
availableField.setText("");
allocationPanel.removeAll();
maxPanel.removeAll();
resultLabel.setText("");
}
}
public boolean isSafeState(int n, int m) {
int[][] work = new int[1][m];
for (int i = 0; i < m; i++) {
work[0][i] = available[0][i];
}
boolean[] finish = new boolean[n];
for (int i = 0; i < n; i++) {
finish[i] = false;
}
int count = 0;
while (count < n) {
boolean found = false;
for (int i = 0; i < n; i++) {
if (!finish[i]) {
boolean notEnoughResources = false;
for (int j = 0; j < m; j++) {
if (need[i][j] > work[0][j]) {
notEnoughResources = true;
break;
}
}
if (!notEnoughResources) {
for (int j = 0; j < m; j++) {
work[0][j] += allocation[i][j];
}
finish[i] = true;
found = true;
count++;
}
}
}
if (!found) {
return false;
}
}
return true;
}
public static void main(String[] args) {
new BankerAlgorithm();
}
}
```
这个界面包括了进程数、资源数、已分配矩阵、最大需求矩阵和可用资源数的输入框,以及运行和重置按钮。在输入数据后,点击运行按钮会调用 `isSafeState` 方法来判断系统是否处于安全状态,并在界面上显示结果。
阅读全文