-1
我有一個關於Java的問題,即關於AWT。我剛開始學習並使用它。我寫了一個代碼,我需要儘可能地減少它。如何減少AWT中的代碼長度?
如何減少在AWT的代碼長度是多少?
這裏是我的按鈕1代碼
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Task extends Frame implements ActionListener{
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn_gen;
TextField tf1,tf2;
Label lb1,lb2;
Panel pn1,pn2,pn3;
public void init(){
btn1 = new Button("btn1");
btn1.addActionListener(this);
btn2 = new Button("btn2");
btn2.addActionListener(this);
btn3 = new Button("btn3");
btn3.addActionListener(this);
btn4 = new Button("btn4");
btn4.addActionListener(this);
btn5 = new Button("btn5");
btn5.addActionListener(this);
btn6 = new Button("btn6");
btn6.addActionListener(this);
btn7 = new Button("btn7");
btn7.addActionListener(this);
btn8 = new Button("btn8");
btn8.addActionListener(this);
btn9 = new Button("btn9");
btn9.addActionListener(this);
btn10 = new Button("btn10");
btn10.addActionListener(this);
btn11 = new Button("btn11");
btn11.addActionListener(this);
btn12= new Button("btn12");
btn12.addActionListener(this);
btn13 = new Button("btn13");
btn13.addActionListener(this);
btn14 = new Button("btn14");
btn14.addActionListener(this);
btn15 = new Button("btn15");
btn15.addActionListener(this);
btn16 = new Button("btn16");
btn16.addActionListener(this);
btn_gen = new Button("Generate");
btn_gen.addActionListener(this);
tf1 = new TextField(30);
tf1.setFocusable(false);
tf2 = new TextField(30);
tf2.setFocusable(false);
lb1 = new Label("row",Label.RIGHT);
lb2 = new Label("col",Label.RIGHT);
pn1 = new Panel();
pn1.setLayout(new GridLayout(2,2));
pn2 = new Panel();
pn2.setLayout(new GridLayout(4,4));
pn3 = new Panel();
pn3.setLayout(new GridLayout(1,1));
}
public Task(){
init();
setSize(400,400);
setLayout(new BorderLayout());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
pn1.add(lb1);
pn1.add(tf1);
pn1.add(lb2);
pn1.add(tf2);
pn2.add(btn1);
pn2.add(btn2);
pn2.add(btn3);
pn2.add(btn4);
pn2.add(btn5);
pn2.add(btn6);
pn2.add(btn7);
pn2.add(btn8);
pn2.add(btn9);
pn2.add(btn10);
pn2.add(btn11);
pn2.add(btn12);
pn2.add(btn13);
pn2.add(btn14);
pn2.add(btn15);
pn2.add(btn16);
pn3.add(btn_gen);
add(pn1,BorderLayout.NORTH);
add(pn2,BorderLayout.CENTER);
add(pn3,BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent ae){
if (ae.getSource()==btn1){
tf1.setText("1");
tf2.setText("1");
}else
if (ae.getSource()==btn2){
tf1.setText("1");
tf2.setText("2");
}else
if (ae.getSource()==btn3){
tf1.setText("1");
tf2.setText("3");
}else
if(ae.getSource()==btn4){
tf1.setText("1");
tf2.setText("4");
}else
if(ae.getSource()==btn5){
tf1.setText("2");
tf2.setText("1");
}else
if (ae.getSource()==btn6){
tf1.setText("2");
tf2.setText("2");
}else
if(ae.getSource()==btn7){
tf1.setText("2");
tf2.setText("3");
}else
if(ae.getSource()==btn8){
tf1.setText("2");
tf2.setText("4");
}else
if(ae.getSource()==btn9){
tf1.setText("3");
tf2.setText("1");
}else
if(ae.getSource()==btn10){
tf1.setText("3");
tf2.setText("2");
}else
if(ae.getSource()==btn11){
tf1.setText("3");
tf2.setText("3");
}else
if(ae.getSource()==btn12){
tf1.setText("3");
tf2.setText("4");
}else
if(ae.getSource()==btn13){
tf1.setText("4");
tf2.setText("1");
}else
if(ae.getSource()==btn14){
tf1.setText("4");
tf2.setText("2");
}else
if(ae.getSource()==btn15){
tf1.setText("4");
tf2.setText("3");
}else
if(ae.getSource()==btn16){
tf1.setText("4");
tf2.setText("4");
}else
if(ae.getSource()==btn_gen){
tf1.setText(String.valueOf((int)(Math.random()*4+1)));
tf2.setText(String.valueOf((int)(Math.random()*4+1)));
}
}
public static void main(String [] args){
Task ts = new Task();
ts.setVisible(true);
}
}
1)爲什麼使用AWT?看到[這個答案](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978)有很多很好的理由放棄AWT組件而轉向Swing。 2)從1到16的按鈕相對容易理解,並且最小化代碼應該相對簡單(使用'Button [4] [4]'查看),但是'Generate'按鈕的目的是什麼? –