Bonjour,
j'arrive pas a faire fonctionné ma calculatrice
voici le code :
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
//import java.util.*;
//import java.awt.BorderLayout;
//import java.awt.GridLayout;
//import javax.swing.SwingConstants;
public class Calculator2 {
private JFrame frame; // Declare component Frame
private JTextField textFieldScreen; // Declare component TextField
private JButton button0; // Declare component Button
private JButton button1;
private JButton button2;
private JButton button3 ;
private JButton button4;
private JButton button5 ;
private JButton button6;
private JButton button7 ;
private JButton button8;
private JButton button9;
private JButton btnOneOverX ;
private JButton btnXsquare ;
private JButton buttonSquareroot;
private JButton btnXCube;
private JButton buttonPower;
private JButton btnLog;
private JButton btnLn ;
private JButton buttonNegative;
private JButton buttonParenthesis1;
private JButton buttonParenthesis2;
private JButton btnSin;
private JButton btnCos;
private JButton btnTan ;
private JButton btnN;
private JButton btnDel;
private JButton button_17;
private JButton btnC ;
private JButton buttonMultiply;
private JButton buttonAdd;
private JButton buttonDivide;
private JButton buttonSubstract;
private JButton btnOff;
private JButton buttonDot;
private JButton btnXx;private JButton buttonEqual;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator2 window = new Calculator2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
double Nbr1;
double Nbr2;
double Result;
String Operation;
/**
* Create the application.
*/
public Calculator2() {
frame = new JFrame("Epic Scientific calculator");
frame.setBounds(100, 100, 450, 462);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textFieldScreen = new JTextField();
textFieldScreen.setBounds(59, 12, 315, 58);
frame.getContentPane().add(textFieldScreen);
button0 = new JButton("0");
button0.setBounds(12, 380, 46, 25);
frame.getContentPane().add(button0);
button1 = new JButton("1");
button1.setBounds(12, 343, 46, 25);
frame.getContentPane().add(button1);
button2 = new JButton("2");
button2.setBounds(70, 343, 46, 25);
frame.getContentPane().add(button2);
button3 = new JButton("3");
button3.setBounds(128, 343, 46, 25);
frame.getContentPane().add(button3);button4 = new JButton("4");
button4.setBounds(12, 300, 46, 25);
frame.getContentPane().add(button4);
button5 = new JButton("5");
button5.setBounds(70, 300, 46, 25);
frame.getContentPane().add(button5);
button6 = new JButton("6");
button6.setBounds(128, 300, 46, 25);
frame.getContentPane().add(button6);
button7 = new JButton("7");
button7.setBounds(12, 262, 46, 25);
frame.getContentPane().add(button7);
button8 = new JButton("8");
button8.setBounds(70, 262, 46, 25);
frame.getContentPane().add(button8);
button9 = new JButton("9");
button9.setBounds(128, 262, 46, 25);
frame.getContentPane().add(button9);
btnOneOverX = new JButton("x − 1");
btnOneOverX.setBounds(12, 139, 55, 25);
frame.getContentPane().add(btnOneOverX);
btnXsquare = new JButton("x2");
btnXsquare.setBounds(70, 139, 46, 25);
frame.getContentPane().add(btnXsquare);
buttonSquareroot = new JButton("√");
buttonSquareroot.setBounds(128, 139, 42, 25);
frame.getContentPane().add(buttonSquareroot);
btnXCube = new JButton("x3");
btnXCube.setBounds(186, 139, 46, 25);
frame.getContentPane().add(btnXCube);
buttonPower = new JButton("^");
buttonPower.setBounds(244, 139, 52, 25);
frame.getContentPane().add(buttonPower);
btnLog = new JButton("log");
btnLog.setBounds(12, 182, 56, 25);
frame.getContentPane().add(btnLog);
btnLn = new JButton("ln");
btnLn.setBounds(70, 182, 47, 25);
frame.getContentPane().add(btnLn);buttonNegative = new JButton("(-)");
buttonNegative.setBounds(128, 182, 49, 25);
frame.getContentPane().add(buttonNegative);
buttonParenthesis1 = new JButton("(");
buttonParenthesis1.setBounds(186, 182, 39, 25);
frame.getContentPane().add(buttonParenthesis1);
buttonParenthesis2 = new JButton(")");
buttonParenthesis2.setBounds(244, 182, 39, 25);
frame.getContentPane().add(buttonParenthesis2);
btnSin = new JButton("sin");
btnSin.setBounds(12, 219, 55, 25);
frame.getContentPane().add(btnSin);
btnCos = new JButton("cos");
btnCos.setBounds(70, 219, 58, 25);
frame.getContentPane().add(btnCos);
btnTan = new JButton("tan");
btnTan.setBounds(128, 219, 58, 25);
frame.getContentPane().add(btnTan);
btnN = new JButton("n!");
btnN.setBounds(186, 219, 48, 25);
frame.getContentPane().add(btnN);
btnDel = new JButton("Del");
btnDel.setBounds(186, 262, 57, 25);
frame.getContentPane().add(btnDel);
button_17 = new JButton("x-");
button_17.setBounds(244, 219, 46, 25);
frame.getContentPane().add(button_17);
btnC = new JButton("C");
btnC.setBounds(244, 262, 46, 25);
frame.getContentPane().add(btnC);
buttonMultiply = new JButton("X");
buttonMultiply.setBounds(186, 300, 46, 25);
frame.getContentPane().add(buttonMultiply);
buttonAdd = new JButton("+");
buttonAdd.setBounds(186, 343, 46, 25);
frame.getContentPane().add(buttonAdd);
buttonDivide = new JButton("/");
buttonDivide.setBounds(244, 300, 46, 25);
frame.getContentPane().add(buttonDivide);buttonSubstract = new JButton("-");
buttonSubstract.setBounds(244, 343, 46, 25);
frame.getContentPane().add(buttonSubstract);
btnOff = new JButton("OFF");
btnOff.setBounds(244, 102, 66, 25);
frame.getContentPane().add(btnOff);
buttonDot = new JButton(".");
buttonDot.setBounds(70, 380, 46, 25);
frame.getContentPane().add(buttonDot);
btnXx = new JButton("x10^x");
btnXx.setBounds(128, 380, 74, 25);
frame.getContentPane().add(btnXx);
buttonEqual = new JButton("=");
buttonEqual.setBounds(244, 380, 46, 25);
frame.getContentPane().add(buttonEqual);
HandlerClass handler = new HandlerClass();
button0.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
button4.addActionListener(handler);
button5.addActionListener(handler);
button6.addActionListener(handler);
button7.addActionListener(handler);
button8.addActionListener(handler);
button9.addActionListener(handler);
buttonAdd.addActionListener(handler);
buttonSubstract.addActionListener(handler);
buttonDivide.addActionListener(handler);
buttonMultiply.addActionListener(handler);
buttonEqual.addActionListener(handler);
btnC.addActionListener(handler);
btnOneOverX.addActionListener(handler) ;
btnXsquare.addActionListener(handler) ;
buttonSquareroot.addActionListener(handler);
btnXCube.addActionListener(handler);
buttonPower.addActionListener(handler);
btnLog.addActionListener(handler);
btnLn.addActionListener(handler);
buttonNegative.addActionListener(handler);
buttonParenthesis1.addActionListener(handler);
buttonParenthesis2.addActionListener(handler);
btnSin.addActionListener(handler);
btnCos.addActionListener(handler);
btnTan.addActionListener(handler) ;btnN.addActionListener(handler);
btnDel.addActionListener(handler);
button_17.addActionListener(handler);
btnOff.addActionListener(handler);
buttonDot.addActionListener(handler);
btnXx.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent e){
String OnScreen = textFieldScreen.getText();
if(e.getSource()==button0){
String button0Text = OnScreen + button0.getText();
textFieldScreen.setText(button0Text); //Prints on screen what you type
}else if(e.getSource()==button1){
String button1Text = OnScreen + button1.getText();
textFieldScreen.setText(button1Text);
}else if(e.getSource()==button2){
String button2Text = OnScreen + button2.getText();
textFieldScreen.setText(button2Text);
}else if(e.getSource()==button3){
String button3Text = OnScreen + button3.getText();
textFieldScreen.setText(button3Text);
}else if(e.getSource()==button4){
String button4Text = OnScreen + button4.getText();
textFieldScreen.setText(button4Text);
}else if(e.getSource()==button5){
String button5Text = OnScreen + button5.getText();
textFieldScreen.setText(button5Text);
}else if(e.getSource()==button6){
String button6Text = OnScreen + button6.getText();
textFieldScreen.setText(button6Text);
}else if(e.getSource()==button7){
String button7Text = OnScreen + button7.getText();
textFieldScreen.setText(button7Text);
}else if(e.getSource()==button8){
String button8Text = OnScreen + button8.getText();
textFieldScreen.setText(button8Text);
}else if(e.getSource()==button9){
String button9Text = OnScreen + button9.getText();
textFieldScreen.setText(button9Text);
}else if(e.getSource()==buttonAdd){
Nbr1= Double.parseDouble(OnScreen); //Transforms String to Double
Operation="+";
String buttonPlusText = OnScreen + buttonAdd.getText();
textFieldScreen.setText(buttonPlusText);
}else if(e.getSource()==buttonSubstract){
Nbr1= Double.parseDouble(OnScreen);
Operation="-";
String buttonSubstractText = OnScreen + buttonSubstract.getText();textFieldScreen.setText(buttonSubstractText);
}else if(e.getSource()==buttonMultiply){
Nbr1= Double.parseDouble(OnScreen);
Operation="*";
String buttonMultiplyText = OnScreen + buttonMultiply.getText();
textFieldScreen.setText(buttonMultiplyText);
}else if(e.getSource()==buttonDivide){
Nbr1= Double.parseDouble(OnScreen);
Operation="/";
String buttonDivideText = OnScreen + buttonDivide.getText();
textFieldScreen.setText(buttonDivideText);
}else if(e.getSource()== btnC){
String buttonClearText = "";
textFieldScreen.setText(buttonClearText);
}else if(e.getSource()==buttonEqual){
String buttonEqualText = OnScreen + buttonEqual.getText();
textFieldScreen.setText(buttonEqualText);
Nbr2 = Double.parseDouble(OnScreen); //Need to find where to place it
if(Operation =="+"){
Result = Nbr1 + Nbr2;
textFieldScreen.setText(OnScreen + "=" + Result);
}else if(Operation =="-"){
Result = Nbr1 - Nbr2;
textFieldScreen.setText(OnScreen + "=" + Result);
}else if(Operation =="*"){
Result = Nbr1 * Nbr2;
textFieldScreen.setText(OnScreen + "=" + Result);
}else if(Operation =="/"){
Result = Nbr1 / Nbr2;
textFieldScreen.setText(OnScreen + "=" + Result);
}
} //Need to add the rest of the if operations
//Need to add the rest of the buttons
}
}
}
Afficher la suite
16 déc. 2015 à 06:01