Witam, mam taki problem z programem. Mam napisać program obliczający odwrotność macierzy 3x3 z interfejsem graficznym. Interfejs jako taki jest i trochę programu, ale nie mogę sobie poradzić z wypisywaniem wyniku w 9 pól po naciśnięciu buttona. Mniej więcej wiem jak to zrobić tylko że program nie przyjmuje mi kolejnej zmiennej. Załączam kod, proszę o odpowiedź.
public class MainKalkulator {
public static void main(String args[])
{
Kalkulator MojKalkulator = new Kalkulator();
// wymiar okna kalkulatora
MojKalkulator.setSize(500,320);
// uruchomienie okna
MojKalkulator.show();
}
}
i sam kalkulator
import java.awt.*;
import java.awt.event.*;
public class Kalkulator extends Frame implements ActionListener
{
TextField P_1;
TextField P_2;
TextField P_3;
TextField P_4;
TextField P_5;
TextField P_6;
TextField P_7;
TextField P_8;
TextField P_9;
TextField W_1;
TextField W_2;
TextField W_3;
TextField W_4;
TextField W_5;
TextField W_6;
TextField W_7;
TextField W_8;
TextField W_9;
Button DetA;
public Kalkulator()
{
super("Kalkulator");
setLayout(new GridLayout(0,9));
setBackground(Color.gray );
P_1 = new TextField(4);
add(P_1);
P_1.setText("0.0");
P_1.setBackground(null);
P_1.setBackground(Color.white);
P_2 = new TextField(4);
add(P_2);
P_2.setText("0.0") ;
P_2.setBackground(null);
P_2.setBackground(Color.white);
P_3 = new TextField(4);
add(P_3);
P_3.setText("0.0");
P_3.setBackground(null);
P_3.setBackground(Color.white);
add(new Label(""));
add(new Label(""));
add(new Label(""));
W_1 = new TextField();
add(W_1);
W_1.setBackground(Color.white);
W_2 = new TextField();
add(W_2);
W_2.setBackground(Color.white);
W_3 = new TextField(4);
add(W_3);
W_3.setBackground(Color.white);
P_4 = new TextField(4);
add(P_4);
P_4.setText("0.0");
P_4.setBackground(null);
P_4.setBackground(Color.white);
P_5 = new TextField(4);
add(P_5);
P_5.setText("0.0");
P_5.setBackground(null);
P_5.setBackground(Color.white);
P_6 = new TextField(4);
add(P_6);
P_6.setText("0.0");
P_6.setBackground(null);
P_6.setBackground(Color.white);
add(new Label(""));
// obsluga klawisza dzialania
DetA = new Button("DetA");
DetA.addActionListener(this);
add(DetA);
DetA.setBackground(Color.red);
add(new Label(""));
W_4 = new TextField();
add(W_4);
W_4.setBackground(Color.white);
W_5 = new TextField();
add(W_5);
W_5.setBackground(Color.white);
W_6 = new TextField();
add(W_6);
W_6.setBackground(Color.white);
P_7 = new TextField(4);
add(P_7);
P_7.setText("0.0");
P_7.setBackground(Color.white);
P_8 = new TextField(4);
add(P_8);
P_8.setText("0.0");
P_8.setBackground(Color.white);
P_9 = new TextField(4);
add(P_9);
P_9.setText("0.0");
P_9.setBackground(Color.white);
add(new Label(""));
add(new Label(""));
add(new Label(""));
W_7 = new TextField();
add(W_7);
W_7.setBackground(Color.white);
W_8 = new TextField();
add(W_8);
W_8.setBackground(Color.white);
W_9 = new TextField();
add(W_9);
W_9.setBackground(Color.white);
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}
);
}// konstruktor
// wyswietlanie sformatowanego
public void WyswietlWynikDetA(double re){
W_1.setText(Double.toString(re));
W_2.setText(Double.toString(re));
}
//dzialanie dodawania
public void W_1(){
double p_5 = Double.parseDouble(P_5.getText());
double p_6 = Double.parseDouble(P_6.getText());
double p_8 = Double.parseDouble(P_8.getText());
double p_9 = Double.parseDouble(P_9.getText());
double re = p_5 * p_9 - p_6 * p_8;
this.WyswietlWynikDetA(re);
}
public void W_2(){
double p_4 = Double.parseDouble(P_4.getText());
double p_6 = Double.parseDouble(P_6.getText());
double p_7 = Double.parseDouble(P_7.getText());
double p_9 = Double.parseDouble(P_9.getText());
double re = p_4 * p_9 - p_6 * p_7;
this.WyswietlWynikDetA(re);
}
// rozpoznanie zdarzenia - dzialania
public void actionPerformed(ActionEvent DetA)
{
String label = DetA.getActionCommand();
try {
if (label.equals("DetA"))
this.W_1();
this.W_2();
}
catch(NumberFormatException e){
System.out.println("Niepopprawne dane wejsciowe");
}
}
}