Java:
package rechner;
import javax.swing.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
public class Rechner
{
public static void main (String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setLayout(new BorderLayout());
setIconImage(getToolkit().getImage("img1.png"));
setResizable(false);
setSize(250,215);
setTitle ("Saltuks' Rechner");
setLocation(100,100);
CalculatorPanel hpanel = new CalculatorPanel();
add(hpanel);
}
}
class CalculatorPanel extends JPanel implements ActionListener
{
private JTextField field;
private JPanel panel;
private String lastCommand;
public CalculatorPanel() {
setLayout(null);
field = new JTextField();
field.setBounds (10,10,225,40);
field.setEditable(false);
field.setVisible(true);
panel = new JPanel();
panel.setLayout(null);
panel.setSize(300,300);
addButton1();
addButton2();
addButton3();
addButton4();
addButton5();
addButton6();
addButton7();
addButton8();
addButton9();
addButton10();
addButton11();
addButton12();
addButton13();
addButton14();
addButton15();
add(field, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
}
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("+");
JButton button5 = new JButton("4");
JButton button6 = new JButton("5");
JButton button7 = new JButton("6");
JButton button8 = new JButton("-");
JButton button9 = new JButton("7");
JButton button10 = new JButton("8");
JButton button11 = new JButton("9");
JButton button12 = new JButton("x");
JButton button13 = new JButton("/");
JButton button14 = new JButton(".");
JButton button15 = new JButton("=");
private void addButton1()
{
button1.addActionListener(this);
button1.setBounds(10,60,45,30);
panel.add(button1);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton2()
{
button2.setBounds(55,60,45,30);
button2.addActionListener(this);
panel.add(button2);
setFont(new Font("Arial", 5, 3));
}
private void addButton3()
{
button3.setBounds(100,60,45,30);
button3.addActionListener(this);
panel.add(button3);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton4()
{
button4.setBounds(145,60,45,30);
button4.addActionListener(this);
panel.add(button4);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton5()
{
button5.setBounds(10,90,45,30);
button5.addActionListener(this);
panel.add(button5);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton6()
{
button6.setBounds(55,90,45,30);
button6.addActionListener(this);
panel.add(button6);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton7()
{
button7.setBounds(100,90,45,30);
button7.addActionListener(this);
panel.add(button7);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton8()
{
button8.setBounds(190,60,45,30);
button8.addActionListener(this);
panel.add(button8);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton9()
{
button9.setBounds(10,120,45,30);
button9.addActionListener(this);
panel.add(button9);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton10()
{
button10.setBounds(55,120,45,30);
button10.addActionListener(this);
panel.add(button10);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton11()
{
button11.setBounds(100,120,45,30);
button11.addActionListener(this);
panel.add(button11);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton12()
{
button12.setBounds(145,90,45,30);
button12.addActionListener(this);
panel.add(button12);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton13()
{
button13.setBounds(190,90,45,30);
button13.addActionListener(this);
panel.add(button13);
setFont(new Font("Agency FB Regular", 10, 3));
}
private void addButton14()
{
button14.setBounds(145,120,45,30);
button14.addActionListener(this);
panel.add(button14);
setFont(new Font("Agency FB Regular", 10, 3));
}
private void addButton15()
{
button15.setBounds(190,120,45,30);
button15.addActionListener(this);
panel.add(button15);
setFont(new Font("Agency FB Regular", 10, 3));
}
public void actionPerformed (ActionEvent ae){
if(ae.getSource() == this.button1){
int i = 1;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button2) {
int i = 2;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button3) {
int i = 3;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button4) {
}
if(ae.getSource() == this.button5) {
int i = 4;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button6) {
int i = 5;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button7) {
int i = 6;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button8) {
}
if(ae.getSource() == this.button9) {
int i = 7;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button10) {
int i = 8;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button11) {
int i = 9;
String str = String.valueOf(i);
field.setText(str);
}
if(ae.getSource() == this.button12) {
}
if(ae.getSource() == this.button13) {
}
if(ae.getSource() == this.button14) {
String str = ".";
field.setText(str);
}
if(ae.getSource() == this.button15) {
if(ae.getSource() == this.button4) {
String a = field.getText() + field.getText();
field.setText(a);
}
}
}
}
Und hier zur Frage : Wie bekomme ich es nun hin, dass z.b beim Button 4 (+-Button) eine Addition mit der 1. und 2. Zahl stattfindet (Also in gedrückter vorm: Zahlen - + - Zahlen).
Wäre froh wenn mir jemand weiterhelfen könnte