import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*主程序类*/
public class memory extends JFrame implements ActionListener{
JFrame set_frame,fr;
JButton jbutton1,jbutton2,jbutton3,jbutton4,jbutton5,jbutton6;
JLabel jLabel1,jLabel2,jLabel3,jLabel4,thread_label1,thread_label2,lab1;
JTextPane jTextPane1,jTextPane2,thread_text;
static JTable jTable;
JProgressBar jProgressBar;
JPanel pel;
final String[] table_title = {"分 区 号","大 小(K)","起 址(K)","状 态"};
static Object[][] table_data = new Object[15][4];
static int free_size = 1000,use_size = 0,num = 0,thread_num;
static boolean find_flag = true;
static threads[] thread_arry = new threads[15];
static memory_class[] memory_arry = new memory_class[15];
public memory(){
super("实验五:可变分区方式的存储器管理");
setSize(620,450);
setLayout(null);
setLocation(240,180);
setResizable(false);
jLabel1 = new JLabel("内存的使用情况列表:");
add(jLabel1);
jLabel1.setBounds(20,20,200,25);
jTable = new JTable(table_data, table_title);
jTable.setRowHeight(20);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane scrollPane = new JScrollPane(jTable,v,h);
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
add(cp);
cp.add(scrollPane, BorderLayout.CENTER);
cp.setBounds(20,50,370,220);
String str1,str2;
str1 = 0+" K";
str2 = 1000+" K";
jLabel2 = new JLabel("已用的内存空间:");
add(jLabel2);
jLabel2.setBounds(440,20,200,25);
jTextPane1 = new JTextPane();
add(jTextPane1);
jTextPane1.setBounds(440,55,100,30);
jTextPane1.setEditable(false);
jTextPane1.setText(str1);
jLabel3 = new JLabel("可用的内存空间:");
add(jLabel3);
jLabel3.setBounds(440,115,200,25);
jTextPane2 = new JTextPane();
add(jTextPane2);
jTextPane2.setBounds(440,150,100,30);
jTextPane2.setEditable(false);
jTextPane2.setText(str2);
jLabel4 = new JLabel("内存总的使用情况:");
add(jLabel4);
jLabel4.setBounds(440,210,200,25);
jProgressBar = new JProgressBar();
add(jProgressBar);
jProgressBar.setBackground(Color.white);
jProgressBar.setMaximum(1000);
jProgressBar.setStringPainted(true);
jProgressBar.setBounds(440,245,150,20);
jbutton1 = new JButton("创 建 进 程");
add(jbutton1);
jbutton1.addActionListener(this);
jbutton1.setBounds(50,310,120,30);
jbutton1.setCursor(new Cursor(12));
jbutton2 = new JButton("撤 消 进 程");
add(jbutton2);
jbutton2.addActionListener(this);
jbutton2.setBounds(250,310,120,30);
jbutton2.setCursor(new Cursor(12));
jbutton3 = new JButton("退 出 程 序");
add(jbutton3);
jbutton3.addActionListener(this);
jbutton3.setBounds(440,310,120,30);
jbutton3.setCursor(new Cursor(12));
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jbutton1)
{
if(free_size > 0){
add_thread();
if(thread_arry[num-1].thread_size <= free_size){
memory_class.find_free_memory();
if(find_flag == true){
set_table();
jTable.repaint();
free_size = 0;
use_size = 0;
set_text();
jProgressBar.setValue(use_size);
}
else if(find_flag == false){
notice("find");
}
}
else{
num--;
notice("free");
}
}
else{
notice("mem");
}
}
else if(e.getSource() == jbutton2)
{
destroy_thread_frame();
}
else if(e.getSource() == jbutton3)
{
System.exit(0);
}
else if(e.getSource() == jbutton4)
{
thread_num = Integer.parseInt(thread_text.getText());
destroy_thread();
set_frame.setVisible(false);
}
else if(e.getSource() == jbutton5)
{
set_frame.setVisible(false);
}
else if(e.getSource() == jbutton6)
{
fr.setVisible(false);
}
}
/*用于添加进程的方法*/
public void add_thread(){
thread_arry[num] = new threads();
memory_arry[num+1] = new memory_class();
num++;
}
/*设置文本框的值*/
public void set_text(){
String str1,str2;
for(int j = 0; j < num; j++){
if(memory_arry[j].use_flag == true){
use_size += memory_arry[j].size;
}
}
for(int i = 0; i <= num; i++){
if(memory_arry[i].use_flag == false){
free_size += memory_arry[i].size;
}
}
str1 = use_size+" K";
str2 = free_size+" K";
jTextPane1.setText(str1);
jTextPane2.setText(str2);
}
/*设置表格*/
public static void set_table(){
for(int j = 1; j <= num; j++){
int s = 0;
for(int n = 0; n < j; n++){
s += memory_arry[n].size;
}
memory_arry[j].start_address = s + 1;
}
for(int i = 0; i <= num; i++){
table_data[i][0] = memory_arry[i].mem_num;
table_data[i][1] = memory_arry[i].size;
table_data[i][2] = memory_arry[i].start_address;
if(memory.memory_arry[i].use_flag == true){
table_data[i][3] = memory_arry[i].thread_use+"号进程占用";
}
else if(memory_arry[i].use_flag == false){
table_data[i][3] = "可用";
}
jTable.repaint();
}
}
/*撤消进程的窗口*/
public void destroy_thread_frame(){
set_frame = new JFrame("撤 消 进 程");
set_frame.setSize(300,150);
set_frame.setLayout(null);
set_frame.setLocation(370,330);
set_frame.setResizable(false);
thread_label1 = new JLabel("请输入想要撤消的进程编号:");
set_frame.add(thread_label1);
thread_label1.setBounds(20,20,180,25);
thread_text = new JTextPane();
set_frame.add(thread_text);
thread_text.setBounds(200,20,30,20);
thread_label2 = new JLabel("号进程");
set_frame.add(thread_label2);
thread_label2.setBounds(230,20,150,25);
jbutton4 = new JButton("确 定");
set_frame.add(jbutton4);
jbutton4.addActionListener(this);
jbutton4.setBounds(50,70,80,30);
jbutton4.setCursor(new Cursor(12));
jbutton5 = new JButton("取 消");
set_frame.add(jbutton5);
jbutton5.addActionListener(this);
jbutton5.setBounds(160,70,80,30);
jbutton5.setCursor(new Cursor(12));
set_frame.setVisible(true);
}
/*撤消进程的方法*/
public void destroy_thread(){
for(int n = 0; n <= num; n++){
if(memory_arry[n].thread_use == thread_num){
if(memory_arry[n-1].use_flag == false){/*如果前面分区为空,与前面的分区合并*/
memory_arry[n-1].size += memory_arry[n].size;
int na = n;
for(int x = n; x < num; x++){
memory_arry[x] = memory_arry[x+1];
memory_arry[x].mem_num = na;
na++;
}
table_data[num][0] = "";
jTable.repaint();
}
else if(memory_arry[n+1].use_flag == false){/*如果后面分区为空,与后面的分区合并*/
memory_arry[n].size += memory_arry[n+1].size;
int na = n + 1;
for(int x = n+1; x < num; x++){
memory_arry[x]= memory_arry[x+1];
memory_arry[x].mem_num = na;
na++;
}
}
else if(memory_arry[n+1].use_flag == false && memory_arry[n-1].use_flag == false){/*如果前后的分区都为空,与前后的分区合并*/
memory_arry[n-1].size = memory_arry[n-1].size + memory_arry[n].size + memory_arry[n+1].size;
int na = n;
for(int x = n; x < num; x++){
memory_arry[x]= memory_arry[x+2];
memory_arry[x].mem_num = na;
na++;