- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryGUI.java
More file actions
Latest commit
120 lines (103 loc) · 4.71 KB
/
Copy pathMemoryGUI.java
File metadata and controls
120 lines (103 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
importjava.awt.*;
importjavax.swing.*;
publicclassMemoryGUI {
privatestaticMemoryManagermemoryManager;
privatestaticJFrameframe;
privatestaticJTextPanestatusArea;
privatestaticJTextFieldprocessIdField;
privatestaticJTextFieldsizeField;
privatestaticJTextFieldstrategyField;
publicstaticvoidmain(String[] args) {
SwingUtilities.invokeLater(MemoryGUI::createAndShowGUI);
}
privatestaticvoidcreateAndShowGUI() {
frame = newJFrame("Memory Simulation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 600);
frame.setLayout(newBorderLayout());
JPanelinputPanel = newJPanel();
inputPanel.setLayout(newGridLayout(6, 2, 10, 10));
JLabeltotalSizeLabel = newJLabel("Total memory size:");
JTextFieldtotalSizeField = newJTextField();
JButtoninitializeButton = newJButton("Initialize Memory");
inputPanel.add(totalSizeLabel);
inputPanel.add(totalSizeField);
inputPanel.add(newJLabel("Process ID:"));
processIdField = newJTextField();
inputPanel.add(processIdField);
inputPanel.add(newJLabel("Memory Size:"));
sizeField = newJTextField();
inputPanel.add(sizeField);
inputPanel.add(newJLabel("Strategy (first, best, worst):"));
strategyField = newJTextField();
inputPanel.add(strategyField);
inputPanel.add(initializeButton);
frame.add(inputPanel, BorderLayout.NORTH);
statusArea = newJTextPane(); // JTextPane supports HTML content
statusArea.setContentType("text/html");
statusArea.setEditable(false);
JScrollPanescrollPane = newJScrollPane(statusArea);
frame.add(scrollPane, BorderLayout.CENTER);
initializeButton.addActionListener(e -> {
try {
inttotalSize = Integer.parseInt(totalSizeField.getText());
memoryManager = newMemoryManager(totalSize);
displayMessage("<b>Memory initialized with size " + totalSize + " units.</b>");
} catch (NumberFormatExceptionex) {
displayMessage("<font color='red'>Invalid total memory size.</font>");
}
});
JButtonallocateButton = newJButton("Allocate Memory");
JButtondeallocateButton = newJButton("Deallocate Memory");
JButtondisplayButton = newJButton("Display Memory");
JPanelbuttonPanel = newJPanel();
buttonPanel.add(allocateButton);
buttonPanel.add(deallocateButton);
buttonPanel.add(displayButton);
frame.add(buttonPanel, BorderLayout.SOUTH);
allocateButton.addActionListener(e -> {
if (memoryManager == null) {
displayMessage("<font color='red'>Memory not initialized.</font>");
return;
}
try {
intprocessId = Integer.parseInt(processIdField.getText());
intsize = Integer.parseInt(sizeField.getText());
Stringstrategy = strategyField.getText();
booleansuccess = memoryManager.allocateMemory(processId, size, strategy);
if (success) {
displayMessage("<font color='green'>Memory allocated successfully.</font>");
} else {
displayMessage("<font color='red'>Memory allocation failed. Ensure sufficient memory and valid input.</font>");
}
} catch (NumberFormatExceptionex) {
displayMessage("<font color='red'>Invalid input for process ID or size.</font>");
}
});
deallocateButton.addActionListener(e -> {
if (memoryManager == null) {
displayMessage("<font color='red'>Memory not initialized.</font>");
return;
}
try {
intprocessId = Integer.parseInt(processIdField.getText());
memoryManager.deallocateMemory(processId);
displayMessage("<font color='green'>Memory deallocated successfully.</font>");
} catch (NumberFormatExceptionex) {
displayMessage("<font color='red'>Invalid process ID.</font>");
}
});
displayButton.addActionListener(e -> {
if (memoryManager == null) {
displayMessage("<font color='red'>Memory not initialized.</font>");
return;
}
Stringstatus = memoryManager.getMemoryStatus();
statusArea.setText(status);
});
frame.setVisible(true);
}
privatestaticvoiddisplayMessage(Stringmessage) {
statusArea.setText("<html><body>" + message + "</body></html>");
}
}