R
Robin
Gast
Hallo, ich habe eine Frage: Wir haben in Informatik halbwegs ein Taschenrechner GUI erstellt bei dem auch schon einzelne Tasten funktionieren. Jetzt haben wir dazu neben dem GUI eine neue Klasse erstellt die dann alles ausrechnen soll, aber ich weiß noch nicht genau wie man dann die Klasse aus dem GUI ansprechen soll oder wie man die Strings in double-Werte umwandeln kann(jedenfalls dass der das dann verrechnen kann).
GUI:
Recheneinheit:
Am besten wäre es wenn es Möglichkeiten gäbe ohne viele neue Sachen in Java, wir haben das glaub ich schon extremst simpel für Java erstellt.
GUI:
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TR_GUI
{
protected JFrame fenster;
protected JTextField display;
protected Recheneinheit recheneinheit;
public TR_GUI()
{
recheneinheit = new Recheneinheit();
fenster = new JFrame("Taschenrechner");
fuelleFenster();
fenster.pack();
fenster.setVisible(true);
fenster.setResizable(false);
}//Konstrukt TR_GUI
public void fuelleFenster()
{
Container contentPane = fenster.getContentPane();
contentPane.setLayout (new BorderLayout());
//Display kommt nach oben
display = new JTextField();
display.setHorizontalAlignment(JTextField.RIGHT);
contentPane.add (display, BorderLayout.NORTH);
//Autorrenname und Versionsnummer werden unten angezeigt
contentPane.add(new JLabel("Robin - Version 0.3"), BorderLayout.SOUTH);
//Ziffernblock
JPanel ziffernblock = new JPanel();
contentPane.add(ziffernblock, BorderLayout.WEST);
fuelleZiffernblock(ziffernblock);
//Funktionsblock
JPanel funktionsblock = new JPanel();
contentPane.add(funktionsblock, BorderLayout.EAST);
fuelleFunktionsblock(funktionsblock);
}//fuelleFenster
public void fuelleZiffernblock(JPanel ziffern)
{
ziffern.setLayout(new GridLayout(4,3));
TastenActionListener tastenAL = new TastenActionListener();
for(int i=1; i<10; i++)
{
JButton taste = new JButton(String.valueOf(i));
ziffern.add(taste);
taste.addActionListener(tastenAL);
}//for
//Die Tasten 0 und das Komma werden einzeln eingefügt
JButton taste0 = new JButton("0");
ziffern.add(taste0);
taste0.addActionListener(tastenAL);
JButton tasteKomma = new JButton(",");
ziffern.add(tasteKomma);
tasteKomma.addActionListener(tastenAL);
}//fuelleZiffernblock
public void fuelleFunktionsblock(JPanel funktionen)
{
funktionen.setLayout(new GridLayout(4,2));
TastenActionListener tastenAL = new TastenActionListener();
GleichActionListener gleichAL = new GleichActionListener();
ClearActionListener clearAL = new ClearActionListener();
JButton plus = new JButton("+");
plus.addActionListener(tastenAL);
funktionen.add(plus);
JButton gleich = new JButton("=");
gleich.addActionListener(gleichAL);
funktionen.add(gleich);
JButton minus = new JButton("-");
minus.addActionListener(tastenAL);
funktionen.add(minus);
JButton leer1 = new JButton("");
funktionen.add(leer1);
JButton mal = new JButton("*");
mal.addActionListener(tastenAL);
funktionen.add(mal);
JButton leer2 = new JButton("");
funktionen.add(leer2);
JButton geteilt = new JButton("/");
geteilt.addActionListener(tastenAL);
funktionen.add(geteilt);
JButton tasteC = new JButton("C");
funktionen.add(tasteC);
tasteC.addActionListener(clearAL);
}//fuelleFunktionsblock
class TastenActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton taste = (JButton) e.getSource();
display.setText(display.getText()+taste.getText());
}//void ActionListenerTasten
}//ActionListenerTasten
class ClearActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
display.setText("");
}//void ActionListeneerClear
}//ActionListenerClear
class GleichActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
display.setText("Syntax Error");
}//void ActionListenerGleich
}//ActionListenerGleich
}//TR_GUI
Recheneinheit:
Code:
public class Recheneinheit
{
public Recheneinheit()
{
}//Recheneinheit
public double summeBerechnen (double x,double y)
{
return (x+y);
}//int summeBerechnen
public double differenzBerechnen (double x,double y)
{
return (x-y);
}//int differenzBerechnen
public double produktBerechnen (double x,double y)
{
return (x*y);
}//int produktBerechnen
public double quotientBerechnen (double x,double y)
{
return (x/y);
}//int quotientBerechnen
public double quadratBerechnen (double x)
{
return (x*x);
}//int quadratBerechnen
}//class Recheneinheit
Am besten wäre es wenn es Möglichkeiten gäbe ohne viele neue Sachen in Java, wir haben das glaub ich schon extremst simpel für Java erstellt.