简体   繁体   中英

How to reduce the code length in AWT?

I have a question about Java, namely about AWT. I've just started to learn and to use it. I have written a code and I need to minimize it as far as possible.

How to reduce the code length in AWT?

Here is my code

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);
  }
}

Minimizing the code is possible by using a button array (eg new Button[4][4] ) for buttons 1 through 16, loops, and a method to check the row and column of the button, then update the text fields.

In fact, a quick check indicates it is possible to reduce the code from over 170 lines of code to less than 90.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM