时间片轮转算法输出包含进程名,开始时间,运行时间,状态的表格javaGUI实现完整可运行代码
时间: 2023-06-17 19:04:44 浏览: 90
以下是一个使用Java Swing GUI实现的时间片轮转算法的完整可运行代码,包含进程名,开始时间,运行时间和状态的表格输出:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Queue;
public class TimeSliceRoundRobin extends JFrame {
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JLabel titleLabel;
private JLabel quantumLabel;
private JTextField quantumTextField;
private JButton addButton;
private JButton runButton;
private JTable processTable;
private DefaultTableModel model;
private int quantum;
private Queue<Process> processQueue;
private Queue<Process> readyQueue;
public TimeSliceRoundRobin() {
// 设置窗口标题和大小
setTitle("Time Slice Round Robin");
setSize(500, 500);
// 初始化队列和表格模型
processQueue = new LinkedList<>();
readyQueue = new LinkedList<>();
String[] columnNames = {"进程名", "开始时间", "运行时间", "状态"};
model = new DefaultTableModel(columnNames, 0);
// 设置表格
processTable = new JTable(model);
processTable.setPreferredScrollableViewportSize(new Dimension(450, 400));
processTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(processTable);
panel3.add(scrollPane);
// 添加按钮事件
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String processName = JOptionPane.showInputDialog(panel1, "请输入进程名");
int startTime = Integer.parseInt(JOptionPane.showInputDialog(panel1, "请输入开始时间"));
int runTime = Integer.parseInt(JOptionPane.showInputDialog(panel1, "请输入运行时间"));
processQueue.add(new Process(processName, startTime, runTime));
model.addRow(new Object[]{processName, startTime, runTime, "未执行"});
}
});
runButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取时间片大小
quantum = Integer.parseInt(quantumTextField.getText());
// 运行进程
int currentTime = 0;
while (!processQueue.isEmpty() || !readyQueue.isEmpty()) {
if (!processQueue.isEmpty()) {
Process currentProcess = processQueue.poll();
int runTime = Math.min(quantum, currentProcess.getRunTime());
currentTime += runTime;
currentProcess.setRunTime(currentProcess.getRunTime() - runTime);
if (currentProcess.getRunTime() == 0) {
model.setValueAt("已完成", currentProcess.getIndex(), 3);
} else {
readyQueue.add(currentProcess);
}
}
while (!readyQueue.isEmpty()) {
Process p = readyQueue.poll();
int runTime = Math.min(quantum, p.getRunTime());
currentTime += runTime;
p.setRunTime(p.getRunTime() - runTime);
if (p.getRunTime() == 0) {
model.setValueAt("已完成", p.getIndex(), 3);
} else {
readyQueue.add(p);
}
}
}
}
});
// 添加组件到面板
panel1.setLayout(new GridLayout(2, 2));
panel1.add(new JLabel("进程名:"));
panel1.add(new JLabel("开始时间:"));
panel1.add(new JLabel("运行时间:"));
panel1.add(new JPanel());
panel2.setLayout(new GridLayout(2, 2));
panel2.add(addButton);
panel2.add(runButton);
panel2.add(quantumLabel);
panel2.add(quantumTextField);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.SOUTH);
add(panel3, BorderLayout.CENTER);
// 显示窗口
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
TimeSliceRoundRobin tsrr = new TimeSliceRoundRobin();
}
private class Process {
private String name;
private int startTime;
private int runTime;
private int index;
public Process(String name, int startTime, int runTime) {
this.name = name;
this.startTime = startTime;
this.runTime = runTime;
this.index = model.getRowCount();
}
public String getName() {
return name;
}
public int getStartTime() {
return startTime;
}
public int getRunTime() {
return runTime;
}
public void setRunTime(int runTime) {
this.runTime = runTime;
}
public int getIndex() {
return index;
}
}
}
```
在窗口上方,有一个输入框,可以输入进程名、开始时间和运行时间,并且有一个“添加”按钮,可以将进程添加到进程队列中。在窗口下方,有一个输入框,可以输入时间片大小,并且有一个“运行”按钮,可以开始运行进程。窗口中央为一个表格,显示进程的名称、开始时间、运行时间和状态。当进程完成时,状态将更新为“已完成”。
该程序使用Java的Swing GUI库实现,可以在任何支持Java的操作系统上运行。
阅读全文