?? lru.java
字號:
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Lru extends JFrame implements ActionListener{
JFrame frame1,frame2,frame3;
JTextField lengthTextfield,physicalTextField,rangTextfield,handTextField;
JTextField lackCount,lackPercent;
int length=0,physical,rang;
JButton sure1,sure2,show,sure3;
JTextField dataJTextField;
JRadioButton FifoButton,LruButton,RandomButton,HandButton;
int pageLength[],queue[];
int dataWay=0;
int send[];
int way=1;
Vector vector;
String randomString=" ";
File dataFile;
JFileChooser chooser;
Vector fifoVector,lruVector;
float lackTime=0;
float lackTime1=0;
JScrollPane showPanelRight;
public Lru()
{
//frame1
new WelcomeWindow("E:/myjava/3.JPG",10000000);
fifoVector=new Vector();
lruVector=new Vector();
frame1=new JFrame("物理塊/頁面長度");
Container c=frame1.getContentPane();
JPanel panel2=new JPanel();
RandomButton=new JRadioButton("Random",false);
RandomButton.addActionListener(this);
HandButton=new JRadioButton("File",false);
HandButton.addActionListener(this);
ButtonGroup RH=new ButtonGroup();
RH.add(RandomButton);
RH.add(HandButton);
panel2.add(RandomButton);
panel2.add(HandButton);
JLabel l1=new JLabel("隨機范圍:",SwingConstants.RIGHT);
panel2.add(l1);
rangTextfield=new JTextField(5);
rangTextfield.setForeground(Color.red);
panel2.add(rangTextfield);
panel2.add(new JLabel("物理塊數 "));
physicalTextField=new JTextField(5);
physicalTextField.setForeground(Color.red);
panel2.add(physicalTextField);
panel2.add(new JLabel("頁面長度 "));
lengthTextfield=new JTextField(5);
lengthTextfield.setForeground(Color.red);
panel2.add(lengthTextfield);
sure1=new JButton("確定");
sure1.addActionListener(this);
panel2.add(sure1);
c.add(panel2);
Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
frame1.setSize(200,180);
frame1.setLocation( screenSize.width/2-(100),screenSize.height/2-(90));
//creat farme2
frame2=new JFrame("main frame");
Container b=frame2.getContentPane();
b.setLayout(new GridLayout(3,1));
JPanel topPanel1=new JPanel(new FlowLayout());
FifoButton=new JRadioButton("F I F O",false);
FifoButton.setFont(new Font("TimeRoman",Font.ITALIC,25));
FifoButton.setForeground(Color.gray);
FifoButton.addActionListener(this);
LruButton=new JRadioButton("L R U",false);
LruButton.setForeground(Color.gray);
LruButton.setFont(new Font("TimeRoman",Font.ITALIC,25));
LruButton.addActionListener(this);
Icon icon = new ImageIcon("E:/myjava/logo.JPG");
JLabel logoLabel=new JLabel(icon);
logoLabel.setForeground(Color.RED);
logoLabel.setFont(new Font("TimeRoman",Font.ITALIC,30));
topPanel1.add(logoLabel);
ButtonGroup FL=new ButtonGroup();
FL.add(FifoButton);
FL.add(LruButton);
topPanel1.add(FifoButton);
topPanel1.add(new JLabel(" "));
topPanel1.add(LruButton);
JPanel topPanel2=new JPanel(new FlowLayout());
JLabel dataLabel=new JLabel("D A T A: ",SwingConstants.CENTER);
dataLabel.setFont(new Font("TimeRoman",Font.ITALIC,20));
dataLabel.setForeground(Color.gray);
topPanel2.add(dataLabel);
dataJTextField=new JTextField(25);
dataJTextField.setFont(new Font("TimeRoman",Font.ITALIC,17));
dataJTextField.setForeground(Color.red);
topPanel2.add(dataJTextField);
topPanel2.add(new JLabel("缺 頁 次 數:"));
lackCount=new JTextField(5);
lackCount.setForeground(Color.red);
topPanel2.add(lackCount);
topPanel2.add(new JLabel("缺 頁 率 :"));
lackPercent=new JTextField(5);
lackPercent.setForeground(Color.red);
topPanel2.add(lackPercent);
b.add(topPanel1);
b.add(topPanel2);
//creat centerpanel
JPanel centerPanel1=new JPanel(new FlowLayout());
sure2=new JButton("確定");
sure2.addActionListener(this);
centerPanel1.add(sure2);
show=new JButton("顯示");
show.addActionListener(this);
centerPanel1.add(show);
b.add(centerPanel1);
frame2.setSize(300,400);
frame2.setLocation(screenSize.width/2-(150),screenSize.height/2-(160));
//frame3
sure3=new JButton("確定");
sure3.addActionListener(this);
frame3=new JFrame("內存塊:");
frame3.setSize(200,240);
frame3.setLocation(screenSize.width/2-(100),screenSize.height/2-(120));
}
/*****************************************/
class WelcomeWindow extends JWindow{//welcome window.
public WelcomeWindow(String filename,int waitTime){
//super(f);
JLabel l = new JLabel(new ImageIcon(filename));//image add to label l
getContentPane().add(l, BorderLayout.CENTER);
pack();
Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.width/2 - (labelSize.width/2),screenSize.height/2 - (labelSize.height/2));
//press the welcome window,it will be closed.
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
setVisible(false);
dispose();
frame1.setVisible(true);
}
});
final int pause = waitTime;
final Runnable closerRunner = new Runnable(){
public void run(){
setVisible(false);
dispose();
}
};
Runnable waitRunner = new Runnable(){
public void run(){
try{
Thread.sleep(pause);
//invoke closerRunner and wait for waitRunner run.
SwingUtilities.invokeAndWait(closerRunner);
}catch(Exception e){
e.printStackTrace();
// Catch InvocationTargetException
// Catch InterruptedException
}
}
};
setVisible(true);
Thread waitThread = new Thread(waitRunner, "SplashThread");
waitThread.start();
}
}
public static void main(String[] args)
{
new Lru();
}
public void Random(int k)
{
Random r=new Random();
for(int j=0;j<length;j++)
{
pageLength[j]=r.nextInt(k+1);
}
}
public void getDataString()
{
for(int j=0;j<length;j++)
{
randomString=randomString+pageLength[j]+" ";
System.out.println(randomString);
}
}
public int Compare(int a[],int b)
{
int amount=0;
for(int i=0;i<a.length;i++)
{
if(b==a[i])
{
amount=1;
break;
}
}
return amount;
}
public String getString(int a[])
{
String string=" ";
for(int i=0;i<a.length;i++)
{
string=string+a[i]+" ";
}
return string;
}
public void nizhi(int a[])
{
int temp;
for(int i=0,j=a.length-1;i<j;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
public void doLru()
{
for(int i=0;i<physical;i++)
{
queue[i]=pageLength[i];
}
nizhi(queue);
for(int i=0;i<physical;i++)
{
System.out.println(queue[i]);
}
/***********************************/
for(int j=physical;j<length;j++)
{
if(Compare(queue, pageLength[j])==0)
{
deal(queue,pageLength[j]);
lackTime1++;
lruVector.addElement(getString(queue));
}
if(Compare(queue, pageLength[j])==1)
{
int position=position(queue,pageLength[j]);
if(position>0)
{
for(int i=position;i>0;i--)
{
queue[i]=queue[i-1];
}
queue[0]=pageLength[j];
//lruVector.addElement(queue);
}
}
}
/***********************************/
lackCount.setText(lackTime1+"");
lackPercent.setText((lackTime1)/length+"");
}
public int position(int a[],int b)
{
int amount=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==b)
amount=i;
}
return amount;
}
public void deal(int a[],int b)
{
for(int i=a.length-1;i>=1;i--)
{
a[i]=a[i-1];
}
a[0]=b;
}
public void doFifo()
{
int a=0;
for(int i=0;i<physical;i++)
{
queue[i]=pageLength[i];
}
for(int j=physical;j<length;j++)
{
if(Compare(queue, pageLength[j])==0)
{
a=a%physical;
queue[a]=pageLength[j];
lackTime++;
a++;
fifoVector.add(getString(queue));
}
}
lackCount.setText(lackTime+"");
lackPercent.setText((lackTime)/length+"");
}
public void readFromFileGetlength()
{
String string;
chooser=new JFileChooser("E:/eclips");
chooser.showOpenDialog(frame2);
dataFile=chooser.getSelectedFile();
if(dataFile.length()!=0)
{
try
{
BufferedReader in=new BufferedReader(new FileReader(dataFile));
do
{
string=in.readLine();
length++;
}while(string!=null);
}
catch(Exception e)
{
}
}
length--;
}
public void readData()
{
String string;
int i=0;
try
{
BufferedReader in=new BufferedReader(new FileReader(dataFile));
do
{
string=in.readLine();
pageLength[i]=Integer.parseInt(string);
i++;
}while(string.equals("")==false);
}
catch(Exception e)
{
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==sure1)
{
if(dataWay==1)
{
length=Integer.parseInt(lengthTextfield.getText());
rang=Integer.parseInt(rangTextfield.getText());
}
if(dataWay==2)
{
//readFromFileGetlength();
}
physical=Integer.parseInt(physicalTextField.getText());
pageLength=new int[length];
System.out.println("length為:"+length);
if(dataWay==1)
{
Random(rang);
}
if(dataWay==2)
{
readData();
}
getDataString();
dataJTextField.setText(randomString);
queue=new int[physical];
frame1.setVisible(false);
frame1.dispose();
frame2.setVisible(true);
}
if(e.getSource()==FifoButton)
{
way=1;
}
if(e.getSource()==LruButton)
{
way=2;
}
if(e.getSource()==RandomButton)
{
dataWay=1;
}
if(e.getSource()==HandButton)
{
dataWay=2;
readFromFileGetlength();
lengthTextfield.setEditable(false);
lengthTextfield.setText(length+"");
rangTextfield.setEditable(false);
}
if(e.getSource()==sure2)
{
if(way==1)
{
doFifo();
for(int i=0;i<fifoVector.size();i++)
{
System.out.print(fifoVector.elementAt(i));
}
}
if(way==2)
{
doLru();
}
}
if(e.getSource()==show)
{
frame2.setVisible(false);
frame2.dispose();
if(way==1)
{
JList list=new JList(fifoVector);
JLabel l=new JLabel("內存塊情況:",SwingConstants.CENTER);
showPanelRight=new JScrollPane(list);
list.setForeground(Color.red);
Container c=frame3.getContentPane();
c.setLayout(new FlowLayout());
c.add(l);
c.add(showPanelRight);
c.add(sure3);
frame3.setVisible(true);
show.removeActionListener(this);
}
if(way==2)
{
JList list=new JList(lruVector);
JLabel l=new JLabel("內存塊情況:",SwingConstants.CENTER);
showPanelRight=new JScrollPane(list);
list.setForeground(Color.red);
Container c=frame3.getContentPane();
c.setLayout(new FlowLayout());
c.add(l);
c.add(showPanelRight);
c.add(sure3);
frame3.setVisible(true);
show.removeActionListener(this);
}
}
if(e.getSource()==sure3)
{
frame3.setVisible(false);
frame3.dispose();
System.exit(0);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -