Hallo,
ich habe einen kleinen Taschenrechner geschrieben er soll Vektoren addieren, subtrahieren und das skalar produkt bilden können es ist eine aufgabe von der schule da ich sowas schon bei meinem vorigem programm gemacht habe und es dort gegangen ist, wundert es mich das es hier nicht geht,
ich habe vier JButtons +, -, skalar und = und einen antwort String in dem steht dann +, -, s oder u abhängig welchen Button man drückt dann werden die Felder gelöscht für den zweiten Vektor, die neue Eingabe und wenn man auf den Button = drückt passiert nichts
ich versuche weiter unten im ActionListener den String mittels String.equals("Zeichenkette") abzufragen, jedoch
wird kein Zweig betreten. Kann mir wer helfen?
lg fridolin
ich habe einen kleinen Taschenrechner geschrieben er soll Vektoren addieren, subtrahieren und das skalar produkt bilden können es ist eine aufgabe von der schule da ich sowas schon bei meinem vorigem programm gemacht habe und es dort gegangen ist, wundert es mich das es hier nicht geht,
ich habe vier JButtons +, -, skalar und = und einen antwort String in dem steht dann +, -, s oder u abhängig welchen Button man drückt dann werden die Felder gelöscht für den zweiten Vektor, die neue Eingabe und wenn man auf den Button = drückt passiert nichts
ich versuche weiter unten im ActionListener den String mittels String.equals("Zeichenkette") abzufragen, jedoch
wird kein Zweig betreten. Kann mir wer helfen?
lg fridolin
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Vektor2d extends JFrame implements ActionListener
{
double a;
double b;
double c;
double[] zahla = new double[3];
double[] zahlb = new double[3];
JTextField eins;
JTextField zwei;
JButton skalar;
JButton plus;
JButton minus;
JButton gleich;
JPanel panel;
JPanel text;
String antwort;
public double seta(double x)
{
this.a = x;
return this.a;
}
public double setb(double y)
{
this.b = y;
return this.b;
}
public double setc(double z)
{
this.c = z;
return this.c;
}
public double geta()
{
return this.a;
}
public double getb()
{
return this.b;
}
public double getc()
{
return this.c;
}
public void add(double x, double y, double z, double n)
{
this.a = x*n + z*y;
this.b = y*n;
}
public void subtract(double x, double y, double z, double n)
{
this.a = x*n - z*y;
this.b = y*n;
}
public double skalar(double x, double y, double z, double n)
{
this.a = x*z;
this.b = y*n;
this.c = this.a + this.b;
return this.c;
}
public void clear(JTextField x, JTextField y)
{
x.setText("");
y.setText("");
}
public double greatestcommonDivisor(double x, double y)
{
while(x != 0.0)
{
double temp = y % x;
y = x;
x = temp;
}
return y;
}
public Vektor2d()
{
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
System.exit(0);
}
});
menuFile.add(menuFileExit);
menuBar.add(menuFile);
Container content = getContentPane();
content.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new FlowLayout());
plus = new JButton("+");
plus.addActionListener(this);
//plus.setPreferredSize(new Dimension(20,20));
panel.add(plus);
minus = new JButton("-");
minus.addActionListener(this);
//minus.setPreferredSize(new Dimension(20,20));
panel.add(minus);
gleich = new JButton("=");
gleich.addActionListener(this);
//gleich.setPreferredSize(new Dimension(20,20));
panel.add(gleich);
skalar = new JButton("skalar");
skalar.addActionListener(this);
panel.add(skalar);
content.add(panel, BorderLayout.NORTH);
text = new JPanel();
text.setLayout(new BorderLayout());
eins = new JTextField();
eins.requestFocus();
text.add(eins, BorderLayout.NORTH);
zwei = new JTextField();
text.add(zwei, BorderLayout.SOUTH);
content.add(text, BorderLayout.SOUTH);
this.a = 100.0;
this.b = 200.0;
}
public Vektor2d(double x, double y)
{
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
System.exit(0);
}
});
menuFile.add(menuFileExit);
menuBar.add(menuFile);
Container content = getContentPane();
content.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new FlowLayout());
plus = new JButton("+");
plus.addActionListener(this);
//plus.setPreferredSize(new Dimension(20,20));
panel.add(plus);
minus = new JButton("-");
minus.addActionListener(this);
//minus.setPreferredSize(new Dimension(20,20));
panel.add(minus);
gleich = new JButton("=");
gleich.addActionListener(this);
//gleich.setPreferredSize(new Dimension(20,20));
panel.add(gleich);
skalar = new JButton("skalar");
skalar.addActionListener(this);
panel.add(skalar);
content.add(panel, BorderLayout.NORTH);
text = new JPanel();
text.setLayout(new BorderLayout());
eins = new JTextField();
eins.requestFocus();
text.add(eins, BorderLayout.NORTH);
zwei = new JTextField();
text.add(zwei, BorderLayout.SOUTH);
content.add(text, BorderLayout.SOUTH);
this.a = x;
this.b = y;
}
public void actionPerformed(ActionEvent event)
{
Object quelle = event.getSource();
try
{
zahla[0] = Double.parseDouble(eins.getText());
zahlb[0] = Double.parseDouble(zwei.getText());
seta(zahla[0]);
setb(zahlb[0]);
}
catch(NumberFormatException e11)
{
System.out.println("Das war keine Zahl.\n Bitte geben sie erneut ein.");
zahla[0] = 0.0;
zahlb[0] = 0.0;
seta(zahla[0]);
setb(zahlb[0]);
}
if(quelle == plus)
{
antwort = "+";
clear(eins, zwei);
}
else if(quelle == minus)
{
antwort = "-";
clear(eins, zwei);
}
else if(quelle == skalar)
{
antwort = "s";
clear(eins,zwei);
}
else
{
antwort = "u";
}
if(quelle == gleich)
{
if(!antwort.equals("u"))
{
try
{
zahla[1] = Double.parseDouble(eins.getText());
zahlb[1] = Double.parseDouble(zwei.getText());
}
catch(NumberFormatException e11)
{
System.out.println("Das war keine Zahl.\n Bitte geben sie erneut ein.");
zahla[1] = 0.0;
zahlb[1] = 0.0;
seta(zahla[1]);
setb(zahlb[1]);
zahla[0] = 0.0;
zahlb[0] = 0.0;
seta(zahla[0]);
setb(zahlb[0]);
}
}
if(antwort.equals("+"))
{
add(zahla[0], zahlb[0], zahla[1], zahlb[1]);
zahla[2] = geta();
zahlb[2] = getb();
eins.setText(new Double(zahla[2]).toString());
zwei.setText(new Double(zahlb[2]).toString());
for(int i = 0; i <= 2; i++)
{
zahla[i] = 0.0;
zahlb[i] = 0.0;
}
seta(0.0);
setb(0.0);
}
if(antwort.equals("-"))
{
subtract(zahla[0], zahlb[0], zahla[1], zahlb[1]);
zahla[2] = geta();
zahlb[2] = getb();
eins.setText(new Double(zahla[2]).toString());
zwei.setText(new Double(zahlb[2]).toString());
for(int i = 0; i <= 2; i++)
{
zahla[i] = 0.0;
zahlb[i] = 0.0;
}
seta(0.0);
setb(0.0);
}
if(antwort.equals("s"))
{
skalar(zahla[0], zahlb[0], zahla[1], zahlb[1]);
zahla[2] = getc();
eins.setText(new Double(zahla[2]).toString());
for(int i = 0; i <= 2; i++)
{
zahla[i] = 0.0;
zahlb[i] = 0.0;
}
seta(0.0);
setb(0.0);
setc(0.0);
}
}
}
public static void main(String[] args)
{
// Create application frame.
Vektor2d fridolin = new Vektor2d();
fridolin.setSize(200, 200);
fridolin.setLocation(100, 100);
fridolin.setTitle("Vektor2d");
fridolin.setVisible(true);
}
}