// Copyright 哲猫. /* 行列の積 */ /* */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Ex061022a extends JApplet implements ActionListener{ JTextField tf1[][] = new JTextField[3][3]; JTextField tf2[][] = new JTextField[3][3]; JButton bt; JLabel lb,lba,lbb; /* nMatrix インスタンスをinit()内で宣言しても、各要素は広域変数とはならない。 */ nMatrix m1 = new nMatrix(3,3); nMatrix m2 = new nMatrix(3,3); nMatrix m3 = new nMatrix(3,3); public void init(){ Container cp = getContentPane(); JPanel pl = new JPanel(); pl.setLayout(null); pl.setBackground(Color.white); lba = new JLabel("A="); lba.setBounds(5,5,25,20); pl.add(lba); lbb = new JLabel("B="); lbb.setBounds(230,5,25,20); pl.add(lbb); for(int i=0;i<3;i++) for(int j=0;j<3;j++){ tf1[i][j] = new JTextField(10); tf1[i][j].setText("0.0"); tf1[i][j].setBounds(30+60*j,5+30*i,50,20); pl.add(tf1[i][j]); tf2[i][j] = new JTextField(10); tf2[i][j].setText("0.0"); tf2[i][j].setBounds(260+60*j,5+30*i,50,20); pl.add(tf2[i][j]); } bt = new JButton("行列の積の計算"); bt.setBounds(30,100,180,20); bt.addActionListener(this); pl.add(bt); lb = new JLabel(""); lb.setBounds(30,120,400,150); pl.add(lb); cp.add(pl); } public void actionPerformed(ActionEvent event){ String s=new String(); double val; String str = new String(); if(event.getSource()==bt){ for(int i=0;i<3;i++) for(int j=0;j<3;j++){ s=tf1[i][j].getText(); val=Double.valueOf(s).doubleValue(); m1.set(i,j,val); s=tf2[i][j].getText(); val=Double.valueOf(s).doubleValue(); m2.set(i,j,val); } m3=m1.prod(m2); str="
"; for(int i=0;i<3;i++){ str+="
"; for(int j=0;j<3;j++){ str+=""+m3.get(i,j)+"  "; } str+="
"; } str+="
"; lb.setText(str); } } }