package math;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TFrame {

    public static void main(String[] args) {
       new MFrame().launchFrame();
      }
}

class MFrame extends JFrame{
    
    class MyMonitor implements ActionListener{
         int M;
         MyMonitor(int M)
        {
         this.M=M;
         }
        private  double arrayValue(double[][] matra)//求行列式的值
{
    int N=matra.length;
    int i=0,k=0,j=0,t=0;
    double result=1.0;
    for(k=0;k<N;k++)
{
if(matra[k][k]==0)
{
  for(i=k+1;i<N;i++){
   if(matra[i][k]!=0)
    {
      int index=i;
      t++;
        for(j=k;j<N;j++)
       {
     double tep=matra[k][j];
        matra[k][j]=matra[index][j];
        matra[index][j]=tep;
             }
        break;                        
         }
         }
        if(i==N)
    {
                        
    return 0;
        }
        }
                    
     for(i=k+1;i<N;i++){
      double factor=-matra[i][k]/matra[k][k];
       for( j=k;j<N;j++)
       matra[i][j]+=matra[k][j]*factor;
         }
         }
         for(i=0;i<N;i++)
         {
           result*=matra[i][i];
         }
           if(t%2!=0)
           result=-result;
          return result;
}
public void actionPerformed(ActionEvent e){
    //获取外部输入数据并存储
    //建立N*N数组
    int i=0,j=0,k=0;
    double[][] arrayList=new double[M][M];
    for( i=0;i<M;i++)
        for(j=0;j<M;j++)
            arrayList[i][j]=Double.parseDouble(jtfConstant[i][j].getText());
    //建立常量
    double[] contantList=new double[M];
    for(i=0;i<M;i++)
        contantList[i]=Double.parseDouble(jtfList[i].getText());
    //建立所得值
    double[] resultList=new double[M];
    double[][][] value=new double[M][M][M];
    for(i=0;i<M;i++)
        for(j=0;j<M;j++)
            for(k=0;k<M;k++)
            {
            if(k==i)
                value[i][j][k]=contantList[j];
            else
               value[i][j][k]=arrayList[j][k];
            }
    double temp=arrayValue(arrayList);
    if(temp==0){
    System.out.println("方程组无惟一解");
    }
    //方程求解
    else{
    for(i=0;i<M;i++)
      resultList[i]=arrayValue(value[i])/temp;
    }
    for(i=0;i<M;i++)
        jtfResult[i].setText(""+resultList[i]);

}
}
     

      JTextField[][] jtfConstant;
      JTextField[] jtfList;
      JTextField[] jtfResult;
      JLabel[][] labPlus;
      JLabel[] labEqual;
      JLabel[] resultLabEqual;
      JLabel[][] varSymbol;
      String[] strX;
      JPanel[] jp;//N+2个面板
      JButton []btn;
      JPanel totalContentPane;
      JButton[] bigButton;
      JPanel tempPane;
      JPanel[] gridPane;
      CardLayout card;
      GridLayout grid;
      int N=1;

public void launchFrame(){
    
    int i=0,j=0,k=0;
    totalContentPane=new JPanel();
    card=new CardLayout(5,5);
    totalContentPane.setLayout(card);
    //增加选择按钮
    bigButton=new JButton[7];
     for(i=0;i<7;i++)
        bigButton[i]=new JButton();
    
        bigButton[0].setText("一元一次");
        bigButton[1].setText("二元一次");
        bigButton[2].setText("三元一次");
        bigButton[3].setText("四元一次");
        bigButton[4].setText("五元一次");
        bigButton[5].setText("六元一次");
        bigButton[6].setText("七元一次");
        //设置空隙
        for(i=0;i<7;i++)
          bigButton[i].setMargin(new Insets(2,2,2,2));
        //把7个按钮放进tempPane面板中  
       tempPane=new JPanel(new FlowLayout());
       for(i=0;i<7;i++)
          tempPane.add(bigButton[i]);
       btn=new JButton[7];
       //把tempPane和totalContentPane放进主面板
       this.getContentPane().add(totalContentPane);
       this.getContentPane().add(tempPane,BorderLayout.NORTH);
    //构建cardLayout的7个不同面板并依次将这七个面板放到totalContentPane
       gridPane=new JPanel[7];
       for(i=0;i<7;i++)
           gridPane[i]=new JPanel();
       for( N=1;N<8;N++)
       {
       grid=new GridLayout(N+2,1);
       
       gridPane[N-1].setLayout(grid);
       //totalContentPane.add(gridPane[N-1]);
       jtfConstant=new JTextField[N][N];
       jtfList=new JTextField[N];
       jtfResult=new JTextField[N];
       labPlus=new JLabel[N][N-1];
       labEqual=new JLabel[N];
       resultLabEqual=new JLabel[N];
       varSymbol=new JLabel[N+1][N];
       strX=new String[N];
       jp=new JPanel[N+2];//
      
       //构建变量标签字符串
      for(i=0;i<N;i++){
        strX[i]=" X"+(i+1);
               }

       //构建系数矩阵
      for(i=0;i<N;i++)
         for(j=0;j<N;j++)
             jtfConstant[i][j]=new JTextField(5);

       //构建常量矩阵
         for(i=0;i<N;i++)
            jtfList[i]=new JTextField(5);

       //构建线性方程组的解矩阵
         for(i=0;i<N;i++)
            jtfResult[i]=new JTextField(5);

       //构建加号标签
         for(i=0;i<N;i++)
           for(j=0;j<N-1;j++)
              labPlus[i][j]=new JLabel("+");

       //构建方程组等号
        for(i=0;i<N;i++)
          labEqual[i]=new JLabel("=");

       //构建方程组解的等号
        for(i=0;i<N;i++)
          resultLabEqual[i]=new JLabel("=");

       //构建方程组变量X1,X2,X3....
        for(i=0;i<N+1;i++)
          for(j=0;j<N;j++)
           varSymbol[i][j]=new JLabel(strX[j]);

       //构建子面板jp[],设置为FlowLayout();
       for(i=0;i<N+2;i++){
         jp[i]=new JPanel();
          jp[i].setLayout(new FlowLayout());
               }
        //把子面板放到主面板中
       for(i=0;i<N+2;i++)
          gridPane[N-1].add(jp[i]);
      //构建方程组面板
      //构建N*(N-1)
     for(i=0;i<N;i++)
       for(j=0;j<N-1;j++)
        {
        jp[i].add(jtfConstant[i][j]);
        jp[i].add(varSymbol[i][j]);
        jp[i].add(labPlus[i][j]);
        }

      //构建N*1与常数项向量
       for(i=0;i<N;i++)
        {
           jp[i].add(jtfConstant[i][N-1]);
           jp[i].add(varSymbol[i][N-1]);
           jp[i].add(labEqual[i]);
           jp[i].add(jtfList[i]);
        }

       //构建方程组解数据列表
      for(i=0;i<N;i++)
       {
         jp[N].add(varSymbol[N][i]);
         jp[N].add(resultLabEqual[i]);
         jp[N].add(jtfResult[i]);
       }
       //增加一个“计算”按钮

       btn[N-1]=new JButton("计算");
       jp[N+1].add(btn[N-1]);
      btn[N-1].addActionListener(new MyMonitor(N));
      
      
    }

      //按钮与面板建立关系
      totalContentPane.add(gridPane[0],"p0");
      totalContentPane.add(gridPane[1],"p1");
      totalContentPane.add(gridPane[2],"p2");
      totalContentPane.add(gridPane[3],"p3");
      totalContentPane.add(gridPane[4],"p4");
      totalContentPane.add(gridPane[5],"p5");
      totalContentPane.add(gridPane[6],"p6");

      //建立bigButton的监听器
      //一元一次
      bigButton[0].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
       //new MFrame();
      card.show(totalContentPane, "p0");
      }
      });

      //二元一次
      bigButton[1].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
           
          card.show(totalContentPane, "p1");
        }
       });

      //三元一次
      bigButton[2].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          
          card.show(totalContentPane, "p2");
        }
        });

      //四元一次
      bigButton[3].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            
          card.show(totalContentPane, "p3");
        }
        });

      //五元一次
      bigButton[4].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
           
         card.show(totalContentPane, "p4");
        }
      });

      //六元一次
      bigButton[5].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            
          card.show(totalContentPane, "p5");
        }
        });

        //七元一次
      bigButton[6].addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            
          card.show(totalContentPane, "p6");
        }
        });

       this.getContentPane().add(totalContentPane);
       this.getContentPane().add(tempPane,BorderLayout.NORTH);
       pack();
       setLocation(300,300);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
        setResizable(false);
}  


}
这个是我用克拉默法则来银线性方程组,可以根据按钮来确定(一元到七元),编译时正确,但是在按下"计算"按钮,出现问题,不能求解.但是如果“计算”按钮的ActionListenner只有一个就能成功运行。但我在程序中定义了7个"计算"按钮的ActionListener,分别对应七种情况.
我想问下,是不是在这里的ActionListenner不能定义多个?谢谢解答.