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

📄 graphicscahe.java

📁 主存--辅存模拟 主存--cash模拟
💻 JAVA
字号:
package cahe;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class GraphicsCahe extends JFrame
{
	
	private JTextArea textArea;
	private JTextField textField_18;
	private JTextField textField_17;
	private ButtonGroup buttonGroup_1 = new ButtonGroup();
	private ButtonGroup buttonGroup = new ButtonGroup();
	int				bigContent	= 32;	//大容量
	int				part			= 4;								//块大小
	int				smallContent		= 16;			//小容量
	int				team			= 2;								//组数
	Vector<Integer>	vAddress		= new Vector<Integer>();
	int            longAddress;
	int map =0;//映射方式
	int replace=0 ;//替换算法
	Cahe ca;
	int count=1;//时间顺序
	Vector<Vector>	in	= new Vector<Vector>();
	Vector<String>	inf		= new Vector<String>();
	Vector<String>	inf1	= new Vector<String>();
	final JLabel label_5 = new JLabel();
	final JLabel label = new JLabel();
	final JLabel label_1 = new JLabel();
	final JLabel label_2 = new JLabel();
	public static void main(String args[])
	{
		try
		{
			GraphicsCahe frame = new GraphicsCahe();
			frame.setVisible(true);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public GraphicsCahe()
	{
		super();
		setForeground(new Color(255, 0, 0));
		getContentPane().setLayout(null);
		setBounds(100, 100, 702, 590);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
		final JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		final JMenu menu = new JMenu();
		menu.setText("菜单");
		menuBar.add(menu);

		final JMenuItem menuItem = new JMenuItem();
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				label_5.setText("主存----辅存模拟器");
				label.setText("主存大小:");
				label_1.setText("辅存大小:");
				label_2.setText("页大小:");
			}
		});
		menu.add(menuItem);
		menuItem.setText("主存—辅存");

		final JMenuItem menuItem_1 = new JMenuItem();
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				label_5.setText("主存----Cahe模拟器");
				label.setText("Cahe大小:");
				label_1.setText("主存大小:");
				label_2.setText("块大小:");
			}
		});
		menuItem_1.setText("主存--Cahe");
		menu.add(menuItem_1);


		final JMenuItem menuItem_2 = new JMenuItem();
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		menuItem_2.setText("退出");
		menu.add(menuItem_2);

		final JMenu menu_1 = new JMenu();	
		menu_1.setText("版权");
		menuBar.add(menu_1);

		final JMenuItem menuItem_3 = new JMenuItem();
		menuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(GraphicsCahe.this,"学号:200610626\n 姓名:崔超");
			}
		});
		menuItem_3.setText("作者");
		menu_1.add(menuItem_3);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(11, 44, 625, 53);
		getContentPane().add(panel);

		
		label.setText("主存大小:");
		label.setBounds(10, 21, 76, 29);
		panel.add(label);

		final JTextField textField = new JTextField("16");
		textField.setBounds(92, 26, 49, 20);
		panel.add(textField);
          
		final JTextField textField_1 = new JTextField();
		textField_1.setText("32");
		textField_1.setBounds(254, 26, 49, 20);
		panel.add(textField_1);

		label_1.setText("辅存大小:");
		label_1.setBounds(177, 21, 71, 29);
		panel.add(label_1);

		final JTextField textField_2 = new JTextField();
		textField_2.setText("4");
		textField_2.setBounds(420, 26, 49, 20);
		panel.add(textField_2);


		label_2.setText("页大小:");
		label_2.setBounds(341, 21, 60, 29);
		panel.add(label_2);

		final JTextField textField_3 = new JTextField();
		textField_3.setText("2");
		textField_3.setBounds(554, 26, 49, 20);
		panel.add(textField_3);

		final JLabel label_3 = new JLabel();
		label_3.setText("组 数:");
		label_3.setBounds(488, 21, 60, 29);
		panel.add(label_3);

		final JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBounds(-1, 116, 615, 25);
		getContentPane().add(panel_1);

		final JLabel label_4 = new JLabel();
		label_4.setForeground(new Color(255, 0, 0));
		label_4.setBounds(15, 5, 118, 15);
		label_4.setText("1.请选择映射方式:");
		panel_1.add(label_4);

		final JRadioButton radioButton = new JRadioButton();
		radioButton.setSelected(true);
		radioButton.setBounds(151, 1, 102, 23);
		buttonGroup.add(radioButton);
		panel_1.add(radioButton);
		radioButton.setText("全相联映射");

		final JRadioButton radioButton_2 = new JRadioButton();
		radioButton_2.setBounds(401, 1, 100, 23);
		buttonGroup.add(radioButton_2);
		panel_1.add(radioButton_2);
		radioButton_2.setText("组相联映射");

		final JRadioButton radioButton_1 = new JRadioButton();
		radioButton_1.setBounds(282, 1, 91, 23);
		panel_1.add(radioButton_1);
		buttonGroup.add(radioButton_1);
		radioButton_1.setText("直接映射   ");

		final JPanel panel_1_1 = new JPanel();
		panel_1_1.setLayout(null);
		panel_1_1.setBounds(0, 147, 549, 25);
		getContentPane().add(panel_1_1);

		final JLabel label_4_1 = new JLabel();
		label_4_1.setForeground(new Color(255, 0, 0));
		label_4_1.setBounds(15, 5, 107, 15);
		label_4_1.setText("2.请选择替换算法");
		panel_1_1.add(label_4_1);

		final JRadioButton radioButton_2_1 = new JRadioButton();
		buttonGroup_1.add(radioButton_2_1);
		radioButton_2_1.setBounds(282, 1, 85, 23);
		radioButton_2_1.setText("LRU");
		panel_1_1.add(radioButton_2_1);

		final JRadioButton radioButton_1_1 = new JRadioButton();
		radioButton_1_1.setSelected(true);
		buttonGroup_1.add(radioButton_1_1);
		radioButton_1_1.setBounds(151, 1, 70, 23);
		panel_1_1.add(radioButton_1_1);
		radioButton_1_1.setText("FIFO");

		label_5.setForeground(new Color(0, 0, 255));
		label_5.setFont(new Font("", Font.BOLD, 36));
		label_5.setText("主存----辅存模拟器");
		label_5.setHorizontalTextPosition(SwingConstants.CENTER);
		label_5.setHorizontalAlignment(SwingConstants.CENTER);
		label_5.setBounds(83, 10, 466, 38);
		getContentPane().add(label_5);

		final JPanel panel_2 = new JPanel();
		panel_2.setLayout(new FlowLayout());
		panel_2.setBounds(11, 272, 479, 38);
		getContentPane().add(panel_2);
		
		final JTextField textFieldS[]=new JTextField[10];
		for(int j=0;j<10;j++)
		{
			textFieldS[j] = new JTextField(2);
			int a=(int)(8*Math.random());
			textFieldS[j].setText(""+a);
			panel_2.add(textFieldS[j]);
		}
		final JLabel label_10 = new JLabel();
		label_10.setText("       ");
		panel_2.add(label_10);

		final JButton button = new JButton();
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				smallContent=Integer.parseInt(textField.getText());
				bigContent=Integer.parseInt(textField_1.getText());
				part=Integer.parseInt(textField_2.getText());
				team=Integer.parseInt(textField_3.getText());
				vAddress.clear();
 				longAddress=Integer.parseInt(textField_17.getText());
				for(int j=0;j<longAddress;j++)
				{
					vAddress.add(Integer.parseInt(textFieldS[j].getText()));
				}
				for(int j=longAddress;j<10;j++)
				{
					textFieldS[j].setEditable(false);
				}
				ca=new Cahe(bigContent,smallContent,part,team,vAddress);
				selectCom(radioButton, radioButton_1, radioButton_2,
						radioButton_2_1, radioButton_1_1);//获取映射方式与替换算法
				in=ca.anylzeAddress(map,replace);
				inf=in.get(0);
				inf1=in.get(1);
				textArea.append("时间     状态            替换或调进\n");
				Enumeration<String> en=inf.elements();
				Enumeration<String> en1=inf1.elements();
				while(en.hasMoreElements())
				{
					if(count<10)
					{
					 textArea.append(count+++"  →    ");
					}
					else
					{
						textArea.append(count+++"→    ");
					}
					textArea.append(en1.nextElement());
					textArea.append("       "+en.nextElement());
					textArea.append("\n");
				}
				textField_18.setText(""+ca.targetRate);
				button.setEnabled(false);
			}

			private void selectCom(final JRadioButton radioButton,
					final JRadioButton radioButton_1,
					final JRadioButton radioButton_2,
					final JRadioButton radioButton_2_1,
					final JRadioButton radioButton_1_1)
			{
				if(radioButton.isSelected())
				{
					map=0;
				}
				else if(radioButton_1.isSelected())
				{
					map=1;
				}
				else if(radioButton_2.isSelected())
				{
					map=2;
				}
				if(radioButton_2_1.isSelected())
				{
					replace=0;
				}
				else if(radioButton_1_1.isSelected())
				{
					replace=1;
				}
			}
		});
		panel_2.add(button);
		button.setText("执行");

		final JButton button_1 = new JButton();
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				button.setEnabled(true);
				textArea.setText(" ");
				count=1;
				in.clear();
				inf.clear();
				inf1.clear();
				textField_18.setText(" ");
			}
		});
		button_1.setText("重执");
		panel_2.add(button_1);

		final JLabel label_6 = new JLabel();
		label_6.setForeground(new Color(255, 0, 0));
		label_6.setBounds(11, 241, 144, 25);
		getContentPane().add(label_6);
		label_6.setText("4.请输入地址流:");

		final JPanel panel_3 = new JPanel();
		panel_3.setLayout(null);
		panel_3.setBounds(5, 180, 420, 38);
		getContentPane().add(panel_3);

		textField_17 = new JTextField();
		textField_17.setText("10");
		textField_17.setBounds(205, 12, 95, 20);
		panel_3.add(textField_17);

		final JLabel label_7 = new JLabel();
		label_7.setForeground(new Color(255, 0, 0));
		label_7.setBounds(5, 10, 189, 23);
		panel_3.add(label_7);
		label_7.setText("3.请输入地址流的长度(<10):");

		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(59, 361, 505, 126);
		getContentPane().add(scrollPane);

		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);

		final JLabel label_8 = new JLabel();
		label_8.setForeground(new Color(255, 0, 0));
		label_8.setHorizontalTextPosition(SwingConstants.CENTER);
		label_8.setHorizontalAlignment(SwingConstants.LEFT);
		label_8.setText("5.                                         *********************分析过程**********************");
		label_8.setBounds(11, 316, 625, 39);
		getContentPane().add(label_8);

		final JPanel panel_4 = new JPanel();
		panel_4.setLayout(null);
		panel_4.setBounds(11, 493, 479, 32);
		getContentPane().add(panel_4);

		textField_18 = new JTextField();
		textField_18.setBounds(88, 5, 80, 20);
		panel_4.add(textField_18);
		textField_18.setEditable(false);

		final JLabel label_9 = new JLabel();
		label_9.setBounds(10, -1, 95, 30);
		panel_4.add(label_9);
		label_9.setForeground(new Color(255, 0, 0));
		label_9.setText("6. 命中率 H=");
		//
	}
}

⌨️ 快捷键说明

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