wow danke! das ist schonmal ein toller fortschritt.
jetzt haben wir uns gerade mit der + rechnung befasst.
wir haben 2 variablem definiert (displaysdouble und temp) aber iwie hat da was nicht nicht in der zeile wo der rechengang verlaufen soll :_/
wow danke! das ist schonmal ein toller fortschritt.
jetzt haben wir uns gerade mit der + rechnung befasst.
wir haben 2 variablem definiert (displaysdouble und temp) aber iwie hat da was nicht nicht in der zeile wo der rechengang verlaufen soll :_/
hier:
konkrete probleme: ziffern bleiben nicht oben stehen, rechenoperationen gehen nicht
--->
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Insets;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class CalcGui extends JFrame {
JTextField display = new JTextField();
public void initComponents() {
setTitle ("Rechner");
JButton plusButton = new JButton();
JButton minusButton = new JButton();
JButton divideButton = new JButton();
JButton multiplyButton = new JButton();
JButton resultButton = new JButton();
JButton commaButton = new JButton();
JButton deleteButton = new JButton();
JButton zeroButton = new JButton();
JButton oneButton = new JButton();
JButton twoButton = new JButton();
JButton threeButton = new JButton();
JButton fourButton = new JButton();
JButton fiveButton = new JButton();
JButton sixButton = new JButton();
JButton sevenButton = new JButton();
JButton eightButton = new JButton();
JButton nineButton = new JButton();
JMenuBar mainMenu = new JMenuBar();
JMenu fileMenu = new JMenu();
JMenuItem exitMenuItem = new JMenuItem();
getContentPane().setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
display.setBackground(new Color(255, 255, 204));
//Beginn: Display
GridBagConstraints gbcDisplay = new GridBagConstraints();
gbcDisplay.gridx = 0;
gbcDisplay.gridy = 0;
gbcDisplay.gridwidth = 3;
gbcDisplay.fill = GridBagConstraints.HORIZONTAL;
gbcDisplay.insets = new Insets(1, 1, 1, 1);
getContentPane().add(display, gbcDisplay);
//Beginn: sevenButton
sevenButton.setText("7");
sevenButton.setPreferredSize(new Dimension(50, 30));
sevenButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
sevenButtonGedrueckt(evt);
}
});
GridBagConstraints gbcSevenButton = new
GridBagConstraints();
gbcSevenButton.gridx = 0;
gbcSevenButton.gridy = 1;
gbcSevenButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(sevenButton, gbcSevenButton);
//Beginn: fourButton
fourButton.setText("4");
fourButton.setPreferredSize(new Dimension(50, 30));
fourButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
fourButtonGedrueckt(evt);
}
});
GridBagConstraints gbcFourButton = new
GridBagConstraints();
gbcFourButton.gridx = 0;
gbcFourButton.gridy = 2;
gbcFourButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(fourButton, gbcFourButton);
//Beginn: oneButton
oneButton.setText("1");
oneButton.setPreferredSize(new Dimension(50, 30));
oneButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
oneButtonGedrueckt(evt);
}
});
GridBagConstraints gbcOneButton = new
GridBagConstraints();
gbcOneButton.gridx = 0;
gbcOneButton.gridy = 3;
gbcOneButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(oneButton, gbcOneButton);
//Beginn: zeroButton
zeroButton.setText("0");
zeroButton.setPreferredSize(new Dimension(50, 30));
zeroButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
zeroButtonGedrueckt(evt);
}
});
GridBagConstraints gbcZeroButton = new
GridBagConstraints();
gbcZeroButton.gridx = 0;
gbcZeroButton.gridy = 4;
gbcZeroButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(zeroButton, gbcZeroButton);
//Beginn: resultButton
resultButton.setText("=");
resultButton.setPreferredSize(new Dimension(50, 30));
resultButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
resultButtonGedrueckt(evt);
}
});
GridBagConstraints gbcResultButton = new
GridBagConstraints();
gbcResultButton.fill =
GridBagConstraints.HORIZONTAL;
gbcResultButton.gridx = 0;
gbcResultButton.gridy = 5;
gbcResultButton.gridwidth = 4;
gbcResultButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(resultButton, gbcResultButton);
//Beginn: eightButton
eightButton.setText("8");
eightButton.setPreferredSize(new Dimension(50, 30));
eightButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
eightButtonGedrueckt(evt);
}
});
GridBagConstraints gbcEightButton = new
GridBagConstraints();
gbcEightButton.gridx = 1;
gbcEightButton.gridy = 1;
gbcEightButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(eightButton, gbcEightButton);
//Beginn: fiveButton
fiveButton.setText("5");
fiveButton.setPreferredSize(new Dimension(50, 30));
fiveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
fiveButtonGedrueckt(evt);
}
});
GridBagConstraints gbcFiveButton = new
GridBagConstraints();
gbcFiveButton.gridx = 1;
gbcFiveButton.gridy = 2;
gbcFiveButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(fiveButton, gbcFiveButton);
//Beginn: twoButton
twoButton.setText("2");
twoButton.setPreferredSize(new Dimension(50, 30));
twoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
twoButtonGedrueckt(evt);
}
});
GridBagConstraints gbcTwoButton = new
GridBagConstraints();
gbcTwoButton.gridx = 1;
gbcTwoButton.gridy = 3;
gbcTwoButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(twoButton, gbcTwoButton);
//Beginn: deleteButton
deleteButton.setText("C");
deleteButton.setPreferredSize(new Dimension(50, 30));
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
deleteButtonGedrueckt(evt);
}
});
GridBagConstraints gbcDeleteButton = new
GridBagConstraints();
gbcDeleteButton.gridx = 1;
gbcDeleteButton.gridy = 4;
gbcDeleteButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(deleteButton, gbcDeleteButton);
//Beginn: nineButton
nineButton.setText("9");
nineButton.setPreferredSize(new Dimension(50, 30));
nineButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
nineButtonGedrueckt(evt);
}
});
GridBagConstraints gbcNineButton = new
GridBagConstraints();
gbcNineButton.gridx = 2;
gbcNineButton.gridy = 1;
gbcNineButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(nineButton, gbcNineButton);
//Beginn: sixButton
sixButton.setText("6");
sixButton.setPreferredSize(new Dimension(50, 30));
sixButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
sixButtonGedrueckt(evt);
}
});
GridBagConstraints gbcSixButton = new
GridBagConstraints();
gbcSixButton.gridx = 2;
gbcSixButton.gridy = 2;
gbcSixButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(sixButton, gbcSixButton);
//Beginn: threeButton
threeButton.setText("3");
threeButton.setPreferredSize(new Dimension(50, 30));
threeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
threeButtonGedrueckt(evt);
}
});
GridBagConstraints gbcThreeButton = new
GridBagConstraints();
gbcThreeButton.gridx = 2;
gbcThreeButton.gridy = 3;
gbcThreeButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(threeButton, gbcThreeButton);
//Beginn: commaButton
commaButton.setText(",");
commaButton.setPreferredSize(new Dimension(50, 30));
commaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
commaButtonGedrueckt(evt);
}
});
GridBagConstraints gbcCommaButton = new
GridBagConstraints();
gbcCommaButton.gridx = 2;
gbcCommaButton.gridy = 4;
gbcCommaButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(commaButton, gbcCommaButton);
//Beginn: divideButton
divideButton.setText("/");
divideButton.setPreferredSize(new Dimension(50, 30));
divideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
divideButtonGedrueckt(evt);
}
});
GridBagConstraints gbcDivideButton = new
GridBagConstraints();
gbcDivideButton.gridx = 3;
gbcDivideButton.gridy = 1;
gbcDivideButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(divideButton, gbcDivideButton);
//Beginn: multiplyButton
multiplyButton.setText("*");
multiplyButton.setPreferredSize(new Dimension(50, 30));
multiplyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
multiplyButtonGedrueckt(evt);
}
});
GridBagConstraints gbcMultiplyButton = new
GridBagConstraints();
gbcMultiplyButton.gridx = 3;
gbcMultiplyButton.gridy = 2;
gbcMultiplyButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(multiplyButton, gbcMultiplyButton);
//Beginn: minusButton
minusButton.setText("-");
minusButton.setPreferredSize(new Dimension(50, 30));
minusButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
minusButtonGedrueckt(evt);
}
});
GridBagConstraints gbcMinusButton = new
GridBagConstraints();
gbcMinusButton.gridx = 3;
gbcMinusButton.gridy = 3;
gbcMinusButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(minusButton, gbcMinusButton);
//Beginn: plusButton
plusButton.setText("+");
plusButton.setPreferredSize(new Dimension(50, 30));
plusButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
plusButtonGedrueckt(evt);
}
});
GridBagConstraints gbcPlusButton = new
GridBagConstraints();
gbcPlusButton.gridx = 3;
gbcPlusButton.gridy = 4;
gbcPlusButton.insets = new Insets(1, 1, 1, 1);
getContentPane().add(plusButton, gbcPlusButton);
//Beginn: Menue - Datei - Beenden
fileMenu.setText("Datei");
exitMenuItem.setText("Beenden");
exitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
fileMenu.add(exitMenuItem);
mainMenu.add(fileMenu);
setJMenuBar(mainMenu);
pack();
}
private void sevenButtonGedrueckt(ActionEvent evt) {
showInput("7");
}
private void fourButtonGedrueckt(ActionEvent evt) {
showInput("4");
}
private void oneButtonGedrueckt(ActionEvent evt) {
showInput("1");
}
private void zeroButtonGedrueckt(ActionEvent evt) {
showInput("0");
}
private void resultButtonGedrueckt(ActionEvent evt) {
showInput("=");
}
private void eightButtonGedrueckt(ActionEvent evt) {
showInput("8");
}
private void fiveButtonGedrueckt(ActionEvent evt) {
showInput("5");
}
private void twoButtonGedrueckt(ActionEvent evt) {
showInput("2");
}
private void deleteButtonGedrueckt(ActionEvent evt) {
showInput("C");
}
private void nineButtonGedrueckt(ActionEvent evt) {
showInput("9");
}
private void sixButtonGedrueckt(ActionEvent evt) {
showInput("6");
}
private void threeButtonGedrueckt(ActionEvent evt) {
showInput("3");
}
private void commaButtonGedrueckt(ActionEvent evt) {
showInput(",");
}
private void divideButtonGedrueckt(ActionEvent evt) {
showInput("/");
}
private void multiplyButtonGedrueckt(ActionEvent evt) {
showInput("*");
}
private void minusButtonGedrueckt(ActionEvent evt) {
showInput("-");
}
private void plusButtonGedrueckt(ActionEvent evt) {
showInput("+");
}
private void showInput(String input) {
display.setText(input);
String displayString = display.getText();
display.setText(displayString);
}
// Starten:
public static void main(String[] args) {
CalcGui rechner = new CalcGui();
rechner.initComponents();
rechner.setVisible(true);
}
}
Hallo liebe Informatik Freunde.
Ich und ein Kumpel von mir gehen derzeit in die ABI-Klasse und müssen in Informatik einen Java Taschenrechner programmieren.
Das Interface haben wir bereits geschafft, dh Buttons, Ergebnisfeld etc werden angezeigt, aber wir haben leider keine Ahnung, wie man die Rechenoperation dazu bringt, zu funktionieren.
Kann uns vielleicht jemand von euch helfen? Wir können, wenn nötig, auch das bis jetzt programmierte reinstellen. Nötig sind lediglich die Grundrechenarten und eine Kommastellenfunktion.
Vielen Dank schon im Vorraus. =)