Servus, ich habe heute ein Taschenrechner nach einen Video nachgebaut (
) (siehe min 14 als Beispiel)
soweit so gut aber ich habe das Problem das er die zahl des ersten textField nicht auf das Textfield2 überträgt
ich habe gesucht und gesucht aber soweit ich mit meinen schlechten Basis wissen sehen konnte hab ich den Fehler nicht gefunden ich bitte um eure Hilfe !!
Lg Peat
soweit so gut aber ich habe das Problem das er die zahl des ersten textField nicht auf das Textfield2 überträgt
ich habe gesucht und gesucht aber soweit ich mit meinen schlechten Basis wissen sehen konnte hab ich den Fehler nicht gefunden ich bitte um eure Hilfe !!
Lg Peat
Java:
package Taschenrechner02;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Taschenrechner123 {
private JFrame frame;
private JTextField textField;
private JTextField textField2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Taschenrechner123 window = new Taschenrechner123();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Taschenrechner123() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(500, 100, 288, 403);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBorder(new LineBorder(new Color(171, 173, 179)));
textField.setBounds(10, 47, 219, 34);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField2 = new JTextField();
textField2.setHorizontalAlignment(SwingConstants.RIGHT);
textField2.setColumns(10);
textField2.setBorder(new LineBorder(new Color(171, 173, 179)));
textField2.setBounds(124, 11, 105, 40);
frame.getContentPane().add(textField2);
JEditorPane editorPane = new JEditorPane();
editorPane.setBorder(new LineBorder(new Color(0, 0, 0)));
editorPane.setBounds(10, 11, 219, 70);
frame.getContentPane().add(editorPane);
JButton btnNewButton = new JButton("7");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+7);
}
});
btnNewButton.setBounds(10, 97, 47, 40);
frame.getContentPane().add(btnNewButton);
JButton button = new JButton("4");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+4);
}
});
button.setBounds(10, 148, 47, 40);
frame.getContentPane().add(button);
JButton button_1 = new JButton("1");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+1);
}
});
button_1.setBounds(10, 193, 47, 40);
frame.getContentPane().add(button_1);
JButton button_2 = new JButton("0");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+0);
}
});
button_2.setBounds(10, 244, 104, 40);
frame.getContentPane().add(button_2);
JButton button_3 = new JButton("8");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+8);
}
});
button_3.setBounds(67, 97, 47, 40);
frame.getContentPane().add(button_3);
JButton button_4 = new JButton("5");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+5);
}
});
button_4.setBounds(67, 148, 47, 40);
frame.getContentPane().add(button_4);
JButton button_5 = new JButton("2");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+2);
}
});
button_5.setBounds(67, 193, 47, 40);
frame.getContentPane().add(button_5);
JButton button_6 = new JButton("=");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int one=Integer.parseInt(textField.getText());
int two=Integer.parseInt(textField2.getText());
if(aktionname.getText().equals("sub"))
{ int sub=one-two;
textField2.setText(String.valueOf(sub));
}
if(aktionname.getText().equals("add"))
{ int add=one+two;
textField2.setText(String.valueOf(add));
}
if(aktionname.getText().equals("mul"))
{ int mul=one*two;
textField2.setText(String.valueOf(mul));
}
if(aktionname.getText().equals("div"))
{ int div=one/two;
textField2.setText(String.valueOf("div"));
}
}
});
button_6.setBounds(124, 244, 47, 40);
frame.getContentPane().add(button_6);
JButton button_7 = new JButton("9");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+9);
}
});
button_7.setBounds(124, 97, 47, 40);
frame.getContentPane().add(button_7);
JButton button_8 = new JButton("6");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+6);
}
});
button_8.setBounds(124, 148, 47, 40);
frame.getContentPane().add(button_8);
JButton button_9 = new JButton("3");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+3);
}
});
button_9.setBounds(124, 193, 47, 40);
frame.getContentPane().add(button_9);
JButton button_11 = new JButton("*");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField2.getText());
aktionname.setText("multi");
textField.setText(null);
}
});
button_11.setBounds(182, 97, 47, 40);
frame.getContentPane().add(button_11);
JButton button_12 = new JButton("/");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField2.getText());
aktionname.setText("div");
textField2.setText(null);
}
});
button_12.setBounds(182, 148, 47, 40);
frame.getContentPane().add(button_12);
JButton button_13 = new JButton("-");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField2.getText());
aktionname.setText("sub");
textField2.setText(null);
}
});
button_13.setBounds(182, 193, 47, 40);
frame.getContentPane().add(button_13);
JButton button_14 = new JButton("+");
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField2.getText());
aktionname.setText("add");
textField2.setText(null);
}
});
button_14.setBounds(182, 244, 47, 40);
frame.getContentPane().add(button_14);
aktionname = new JLabel("");
aktionname.setBounds(20, 295, 113, 24);
frame.getContentPane().add(aktionname);
JButton btnC = new JButton("C");
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
textField2.setText (null);
aktionname.setText(null);
}
});
btnC.setBounds(182, 295, 41, 24);
frame.getContentPane().add(btnC);
}
private JLabel aktionname;
private JTextField textField_1;
private JTextField textField_2;
}