Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
JTextField firstfield;
JTextField secondfield;
Dimension framesize = new Dimension(500, 250);
JFrame window;
JLabel erstezahl;
JLabel zweitezahl;
JLabel pluszeichen;
JLabel ergebnis;
JLabel istgleich;
public static void main(String[] args) {
Calculator cal = new Calculator();
cal.setwindow();
}
private void setwindow() {
window = new JFrame();
window.setLayout(null);
window.setSize(framesize);
window.setTitle("Taschenrechner");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
rechnemitte();
setlabels();
settextfields();
addcontent();
rechnungundergebnis();
window.setVisible(true);
}
private void addcontent() {
JPanel pane = new JPanel();
pane.setLayout(null);
pane.setBackground(Color.red);
pane.setBounds(1, 1, 500, 100);
pane.add(erstezahl);
pane.add(zweitezahl);
pane.add(firstfield);
pane.add(secondfield);
pane.add(pluszeichen);
pane.add(ergebnis);
pane.add(istgleich);
pane.setVisible(true);
window.add(pane);
}
private void setlabels() {
erstezahl = new JLabel();
erstezahl.setText("Erste Zahl:");
erstezahl.setBounds(2, 2, 70, 30);
erstezahl.setVisible(true);
zweitezahl = new JLabel();
zweitezahl.setText("Zweite Zahl:");
zweitezahl.setBounds(200, 2, 70, 30);
zweitezahl.setVisible(true);
pluszeichen = new JLabel();
pluszeichen.setText("+");
pluszeichen.setBounds(180, 6, 20, 20);
pluszeichen.setVisible(true);
ergebnis = new JLabel();
ergebnis.setText("Summe");
ergebnis.setBounds(400, 6, 100, 20);
ergebnis.setVisible(true);
istgleich = new JLabel();
istgleich.setText("=");
istgleich.setBounds(385, 6, 20, 20);
}
private void settextfields() {
firstfield = new JTextField();
firstfield.setBounds(75, 6, 100, 20);
firstfield.setBackground(Color.blue);
firstfield.setVisible(true);
secondfield = new JTextField();
secondfield.setBounds(280, 6, 100, 20);
secondfield.setBackground(Color.green);
secondfield.setVisible(true);
}
private void rechnemitte() {
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int framewidth = framesize.width;
int frameheight = framesize.height;
int screenwidth = screensize.width;
int screenheight = screensize.height;
int xpos = screenwidth / 2 - framewidth / 2;
int ypos = screenheight / 2 - frameheight / 2 - 100;
window.setLocation(xpos, ypos);
}
private void rechnungundergebnis() {
String firstfigure = firstfield.getText();
String secondfigure = secondfield.getText();
int firstfigureint = Integer.parseInt(firstfigure);
int secondfigureint = Integer.parseInt(secondfigure);
int summe = firstfigureint + secondfigureint;
String summee = String.valueOf(summe);
}
}
Wieso gibt es hier einen Error obwohl ich noch nicht einmal versucht habe die summe des
Taschenrechners also summee im taschenrechner auszugeben? Die Applikation sieht so aus und summee hätte den Text Summe in dem Programm ersetzen sollen. http://www7.pic-upload.de/15.03.14/87fg9cc2s6s.jpg