Guten Tag,
habe ein kleines Problem mit meinem Taschenrechner, habe in der Schule eine Aufgabe erweitert in sofern, dass ich
JFrames benutze etc., aber eine Anweisung funktioniert nicht so wie ich das möchte.
Wenn ich potenziere kommt immer 3125 raus.
MfG
Gaming4LifeNOT
Source Code
Spoiler anzeigen
package trechner;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
*
* @author 1stNox
*/
public class trechner extends JFrame implements ActionListener {
/**
* @param args the command line arguments
*/
private JTextField txtvar1 = new JTextField();
private JTextField txtvar2 = new JTextField();
private JTextField txtErg = new JTextField();
private JLabel lblVar1 = new JLabel("Variable 1");
private JLabel lblVar2 = new JLabel("Variable 2");
private JLabel lblErg = new JLabel("Ergebnis");
private JButton btplus = new JButton("+");
private JButton btminus = new JButton("-");
private JButton btmal = new JButton("*");
private JButton btgeteilt = new JButton("/");
private JButton btwurzel = new JButton("\u221a");
private JButton btpotenz = new JButton("^X");
private JButton btmodulo = new JButton("%");
private JButton btDel = new JButton("Löschen");
public trechner()
{
Container cp = getContentPane();
cp.setLayout(null);
lblVar1.setBounds(120,10,60,20);
txtvar1.setBounds(190,10,120,20);
lblVar2.setBounds(120,40,190,20);
txtvar2.setBounds(190,40,120,20);
lblErg.setBounds(120,70,190,20);
txtErg.setBounds(190,70,120,20);
txtErg.setEditable(false);
btplus.setBounds(120,120,50,20);
btminus.setBounds(170,120,50,20);
btmal.setBounds(220,120,50,20);
btgeteilt.setBounds(270,120,50,20);
btwurzel.setBounds(200,150,50,20);
btpotenz.setBounds(150,150,50,20);
btmodulo.setBounds(100,150,50,20);
btDel.setBounds(250,150,100,20);
cp.add(lblVar1);
cp.add(lblVar2);
cp.add(lblErg);
cp.add(txtvar1);
cp.add(txtvar2);
cp.add(txtErg);
cp.add(btplus);
cp.add(btminus);
cp.add(btmal);
cp.add(btgeteilt);
cp.add(btwurzel);
cp.add(btpotenz);
cp.add(btmodulo);
cp.add(btDel);
setTitle("Taschenrechner");
setSize(450,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
btplus.addActionListener(this);
btminus.addActionListener(this);
btmal.addActionListener(this);
btgeteilt.addActionListener(this);
btwurzel.addActionListener(this);
btpotenz.addActionListener(this);
btmodulo.addActionListener(this);
btDel.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
double Var1,Var2;
Object obj = event.getSource();
if (obj == btplus) {
Var1 = Double.parseDouble(txtvar1.getText());
Var2 = Double.parseDouble(txtvar2.getText());
Var1 = Var1+Var2;
txtErg.setText(""+Var1);
}//end of if
if (obj == btminus) {
Var1 = Double.parseDouble(txtvar1.getText());
Var2 = Double.parseDouble(txtvar2.getText());
Var1 = Var1-Var2;
txtErg.setText(""+Var1);
}//end of if
if (obj == btmal) {
Var1 = Double.parseDouble(txtvar1.getText());
Var2 = Double.parseDouble(txtvar2.getText());
Var1 = Var1*Var2;
txtErg.setText(""+Var1);
}//end of if
if (obj == btgeteilt) {
Var1 = Double.parseDouble(txtvar1.getText());
Var2 = Double.parseDouble(txtvar2.getText());
Var1 = Var1/Var2;
txtErg.setText(""+Var1);
}//end of if
if (obj == btwurzel) {
Var1 = Double.parseDouble(txtvar1.getText());
Var1 = Math.sqrt(Var1);
txtErg.setText(""+Var1);
}
if (obj == btpotenz) {
Var1 = Double.parseDouble(txtvar1.getText());
Var2 = Double.parseDouble(txtvar1.getText());
Var1 = Math.pow(Var1, Var2);
txtErg.setText(""+Var1);
}
if (obj == btmodulo) {
Var1 = Double.parseDouble(txtvar1.getText());
Var2 = Double.parseDouble(txtvar2.getText());
Var1 = Var1%Var2;
txtErg.setText(""+Var1);
}
if (obj == btDel) {
txtvar1.setText("");
txtvar2.setText("");
txtErg.setText("");
}//end of if
}
public static void main(String[] args) {
// TODO code application logic here
new trechner();
}
}