📄 main.java
字号:
package com.os.lyc;
import java.util.LinkedList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;
public class Main {
//***********系统变量*************//
private Table table;
private List list_jl;
private Text text_s;
public static int m_kind; //内存分配类型
private Memory memory= new Memory();
public static LinkedList<Process> lp = new LinkedList<Process>();
public static LinkedList<SubMemory> lm = new LinkedList<SubMemory>();
public static LinkedList<Label> lt = new LinkedList<Label>();
//****************************
private Text text_n;
private Combo combo_2;
protected Shell shell;
/**
* Launch the application
* @param args
*/
public Main(){
m_kind = 1;
}
public static void main(String[] args) {
try {
Main window = new Main();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(641, 425);
shell.setText("内存的动态分配模拟系统");
//text.setBackgroundImage(image)
final Group group = new Group(shell, SWT.NONE);
group.setText("分配模式");
group.setBounds(19, 14, 144, 96);
final Group group_3_1 = new Group(shell, SWT.NONE);
group_3_1.setText("操作记录");
group_3_1.setBounds(411, 229, 195, 159);
list_jl = new List(group_3_1, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
list_jl.setBounds(10, 23,175, 126);
final Button button = new Button(group, SWT.RADIO);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
m_kind=1;
memory.init();
combo_2.removeAll();
//**********想table写记录
write2table();
//绘制内存模拟状态
reflesh();
}
});
button.setSelection(true);
button.setText("首次适应算法");
button.setBounds(10, 22, 111, 16);
final Button button_1 = new Button(group, SWT.RADIO);
button_1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
m_kind=2;
memory.init();
combo_2.removeAll();
//**********想table写记录
write2table();
//绘制内存模拟状态
reflesh();
}
});
button_1.setText("循环首次适应算法");
button_1.setBounds(10, 44, 124, 16);
final Button button_2 = new Button(group, SWT.RADIO);
button_2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
m_kind=3;
memory.init();
combo_2.removeAll();
//**********想table写记录
write2table();
//绘制内存模拟状态
reflesh();
}
});
button_2.setText("最佳适应算法");
button_2.setBounds(10, 66, 111, 16);
final Group group_1 = new Group(shell, SWT.NONE);
group_1.setText("内存分配");
group_1.setBounds(187, 116, 199, 107);
final Label label_1 = new Label(group_1, SWT.NONE);
label_1.setText("申请内存大小");
label_1.setBounds(10, 44, 72, 20);
final Label label = new Label(shell, SWT.NONE);
label.setRedraw(true);
label.setBackground(SWTResourceManager.getColor(128, 128, 192));
label.setBounds(187, 21, 428, 51);
lt.add(label);
//combo_1.add("5");
final Button button_3 = new Button(group_1, SWT.NONE);
button_3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
int begin=0;
Process newp =new Process();
SubMemory newsubm = new SubMemory();
SubMemory tempsubm = new SubMemory();
//***************************
if(text_n.getText().equals("")){ //进程名不能为空
text_n.setFocus();
Error_M error = new Error_M(shell);
Error_M.kind=3;
error.open();
}
else if(issmart()){ //进程不能重名
text_n.setFocus();
Error_M error = new Error_M(shell);
Error_M.kind=4;
error.open();
}
else{
try{ //申请的内存大小必须为整数
int size = Integer.valueOf(text_s.getText()).intValue();
newsubm.setSize(size);
if(m_kind==1){ //首次适应算法
begin=memory.apply_first(Integer.valueOf(text_s.getText()).intValue());
if(begin==-1){
text_s.setFocus();
Error_M error = new Error_M(shell);
Error_M.kind=5;
error.open();
return;
}
else if(begin==-2){
memory.unit();
begin=memory.apply_first(Integer.valueOf(text_s.getText()).intValue());
}
newsubm.setBegin(begin);
}
else if(m_kind==2){
begin=memory.apply_circle(Integer.valueOf(text_s.getText()).intValue());
if(begin==-1){
text_s.setFocus();
Error_M error = new Error_M(shell);
Error_M.kind=5;
error.open();
return;
}
else if(begin==-2){
memory.unit();
begin=memory.apply_circle(Integer.valueOf(text_s.getText()).intValue());
}
newsubm.setBegin(begin);
}
else if(m_kind==3){
begin=memory.apply_best(Integer.valueOf(text_s.getText()).intValue());
if(begin==-1){
text_s.setFocus();
Error_M error = new Error_M(shell);
Error_M.kind=5;
error.open();
return;
}
else if(begin==-2){
memory.unit();
begin=memory.apply_best(Integer.valueOf(text_s.getText()).intValue());
}
newsubm.setBegin(begin);
}
lm.add(newsubm);
//**********添加进程链表
newp.setName(text_n.getText());
newp.setTake_m(newsubm);
lp.add(newp);
combo_2.add(text_n.getText());
/* //*************更改空闲内存连接
SubMemory tsml = new SubMemory();
tsml= (SubMemory)list.get(arg0)*/
//**************写记录
list_jl.add("创建进程 "+text_n.getText()+" 申请内存 "+size+"K 开始地址 "+begin+"");
//
text_n.setText("");
text_s.setText("");
//向table里写数据
write2table();
//绘制内存模拟状态
reflesh();
//测试代码
tempsubm=(SubMemory)lm.getLast();
System.out.println(tempsubm.getBegin());
System.out.println(tempsubm.getSize());
System.out.println();
//******************
}catch (Exception e1){
if(text_s.getText().equals("")){
text_s.setFocus();
Error_M error = new Error_M(shell);
Error_M.kind=1;
error.open();
}
else{
text_s.setFocus();
Error_M error = new Error_M(shell);
Error_M.kind=2;
error.open();
}
}
for(int i=0;i<214;i++)
System.out.print(memory.memory[i]);
System.out.println();
}
}
});
button_3.setText("执行");
button_3.setBounds(59, 67, 77, 30);
final Label kbLabel = new Label(group_1, SWT.NONE);
kbLabel.setText("KB");
kbLabel.setBounds(171, 44, 21, 20);
final Label label_3 = new Label(group_1, SWT.NONE);
label_3.setText("进程名");
label_3.setBounds(26, 20, 56, 18);
text_n = new Text(group_1, SWT.BORDER);
text_n.setBounds(88, 17, 77, 20);
text_s = new Text(group_1, SWT.BORDER);
text_s.setBounds(88, 41, 77, 20);
final Group group_3 = new Group(shell, SWT.NONE);
group_3.setText("空闲内存链表列表");
group_3.setBounds(187, 229, 199, 159);
GridData gd = new GridData();
gd.horizontalAlignment = SWT.FILL;
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
gd.verticalAlignment = SWT.FILL;
table = new Table(group_3, SWT.BORDER);
table.setRedraw(true);
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(14, 22, 175, 127);
table.setLayoutData(gd);
table.setLinesVisible(true);
String[] tableHeader = {"起始地址", "大 小" };
for(int i=0;i<tableHeader.length;i++){
TableColumn tableColumn = new TableColumn(table,SWT.NONE);
tableColumn.setText(tableHeader[i]);
}
//重新布局表格
for(int i=0;i<tableHeader.length;i++){
table.getColumn(i).pack();
}
final Group group_1_1 = new Group(shell, SWT.NONE);
group_1_1.setText("内存释放");
group_1_1.setBounds(411, 116, 199, 107);
final Label label_1_1 = new Label(group_1_1, SWT.NONE);
label_1_1.setBounds(10, 28, 84, 20);
label_1_1.setText("要释放的进程名");
combo_2 = new Combo(group_1_1, SWT.NONE);
combo_2.setBounds(100, 25, 77, 20);
final Button button_4 = new Button(group_1_1, SWT.NONE);
button_4.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
Process temp = new Process();
for(int i =0;i<lp.size();i++){
temp=(Process)lp.get(i);
if(temp.getName().equals(combo_2.getText())){
memory.back(temp.getTake_m().getBegin(), temp.getTake_m().getSize());
combo_2.remove(combo_2.getText());
list_jl.add("撤销进程 "+temp.getName()+" 释放内存 "+temp.getTake_m().getSize()+"K" +
"");
combo_2.setText("");
//**********想table写记录
write2table();
//绘制内存模拟状态
reflesh();
lp.remove(i);
lm.remove(i);
break;
}
}
}
});
button_4.setBounds(55, 65, 77, 30);
button_4.setText("执行");
final Label label_2 = new Label(shell, SWT.NONE);
label_2.setText("0 K");
label_2.setBounds(187, 78, 42, 20);
final Label label_2_1 = new Label(shell, SWT.NONE);
label_2_1.setBounds(379, 78, 42, 20);
label_2_1.setText("107K");
final Label label_2_2 = new Label(shell, SWT.NONE);
label_2_2.setBounds(591, 78, 42, 20);
label_2_2.setText("214K");
final Group group_4 = new Group(shell, SWT.NONE);
group_4.setText("开发者信息");
group_4.setBounds(19, 229, 144, 159);
final Label label_4 = new Label(group_4, SWT.NONE);
label_4.setFont(SWTResourceManager.getFont("", 11, SWT.BOLD));
label_4.setText(" 李远策");
label_4.setBounds(30, 15, 68, 21);
final Label label_5 = new Label(group_4, SWT.NONE);
label_5.setText("山东农业大学信息学院");
label_5.setBounds(9, 42, 122, 12);
final Label label_6 = new Label(group_4, SWT.NONE);
label_6.setText("QQ:417548204");
label_6.setBounds(10, 60, 79, 12);
final Label label_7 = new Label(group_4, SWT.WRAP);
label_7.setText("E-mail:liyuance87@163.com");
label_7.setBounds(10, 75, 122, 24);
final Label label_8 = new Label(group_4, SWT.WRAP);
label_8.setForeground(SWTResourceManager.getColor(128, 0, 0));
label_8.setText("只有那些躺在坑里、从不仰望高处的人,才会没有出头之日。");
label_8.setBounds(10, 106, 122, 43);
final Button button_zl = new Button(shell, SWT.NONE);
button_zl.setBounds(85, 130,77, 39);
button_zl.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
memory.unit();
list_jl.add("内存碎片整理");
//**********想table写记录
write2table();
//绘制内存模拟状态
reflesh();
}
});
button_zl.setText("内存碎片整理");
final Button button_cs = new Button(shell, SWT.NONE);
button_cs.setBounds(20, 130,66, 39);
button_cs.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
memory.init();
combo_2.removeAll();
list_jl.add("清空内存");
//**********想table写记录
write2table();
//绘制内存模拟状态
reflesh();
}
});
button_cs.setText("初始化内存");
final Button button_tc = new Button(shell, SWT.NONE);
button_tc.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
System.exit(0);
}
});
button_tc.setBounds(19, 175, 66, 39);
button_tc.setText("退出系统");
final Button button_qk = new Button(shell, SWT.NONE);
button_qk.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
list_jl.removeAll();
}
});
button_qk.setBounds(85, 175, 77, 39);
button_qk.setText("清空记录");
//
}
public boolean issmart(){
Process temp = new Process();
for(int i =0;i<lp.size();i++){
temp=(Process)lp.get(i);
if(temp.getName().equals(text_n.getText())){
return true;
}
}
return false;
}
public void write2table(){
//**********想table写记录
SubMemory tm;
table.removeAll();
for(int j=0;j<memory.list.size();j++){
tm=(SubMemory)memory.list.get(j);
TableItem item = new TableItem(table,SWT.NONE);
item.setText(new String[] {String.valueOf(tm.getBegin()),String.valueOf(tm.getSize())});
}
}
public void draw(PaintEvent e,Label label)
{
int heigh = label.getBounds().height;
for(int i=0;i<214;i++){
if(memory.memory[i]!=0){
e.gc.drawLine(2*i, 0, 2*i, heigh);
e.gc.drawLine(2*i+1, 0, 2*i+1, heigh);
}
}
}
public void reflesh(){
//绘制内存模拟状态
Label nlabel ;
nlabel =(Label)lt.get(0);
nlabel.dispose();
lt.clear();
final Label tlabel = new Label(shell, SWT.NONE);
tlabel.setRedraw(true);
tlabel.setBackground(SWTResourceManager.getColor(128, 128, 192));
tlabel.setBounds(187, 21, 428, 51);
tlabel.setRedraw(true);
lt.add(tlabel);
tlabel.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e){
draw(e,tlabel);
lt.add(tlabel);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -