⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diskmanager.java

📁 操作系统课程设计之磁盘管理模拟,采用位视图算法(JAVA版)
💻 JAVA
字号:
/**
 * DiskManager类
 * 带main()入口方法
 * @author linpeitian
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DiskManager extends JFrame implements ActionListener{

	private JLabel title = null;
	
	private JPanel center = null;
	private JButton button[] = new JButton[64]; //磁盘块池模拟
	
	private JPanel applyPanel = null;
	private JButton apply = null;         //申请磁盘地址按钮
	private JTextField applyField = null; //磁盘申请块数输入域
	private int number = 0;
	
	private JPanel releasePanel = null;
	private JButton release = null;     //释放磁盘地址按钮
	private JTextField cid = null;   //C地址输入域
	private JTextField hid = null;   //H地址输入域
	private JTextField sid = null;   //S地址输入域
	private int cnum = 0;          //C地址
	private int hnum = 0;          //H地址
	private int snum = 0;          //S地址
	
	private JTextArea area = null;   //动态分配显示
	
	private JSeparator separator = null; 
	
	private int resource = 64;       //记录可以磁盘块数目
	
	/**
	 * 构造函数
	 * DiskManager
	 */
	public DiskManager(){
		setTitle("磁盘管理模拟");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//具体布局 GridBagLayout布局方式
		GridBagConstraints cst = null;
		GridBagLayout layout = new GridBagLayout();
		getContentPane().setLayout(layout);
		
		title = new JLabel("OS磁盘管理模拟小程序");
		cst = new GridBagConstraints();
		cst.anchor = GridBagConstraints.CENTER;
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(title, cst);
		getContentPane().add(title);
		
		center = new JPanel();
		center.setLayout(new BorderLayout());
		JPanel up = new JPanel();
		JLabel track = new JLabel("磁道数:-----磁道0-----||-----磁道1-----");
		up.add(track);
		JPanel left = new JPanel();
		left.setLayout(new GridLayout(8, 1));
		for(int i = 0; i < 8; i++){
			String str = "第" + i + "字节";
			left.add(new JLabel(str));
		}
		JPanel right = new JPanel();
		right.setLayout(new GridLayout(8, 8));
		for(int i = 0; i < 64; i++){
			button[i] = new JButton("0");
			right.add(button[i]);
		}
		center.add(up, BorderLayout.NORTH);
		center.add(left, BorderLayout.WEST);
		center.add(right, BorderLayout.CENTER);
		cst = new GridBagConstraints();
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(center, cst);
		getContentPane().add(center);
		
		separator = new JSeparator(); 
		cst = new GridBagConstraints();
		cst.fill = GridBagConstraints.HORIZONTAL;
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(separator, cst);
		getContentPane().add(separator);

		applyPanel = new JPanel();
		JLabel applyLabel = new JLabel("申请磁盘空间块数:");
		applyField = new JTextField(20);
		apply = new JButton("申请");
		apply.addActionListener(this);
		applyPanel.add(applyLabel);
		applyPanel.add(applyField);
		applyPanel.add(apply);
		cst = new GridBagConstraints();
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(applyPanel, cst);
		getContentPane().add(applyPanel);
		
		releasePanel = new JPanel();
		JLabel cidLabel = new JLabel("柱面号:");
		cid = new JTextField(6);
		JLabel hidLabel = new JLabel("磁道号:");
		hid = new JTextField(6);
		JLabel sidLabel = new JLabel("块号:");
		sid = new JTextField(6);
		release = new JButton("释放");
		release.addActionListener(this);
		releasePanel.add(cidLabel);
		releasePanel.add(cid);
		releasePanel.add(hidLabel);
		releasePanel.add(hid);
		releasePanel.add(sidLabel);
		releasePanel.add(sid);
		releasePanel.add(release);
		cst = new GridBagConstraints();
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(releasePanel, cst);
		getContentPane().add(releasePanel);
		
		area = new JTextArea(5,30);
		area.setEditable(false);
		JScrollPane scroll = new JScrollPane(area);	
		cst = new GridBagConstraints();
		cst.gridwidth = GridBagConstraints.REMAINDER;
		cst.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(scroll, cst);
		getContentPane().add(scroll);
		
		setSize(450, 580);
		setResizable(false);
		setVisible(true);
	}
	
	/**
	 * 事件相应处理函数
	 * 实现ActionListener接口
	 */
	public void actionPerformed(ActionEvent e){
		Object oj = e.getSource();
		if(oj == apply){
			//申请磁盘空间事件
			if(applyField.getText().trim().equals("")){
				//申请磁盘空间为空处理
				JOptionPane.showMessageDialog(this,
						"申请资源块数为空",
						"错误",
						JOptionPane.ERROR_MESSAGE);
			}else{
				try{
					number = Integer.parseInt(applyField.getText());
					if(number > resource || number < 0){
						//申请磁盘空间数目过大或为负数
						JOptionPane.showMessageDialog(this,
								"申请资源超过系统提供资源数或者为负",
								"错误",
								JOptionPane.ERROR_MESSAGE);
					}else{
						//合法申请,应答申请
						area.setText("");
						area.setText("》-应答:(资源申请) 用户提出" + number + "块资源申请\n");
						for(int i = 0; i < 64 && number != 0; i++){
							if(button[i].getText().equals("0")){
								button[i].setText("1");
								area.append("第" + i/8 + "柱面 - 第" + (i%8)/4 + "磁道 - 第" + (i%8)%4 + "记录块,提供供用\n");
								resource --;
								number --;
							}
						}
					}
				}catch(NumberFormatException ee){
					//输入申请磁盘空间格式错误
					JOptionPane.showMessageDialog(this,
							"申请资源数非法",
							"错误",
							JOptionPane.ERROR_MESSAGE);					
				}
			}
			applyField.setText("");
		}else if(oj == release){
			//释放磁盘空间事件
			if(cid.getText().trim().equals("")||hid.getText().trim().equals("")||sid.getText().trim().equals("")){
				//释放地址为空处理
				JOptionPane.showMessageDialog(this,
						"释放资源CHS地址不全",
						"错误",
						JOptionPane.ERROR_MESSAGE);
			}else{
				try{
					cnum = Integer.parseInt(cid.getText());
					hnum = Integer.parseInt(hid.getText());
					snum = Integer.parseInt(sid.getText());
					if(cnum < 0 || cnum > 7 || hnum > 1 || hnum < 0 ||snum > 3 || snum < 0){
						//释放地址逻辑错误,如越界
						JOptionPane.showMessageDialog(this,
								"输入的物理地址有误,出现越界",
								"错误",
								JOptionPane.ERROR_MESSAGE);
					}else{
						//合法,释放相应的磁盘地址块
						area.setText("");
						area.setText("》-应答:(资源释放) 用户提出释放某一物理地址块的资源\n");
						int i = 8*cnum + 4*hnum + snum;
						if(button[i].getText().equals("0"))
							JOptionPane.showMessageDialog(this,
									"物理地址已经空闲,无需释放",
									"错误",
									JOptionPane.ERROR_MESSAGE);
						else{
							button[i].setText("0");
							area.append("第" + cnum + "柱面 - 第" + hnum + "磁道 - 第" + snum + "记录块,释放待用\n");
							resource ++;
						}
					}
				}catch(NumberFormatException ee){
					//输入释放磁盘空间地址格式错误
					JOptionPane.showMessageDialog(this,
							"申请资源数非法",
							"错误",
							JOptionPane.ERROR_MESSAGE);					
				}
			}
			cid.setText("");
			hid.setText("");
			sid.setText("");
		}
	}
	
	/**
	 * 程序入口方法main
	 * @param args
	 */
	public static void main(String[] args){
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		new DiskManager();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -