Hallo,
ich versuche einen kleinen Taschenrechner in Java zu Programmieren mit Eclipse, dem JDK 1.6.0 und dem Visual Plug in.
Der Quellcode sieht bis jetzt so aus:
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
import java.awt.Point;
public class TR extends Frame {
private static final long serialVersionUID = 1L;
private JTextField TF = null;
private JButton b1 = null;
private JButton b2 = null;
private JButton b3 = null;
private JButton b4 = null;
private JButton b5 = null;
private JButton b6 = null;
private JButton b7 = null;
private JButton b8 = null;
private JButton b9 = null;
private JButton b0 = null;
private JButton bpl = null;
private JButton bmi = null;
private JButton bma = null;
private JButton bdu = null;
private JButton bgl = null;
/**
* This is the default constructor
*/
public TR() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setLayout(null);
this.setSize(221, 196);
this.setTitle("Frame");
this.add(getTF(), null);
this.add(getB1(), null);
this.add(getB2(), null);
this.add(getB3(), null);
this.add(getB4(), null);
this.add(getB5(), null);
this.add(getB6(), null);
this.add(getB7(), null);
this.add(getB8(), null);
this.add(getB9(), null);
this.add(getB0(), null);
this.add(getBpl(), null);
this.add(getBmi(), null);
this.add(getBma(), null);
this.add(getBdu(), null);
this.add(getBgl(), null);
}
/**
* This method initializes TF
*
* @return javax.swing.JTextField
*/
private JTextField getTF() {
if (TF == null) {
TF = new JTextField();
TF.setBounds(new Rectangle(14, 34, 195, 21));
}
return TF;
}
/**
* This method initializes b1
*
* @return javax.swing.JButton
*/
String zahl;
private JButton getB1() {
if (b1 == null) {
b1 = new JButton();
b1.setLocation(new Point(16, 59));
b1.setText("1");
b1.setSize(new Dimension(45, 25));
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
TF.setText("1");
}
});
}
return b1;
}
/**
* This method initializes b2
*
* @return javax.swing.JButton
*/
private JButton getB2() {
if (b2 == null) {
b2 = new JButton();
b2.setLocation(new Point(65, 59));
b2.setText("2");
b2.setSize(new Dimension(45, 25));
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String zahl = "";
zahl = zahl + TF.getText();
TF.setText(zahl);
}
});
}
return b2;
}
/**
* This method initializes b3
*
* @return javax.swing.JButton
*/
private JButton getB3() {
if (b3 == null) {
b3 = new JButton();
b3.setLocation(new Point(114, 59));
b3.setText("3");
b3.setSize(new Dimension(45, 25));
}
return b3;
}
/**
* This method initializes b4
*
* @return javax.swing.JButton
*/
private JButton getB4() {
if (b4 == null) {
b4 = new JButton();
b4.setLocation(new Point(16, 88));
b4.setText("4");
b4.setSize(new Dimension(45, 25));
}
return b4;
}
/**
* This method initializes b5
*
* @return javax.swing.JButton
*/
private JButton getB5() {
if (b5 == null) {
b5 = new JButton();
b5.setLocation(new Point(65, 88));
b5.setText("5");
b5.setSize(new Dimension(45, 25));
}
return b5;
}
/**
* This method initializes b6
*
* @return javax.swing.JButton
*/
private JButton getB6() {
if (b6 == null) {
b6 = new JButton();
b6.setLocation(new Point(114, 88));
b6.setText("6");
b6.setSize(new Dimension(45, 25));
}
return b6;
}
/**
* This method initializes b7
*
* @return javax.swing.JButton
*/
private JButton getB7() {
if (b7 == null) {
b7 = new JButton();
b7.setLocation(new Point(16, 118));
b7.setText("7");
b7.setSize(new Dimension(45, 25));
}
return b7;
}
/**
* This method initializes b8
*
* @return javax.swing.JButton
*/
private JButton getB8() {
if (b8 == null) {
b8 = new JButton();
b8.setLocation(new Point(65, 118));
b8.setText("8");
b8.setSize(new Dimension(45, 25));
}
return b8;
}
/**
* This method initializes b9
*
* @return javax.swing.JButton
*/
private JButton getB9() {
if (b9 == null) {
b9 = new JButton();
b9.setLocation(new Point(114, 118));
b9.setText("9");
b9.setSize(new Dimension(45, 25));
}
return b9;
}
/**
* This method initializes b0
*
* @return javax.swing.JButton
*/
private JButton getB0() {
if (b0 == null) {
b0 = new JButton();
b0.setLocation(new Point(65, 148));
b0.setText("0");
b0.setSize(new Dimension(45, 25));
}
return b0;
}
/**
* This method initializes bpl
*
* @return javax.swing.JButton
*/
private JButton getBpl() {
if (bpl == null) {
bpl = new JButton();
bpl.setSize(new Dimension(45, 25));
bpl.setText("+");
bpl.setLocation(new Point(163, 59));
}
return bpl;
}
/**
* This method initializes bmi
*
* @return javax.swing.JButton
*/
private JButton getBmi() {
if (bmi == null) {
bmi = new JButton();
bmi.setSize(new Dimension(45, 25));
bmi.setText("-");
bmi.setLocation(new Point(163, 88));
}
return bmi;
}
/**
* This method initializes bma
*
* @return javax.swing.JButton
*/
private JButton getBma() {
if (bma == null) {
bma = new JButton();
bma.setSize(new Dimension(45, 25));
bma.setText("*");
bma.setLocation(new Point(163, 118));
}
return bma;
}
/**
* This method initializes bdu
*
* @return javax.swing.JButton
*/
private JButton getBdu() {
if (bdu == null) {
bdu = new JButton();
bdu.setSize(new Dimension(45, 25));
bdu.setText("/");
bdu.setLocation(new Point(163, 148));
}
return bdu;
}
/**
* This method initializes bgl
*
* @return javax.swing.JButton
*/
private JButton getBgl() {
if (bgl == null) {
bgl = new JButton();
bgl.setSize(new Dimension(45, 25));
bgl.setText("=");
bgl.setLocation(new Point(114, 148));
}
return bgl;
}
} // @jve:decl-index=0:visual-constraint="23,39"```
Mein momentanes Problem ist, das ich wenn mehrmals auf eine Zahl klicke die vorherige überschrieben wird. Hat jemand ne idee wie ich das problem lösen kann? Ich hab auch schon google benutz und anderen Code gelesen aber nichts finden können.