RT,隨便看看吧,很簡單的演算法,做得還有些不靈活,畢竟只是按實驗上的要求做的,大家可以多提提意見。謝謝
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Static_Storing{
public static int i=0;
public Static_Storing(){
JFrame app=new JFrame("頁式虛擬儲存 ");
Container c=app.getContentPane();
JLabel l1=new JLabel("頁號");
JLabel l2=new JLabel("標誌");
JLabel []l={new JLabel("0"),new JLabel("1"),new JLabel("2"),new JLabel("3"),new JLabel("4"),new JLabel("5"),new JLabel("6")};
JLabel l3=new JLabel("需要訪問的指令地址流:0,1,2,3,0,6,4,5,1,2,4,6");
final JLabel l4=new JLabel("點擊按鈕開始訪問!");
final int []visit={0,1,2,3,0,6,4,5,1,2,4,6};
JButton b=new JButton("訪問");
final int []usual={0,1,2,3};
final JTextField []t={new JTextField("1",10),new JTextField("1"),new JTextField("1"),new JTextField("1"),new JTextField("0"),new JTextField("0"),new JTextField("0")};
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(i>=visit.length)
l4.setText("指令流全部訪問完!結束進程!");
else
{
if(t[visit[i]].getText().equals("1"))
{
l4.setText("訪問的指令流"+visit[i]+"在主存中,直接存取");
int k=0;
int temp;
for(k=0;k<usual.length;k++)
if(visit[i]==usual[k])
break;
if(k==1)
{
temp=usual[0];
usual[0]=visit[1];
usual[1]=temp;
}
else
if(k==2)
{
temp=usual[0];
visit[2]=visit[1];
usual[0]=visit[2];
visit[1]=temp;
}
else
if(k==3)
{
temp=usual[0];
visit[3]=visit[2];
visit[2]=visit[1];
usual[0]=visit[2];
visit[1]=temp;
}
temp=usual[k];
usual[0]=visit[i];
}
else
{
l4.setText("訪問的指令流不在主存中,調進頁面"+visit[i]+",調出頁面"+usual[3]);
t[visit[i]].setText("1");
t[usual[3]].setText("0");
int temp;
temp=usual[2];
usual[2]=usual[1];
usual[1]=usual[0];
usual[0]=visit[i];
usual[3]=temp;
}
i++;
}
}
});
JPanel p1=new JPanel(new GridLayout(9,0));
p1.add(l1); p1.add(l2);
for(int k=0;k<t.length;k++)
{
p1.add(l[k]); p1.add(t[k]);
}
p1.add(l3);
p1.setSize(300,300);
c.add(p1); c.add(l3);
JPanel p2=new JPanel(new GridLayout(1,0));
p2.add(b); p2.add(l4);
p2.setSize(20,20);
c.add(p2);
c.setLayout(new FlowLayout(FlowLayout.CENTER));
app.setSize(450,350);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
public static void main(String []arg0){
Static_Storing a=new Static_Storing();
}
}