Hallo, Ich hätte mal wieder ne Frage zum Taschenrechner. Hier erstmal mein Code
Hab im Internet schon nachgeschaut aber nichts gefunden bei dem ich es wirklich richtig verstanden habe.. Also wie kriege ich nun hin dass die Zahlen addiert, subtrahiert,multipliziert werden?
Mfg, Zitrus
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FirstGUI extends JFrame
{
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button10;
private JButton button11;
private JButton button12;
private JButton button13;
private JPanel panel;
private JTextArea ta;
public FirstGUI()
{
super("Rechner");
setLayout(null);
setSize(235,180);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
panel = new JPanel(null);
panel.setSize(300,200);
ta = new JTextArea();
ta.setBounds(10,10,200,40);
ta.setEditable(false);
button1 = new JButton ("1");
button1.addActionListener(new ButtonLauscher1());
button1.setBounds(10,60,40,25);
button1.setFont(new Font("Arial", 10, 10));
button2 = new JButton ("2");
button2.addActionListener(new ButtonLauscher2());
button2.setBounds(50,60,40,25);
button2.setFont(new Font("Arial", 10, 10));
button3 = new JButton ("3");
button3.addActionListener(new ButtonLauscher3());
button3.setBounds(90,60,40,25);
button3.setFont(new Font("Arial",10, 10));
button4 = new JButton ("4");
button4.addActionListener(new ButtonLauscher4());
button4.setBounds(10,85,40,25);
button4.setFont(new Font("Arial", 10, 10));
button5 = new JButton ("5");
button5.addActionListener(new ButtonLauscher5());
button5.setBounds(50,85,40,25);
button5.setFont(new Font("Arial", 10, 10));
button6 = new JButton ("6");
button6.addActionListener(new ButtonLauscher6());
button6.setBounds(90,85,40,25);
button6.setFont(new Font("Arial", 10, 10));
button7 = new JButton ("7");
button7.addActionListener(new ButtonLauscher7());
button7.setBounds(10,110,40,25);
button7.setFont(new Font("Arial", 10, 10));
button8 = new JButton ("8");
button8.addActionListener(new ButtonLauscher8());
button8.setBounds(50,110,40,25);
button8.setFont(new Font("Arial", 10, 10));
button9 = new JButton ("9");
button9.addActionListener(new ButtonLauscher9());
button9.setBounds(90,110,40,25);
button9.setFont(new Font("Arial", 10, 10));
button10 = new JButton ("+");
button10.addActionListener(new ButtonLauscher10());
button10.setBounds(130,60,40,25);
button10.setFont(new Font("Arial", 10, 10));
button11 = new JButton ("-");
button11.addActionListener(new ButtonLauscher11());
button11.setBounds(130,85,40,25);
button11.setFont(new Font("Arial", 10, 10));
button12 = new JButton ("x");
button12.addActionListener(new ButtonLauscher12());
button12.setBounds(130,110,40,25);
button12.setFont(new Font("Arial", 10, 10));
button13 = new JButton ("=");
button13.addActionListener(new ButtonLauscher13());
button13.setBounds(170,60,40,75);
button13.setFont(new Font("Arial", 10, 10));
panel.add(ta);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button10);
panel.add(button13);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(button11);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.add(button12);
add(panel);
setVisible(true);
}
public static void main (String[] args)
{
new FirstGUI();
}
class ButtonLauscher1 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if(evt.getSource() == button1){
ta.append("1");
}
}
}
class ButtonLauscher2 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button2){
ta.append("2");
}
}
}
class ButtonLauscher3 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button3) {
ta.append("3");
}
}
}
class ButtonLauscher4 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button4) {
ta.append("4");
}
}
}
class ButtonLauscher5 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button5) {
ta.append("5");
}
}
}
class ButtonLauscher6 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button6) {
ta.append("6");
}
}
}
class ButtonLauscher7 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button7) {
ta.append("7");
}
}
}
class ButtonLauscher8 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button8) {
ta.append("8");
}
}
}
class ButtonLauscher9 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button9) {
ta.append("9");
}
}
}
class ButtonLauscher10 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button10) {
ta.append("+");
}
}
}
class ButtonLauscher11 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button11) {
ta.append("-");
}
}
}
class ButtonLauscher12 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button12) {
ta.append("x");
}
}
}
class ButtonLauscher13 implements ActionListener {
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == button13) {
ta.append("=");
}
}
}
}
Hab im Internet schon nachgeschaut aber nichts gefunden bei dem ich es wirklich richtig verstanden habe.. Also wie kriege ich nun hin dass die Zahlen addiert, subtrahiert,multipliziert werden?
Mfg, Zitrus
Zuletzt bearbeitet von einem Moderator: