Hallo Zusammen,
Ich muss für die Schule einen Taschenrechner mit GUI programmieren. Die GUI ist soweit auch fertig ich kann auch Zahlen über Buttons eingeben und die werden mir im Display angezeigt. Mein Problem: wie bekomme ich es hin das eine Rechenoperation ausgeführt wird? Ich habe auch schon mehrere Beispiele gefunden nur schaffe ich es nicht das ganze ans laufen zu bekommen. Unser Lehrer ist da leider keine große Hilfe er sagt googlet einfach. Ich weiß auch nie ob das so richtig ist was ich bis her gemacht habe. Über einen Denkanstoß oder ein kurzes Beispiel würde ich freuen.
Vielen Dank schonmal im Vorraus
Ich muss für die Schule einen Taschenrechner mit GUI programmieren. Die GUI ist soweit auch fertig ich kann auch Zahlen über Buttons eingeben und die werden mir im Display angezeigt. Mein Problem: wie bekomme ich es hin das eine Rechenoperation ausgeführt wird? Ich habe auch schon mehrere Beispiele gefunden nur schaffe ich es nicht das ganze ans laufen zu bekommen. Unser Lehrer ist da leider keine große Hilfe er sagt googlet einfach. Ich weiß auch nie ob das so richtig ist was ich bis her gemacht habe. Über einen Denkanstoß oder ein kurzes Beispiel würde ich freuen.
Vielen Dank schonmal im Vorraus
Java:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GridBagLayoutExample implements ActionListener
{
private static JButton display;
static void addComponent( Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty )
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
gbc.gridx = x; gbc.gridy = y;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbl.setConstraints( c, gbc );
cont.add( c );
}
public GridBagLayoutExample ()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = f.getContentPane();
GridBagLayout gbl = new GridBagLayout();
c.setLayout( gbl );
f.setSize( 500, 600 );
f.setVisible( true );
display = new JButton("");
display.setEnabled(false);
JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttonplus = new JButton("+");
JButton buttonminus = new JButton("-");
JButton buttonmulty = new JButton("*");
JButton buttondivi = new JButton("/");
JButton buttongleich = new JButton("=");
JButton buttonC = new JButton("C");
JButton buttonleer = new JButton("leer");
JButton buttonkommer = new JButton(",");
JButton buttonvorzeichen = new JButton("+/-");
x y w h wx wy
addComponent( c, gbl, display, 0, 0, 10,2, 0, 1 );
addComponent( c, gbl, button7, 0, 2, 2, 2, 1, 1 );
addComponent( c, gbl, button8, 2, 2, 2, 2, 1, 1 );
addComponent( c, gbl, button9, 4, 2, 2, 2, 1, 1 );
addComponent( c, gbl, buttonC, 6, 2, 2, 2, 1, 1 );
addComponent( c, gbl, buttonleer, 8, 2, 2, 2, 1, 1 );
addComponent( c, gbl, button4, 0, 4, 2, 2, 1, 1 );
addComponent( c, gbl, button5, 2, 4, 2, 2, 1, 1 );
addComponent( c, gbl, button6, 4, 4, 2, 2, 1, 1 );
addComponent( c, gbl, buttonmulty, 6, 4, 2, 2, 1, 1 );
addComponent( c, gbl, buttondivi, 8, 4, 2, 2, 1, 1 );
addComponent( c, gbl, button1, 0, 6, 2, 2, 1, 1 );
addComponent( c, gbl, button2, 2, 6, 2, 2, 1, 1 );
addComponent( c, gbl, button3, 4, 6, 2, 2, 1, 1 );
addComponent( c, gbl, buttonplus, 6, 6, 2, 2, 1, 1 );
addComponent( c, gbl, buttonminus, 8, 6, 2, 2, 1, 1 );
addComponent( c, gbl, button0, 0, 8, 2, 2, 1, 1 );
addComponent( c, gbl, buttonvorzeichen, 2, 8, 2, 2, 1, 1 );
addComponent( c, gbl, buttonkommer, 4, 8, 2, 2, 1, 1 );
addComponent( c, gbl, buttongleich, 6, 8, 4, 2, 1, 1 );
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonkommer.addActionListener(this);
buttonmulty.addActionListener(this);
buttondivi.addActionListener(this);
buttonplus.addActionListener(this);
buttonminus.addActionListener(this);
buttongleich.addActionListener(this);
}
public static void main( String[] args ) {
new GridBagLayoutExample ();
}
@Override
public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
display.setText(display.getText() + input);
}
}