繁体   English   中英

如何减少AWT中的代码长度?

[英]How to reduce the code length in AWT?

我有一个关于 Java 的问题,即关于 AWT。 我刚刚开始学习和使用它。 我已经写了一个代码,我需要尽可能地减少它。

如何减少AWT中的代码长度?

这是我的代码

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 到 16 使用按钮数组(例如new Button[4][4] )、循环和检查按钮的行和列,然后更新文本字段的方法,可以最小化代码。

事实上,快速检查表明可以将代码从 170 多行代码减少到 90 行以下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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