package test;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.AncestorListener;
public class luyou1 extends JFrame{
JPanel row1 = new JPanel();
JLabel luyoubiao = new JLabel("路由表" , JLabel.CENTER);
JPanel row2 = new JPanel();
JLabel data1 = new JLabel("128.30.33.0" , JLabel.CENTER);
JLabel data2 = new JLabel("255.255.255.128" , JLabel.CENTER);
JLabel data3 = new JLabel("接口0" , JLabel.CENTER);
JLabel data4 = new JLabel("128.30.33.128" , JLabel.CENTER);
JLabel data5 = new JLabel("255.255.255.128" , JLabel.CENTER);
JLabel data6 = new JLabel("接口1" , JLabel.CENTER);
JLabel data7 = new JLabel("128.30.36.0" , JLabel.CENTER);
JLabel data8 = new JLabel("255.255.255.0" , JLabel.CENTER);
JLabel data9 = new JLabel("R2" , JLabel.CENTER);
JPanel row3 = new JPanel();
JLabel shuru = new JLabel("請輸入目的地址:" , JLabel.CENTER);
JTextField[] number = new JTextField[4];
JPanel row4 = new JPanel();
JButton chaxun = new JButton("查詢");
JLabel jieguo = new JLabel("查詢的結果為:" , JLabel.RIGHT);
JTextField[] shuchu= new JTextField[2];
public luyou1() throws HeadlessException{
super("模擬路由器查表");
setSize(700 ,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout gridLayout = new GridLayout(5, 1, 10, 10);
setLayout(gridLayout);
GridLayout gridLayout1 = new GridLayout(1, 7, 10,10);
row1.setLayout(gridLayout1);
row1.add(luyoubiao);
add(row1);
GridLayout gridLayout2 = new GridLayout(3, 7, 10,10);
row2.setLayout(gridLayout2);
row2.add(data1);
row2.add(data2);
row2.add(data3);
row2.add(data4);
row2.add(data5);
row2.add(data6);
row2.add(data7);
row2.add(data8);
row2.add(data9);
add(row2);
GridLayout gridLayout3 = new GridLayout(1,7, 10,10);
row3.setLayout(gridLayout3);
row3.add(shuru);
for (int i = 0; i <4; i++) {
number[i] = new JTextField();
row3.add(number[i]);
}
add(row3);
GridLayout gridLayout4 = new GridLayout(1,7, 10,10);
row4.setLayout(gridLayout4);
row4.add(chaxun);
row4.add(jieguo);
for (int i = 0; i <2; i++){
shuchu[i] = new JTextField();
row4.add(shuchu[i]);
}
setVisible(true);
add(row4);
}
public void actionPerformed(ActionEvent e){
int[][] a={{128,30,33,0},{128,30,33,128},{128,30,36,0}};
int[][] b={{255,255,255,128},{255,255,255,128},{255,255,255,0}};
int[] c={128,30,33,0};
int[] d={255,255,255,128};
int i,j;
int[] f=new int[7];
String ch;
if(e.getSource()==chaxun){
for(i=0;i<4;i++){
if((f[i]&d[i])!=c[i]) break;
}
if(i==4)
System.out.println("目的網絡地址:128.30.33.0");
else{
System.out.println("不是直接交付");
for(i=0,j=0;j<4;j++){
if((b[i][j]&f[j])!=a[i][j]) break;
}
if(j==4)
System.out.println("下一跳:接口0");
else{
if(j==4)
System.out.println("下一跳:接口1");
else{
for(i=1,j=0;j<4;j++){
if((b[i][j]&f[j])!=a[i][j]) break;
}
if(j==4)
System.out.println("下一跳:R2");
else
System.out.println("轉發分組出錯");
}
}
}
}
}
public static void main(String[] args) {
luyou1 luyou1=new luyou1();
}
}
給查詢按鈕加Action監聽,點擊的時候進行你想要的計算,把想要的結果用setText()方法賦給查詢結果的兩個文本框。