package UI;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.util.*;
//继承ActionListener实现监听
public class Algorithm extends JPanel implements ActionListener {
class randomNumber {
int[] rand(int n, int length) {
int[] rand = new int[n];
for (int i = 0; i < n; i++) {
rand[i] = (int) (Math.random() * 1000 % length) + 1;//生成n个小于length的数
}
return rand;
}
}
//北、中、南三个面板
JPanel panel_north, panel_center, panel_south;
//面板中的面板,进行面板嵌套
JPanel panel_north_1, panel_north_2, panel_south_1, panel_south_2;
//五个按钮
JButton button_fifo, button_lru, button_opt, button_clock,button_res;
//文本区域
JTextField text_wulikuai, text_yemian, text_queyeshu, text_queyelv, text_zhihuanshu, text_zhihuanlv;
//算法过程输出区域
JTextArea textarea;
//随机序列显示区域
JTextArea textarea2;
//手动输入序列显示区域
JTextArea textarea3;
//文本区域的滚动条
JScrollPane scrollPane_1;//算法区滚动条
JScrollPane scrollPane_2;//随机序列滚动条
JScrollPane scrollPane_3;//指定序列的滚动条
//随机、手动序列单选按钮
private ButtonGroup buttonGroup = new ButtonGroup();
final JRadioButton radioButton1, radioButton2;
//随机数
randomNumber RN;
// 存储页面序列
String m_random = "";
//m 物理块数 n 页面总数
int m, n;
//物理块
int wuli[];
//页面存储序列
int xulie[];
//构造函数,存储缺页率结果
Double res[]=new Double[4];
public Algorithm() {
//初始化画板
panel_north = new JPanel();
panel_north_1 = new JPanel();
panel_north_2 = new JPanel();
//初始化画板中的画板
panel_south = new JPanel();
panel_south_1 = new JPanel();
panel_south_2 = new JPanel();
//初始化中心画板
panel_center = new JPanel();
//物理块文本
text_wulikuai = new JTextField(5);
//置换页面数文本
text_yemian = new JTextField(5);
//文本是否可编辑
text_queyeshu = new JTextField();
text_queyeshu.setEditable(false);
//文本是否可编辑
text_queyelv = new JTextField();
text_queyelv.setEditable(false);
//文本是否可编辑
text_zhihuanshu = new JTextField();
text_zhihuanshu.setEditable(false);
//文本是否可编辑
text_zhihuanlv = new JTextField();
text_zhihuanlv.setEditable(false);
//北部面板
//物理块面板
panel_north_1.add(new JLabel(" 请输入数据-> "));
panel_north_1.add(new JLabel(" 物理块数 "));
panel_north_1.add(text_wulikuai);
//键盘事件
text_wulikuai.addKeyListener(new NumListener());
//置换页面数面板
panel_north_1.add(new JLabel(" 置换页面数 "));
panel_north_1.add(text_yemian);
//添加键盘监听 只能输入数字和空格
text_yemian.addKeyListener(new NumListener());
panel_north_2.setLayout(new GridLayout(4, 1));
textarea2 = new JTextArea(1, 60);
textarea3 = new JTextArea(1, 70);
textarea2.setEditable(false);
textarea3.setEditable(false);
//输入内容自动换行
textarea2.setLineWrap(true);
//输入英文时不会将一个单词分在两行里
textarea2.setWrapStyleWord(true);
textarea3.setLineWrap(true);
textarea3.setWrapStyleWord(true);
textarea3.addKeyListener(new NumListener());
//文本区域应用滚动条
scrollPane_2 = new JScrollPane(textarea2);//
scrollPane_3 = new JScrollPane(textarea3);//
//radioButton1为随机页面序列
radioButton1 = new JRadioButton();
panel_north_2.add(radioButton1);
buttonGroup.add(radioButton1);
radioButton1.setText("随机页面序列");
radioButton1.setSelected(true);//初始化选中随机页面序列
//选择按钮触发事件
radioButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textarea3.setText("");
textarea3.setEditable(false);
}
});
panel_north_2.add(scrollPane_2);
radioButton2 = new JRadioButton();
panel_north_2.add(radioButton2);
buttonGroup.add(radioButton2);
radioButton2.setText("指定页面序列");
//键盘监听
radioButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textarea2.setText("");
textarea3.setEditable(true);
//把输入焦点放在调用这个方法的控件上
textarea3.requestFocus(true);
}
});
panel_north_2.add(scrollPane_3);
//GridLayout网状布局
panel_north.setLayout(new GridLayout(2, 2));
panel_north.add(panel_north_1);
panel_north.add(panel_north_2);
panel_center.setBorder(new TitledBorder(null, "页面置换过程", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
textarea = new JTextArea(20, 60);
textarea.setEditable(false);
//输入内容自动换行
textarea.setLineWrap(true);
//输入英文时不会将一个单词分在两行里
textarea.setWrapStyleWord(true);
scrollPane_1 = new JScrollPane(textarea);
panel_center.add(scrollPane_1);
//南部面板
panel_south_1.setBorder(new TitledBorder(null, "数据统计结果", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
panel_south_1.setLayout(new GridLayout(2, 2));
panel_south_1.add(new JLabel(" 缺页数"));
panel_south_1.add(text_queyeshu);
panel_south_1.add(new JLabel(" 缺页率"));
panel_south_1.add(text_queyelv);
panel_south_1.add(new JLabel(" 置换次数"));
panel_south_1.add(text_zhihuanshu);
panel_south_1.add(new JLabel(" 置换率"));
panel_south_1.add(text_zhihuanlv);
button_fifo = new JButton("FIFO");
button_lru = new JButton("LRU");
button_opt = new JButton("OPT");
button_clock = new JButton("CLOCK");
button_res=new JButton("结果比较");
button_fifo.addActionListener(this);
button_lru.addActionListener(this);
button_opt.addActionListener(this);
button_clock.addActionListener(this);
button_res.addActionListener(this);
panel_south_2.add(button_fifo);
panel_south_2.add(button_lru);
panel_south_2.add(button_opt);
panel_south_2.add(button_clock);
panel_south_2.add(button_res);
//设置布局
setLayout(new BorderLayout());
panel_south.setLayout(new GridLayout(2, 2));
panel_south.add(BorderLayout.NORTH, panel_south_1);
panel_south.add(BorderLayout.CENTER, panel_south_2);
//将三个面板添加到布局中
add(BorderLayout.NORTH, panel_north);
add(BorderLayout.CENTER, panel_center);
add(BorderLayout.SOUTH, panel_south);
}
//计算前检查输入并初始化相关数据,对随机序列和指定页面序列进�


wangfan_love
- 粉丝: 0
- 资源: 1