Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何减少AWT中的代码长度?_Java_User Interface_Button_Awt - Fatal编程技术网

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

Java 如何减少AWT中的代码长度?,java,user-interface,button,awt,Java,User Interface,Button,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,btn

我有一个关于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使用按钮数组(例如,
新按钮[4][4]
),循环,以及检查按钮行和列,然后更新文本字段的方法,可以最小化代码

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

1)为什么要使用AWT?有关放弃AWT组件而支持Swing的许多好理由,请参阅。2) 按钮1到16相对容易理解,最小化代码应该相对简单(查看使用
按钮[4][4]
),但是
生成
按钮的目的是什么?