Hallo,
will einen kleinen Taschenrechner mit SWT coden
Die GUI steht, allerdings klappt die Überprüfung, welcher Button gedrückt wurde (mit e.getSource() == irgendeiner der Buttons), welche in der Methode actionPerformed() eines ActionListeners (als private Klasse definiert) überprüft wird. Hier der Code, vielleicht kann mir ja jemand verraten, wo mein Fehler liegt
will einen kleinen Taschenrechner mit SWT coden
Die GUI steht, allerdings klappt die Überprüfung, welcher Button gedrückt wurde (mit e.getSource() == irgendeiner der Buttons), welche in der Methode actionPerformed() eines ActionListeners (als private Klasse definiert) überprüft wird. Hier der Code, vielleicht kann mir ja jemand verraten, wo mein Fehler liegt
Java:
public class Rechner extends JFrame {
private JPanel contentPane;
private JTextField jtoperand_1;
private JTextField jtoperand_2;
JButton bplus;
JButton bminus;
JButton bmal;
JButton bgeteilt;
JLabel jlErgebnis;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Rechner frame = new Rechner();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the Frame.
*/
public Rechner() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 426, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblOperand = new JLabel("1. Operand");
lblOperand.setBounds(25, 29, 90, 16);
contentPane.add(lblOperand);
JLabel lblOperand_1 = new JLabel("2. Operand");
lblOperand_1.setBounds(300, 29, 90, 16);
contentPane.add(lblOperand_1);
jtoperand_1 = new JTextField();
jtoperand_1.setBounds(25, 73, 90, 28);
contentPane.add(jtoperand_1);
jtoperand_1.setColumns(10);
jtoperand_2 = new JTextField();
jtoperand_2.setBounds(300, 73, 90, 28);
contentPane.add(jtoperand_2);
jtoperand_2.setColumns(10);
JButton bplus = new JButton("+");
bplus.addActionListener(bl);
bplus.setBounds(143, 49, 61, 52);
contentPane.add(bplus);
JButton bminus = new JButton("-");
bminus.addActionListener(bl);
bminus.setBounds(211, 49, 61, 52);
contentPane.add(bminus);
JButton bmal = new JButton("*");
bmal.addActionListener(bl);
bmal.setBounds(143, 109, 61, 52);
contentPane.add(bmal);
JButton bgeteilt = new JButton("/");
bgeteilt.addActionListener(bl);
bgeteilt.setBounds(211, 109, 61, 52);
contentPane.add(bgeteilt);
JLabel lblErgebnis = new JLabel("Ergebnis:");
lblErgebnis.setBounds(25, 206, 61, 16);
contentPane.add(lblErgebnis);
JLabel jlErgebnis = new JLabel("");
jlErgebnis.setBounds(25, 237, 162, 16);
contentPane.add(jlErgebnis);
}
private class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
doubleergebnis;
double operand1 = Double.parseDouble(jtoperand_1.getText());
double operand2 = Double.parseDouble(jtoperand_2.getText());
if(e.getSource() == bplus) {
ergebnis = operand1 + operand2;
} else if (e.getSource() == bminus) {
ergebnis = operand1 - operand2;
} else if (e.getSource() == bmal) {
ergebnis = operand1 * operand2;
} else if (e.getSource() == bgeteilt) {
ergebnis = operand1 / operand2;
} else {
ergebnis = 0.211299;
}
System.out.println(ergebnis);
//rechnen(e);
}
}
ButtonListener bl = new ButtonListener();
Zuletzt bearbeitet von einem Moderator: