if i square 8, and get 64, then square that and get 4096, i then square 4096, and get "1.6777216E7" why is the E there already? is there any way to make it so that it does appear that soon, like on the windows calculator, with the windows calculator i have to press the x2 button 7 times to get the e, not only 3 times like on my calculator. Would i have to replace the doubles, and change it to something else, or is there a way to write a double to a jtextfield without having to convert it to a string first?
Code:import java.lang.Math;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.*;
import java.awt.Toolkit;
//import random shit
public class Main
{
private JPanel MainPanel = new JPanel(); // panel holds the button grid
public static JPanel screenPanel;
public static JFrame frame;
public static JTextField screen;
public static JMenu menu;
public static JMenu edit;
public static JMenu view;
public static JMenuBar menuBar;
public static JMenuItem exit = new JMenuItem("Exit");
public static JMenuItem about = new JMenuItem("About");
public static JMenuItem copy = new JMenuItem("Copy");
public static JCheckBoxMenuItem advanced = new JCheckBoxMenuItem("Advanced");
public static JCheckBoxMenuItem simple = new JCheckBoxMenuItem("Simple");
public static JPanel advancedButtons;
public static JButton log;
public static JButton pi;
public static JButton cube;
public static JButton square;
public static JButton sin;
public static JButton cos;
public static JButton tan;
public static JButton insin;
public static JButton incos;
public static JButton intan;
public static ButtonListener buttonListener;
//{"x²", "sin", "cos", "tan"},
//all the menu items, panels, and the frame
//numbers that allow calc to work
int operation = 0;
double secondnum1;
double firstnum1;
double oppAnswer = 0;
String firstNum;
String writeAnswer;
String secondNum;
double secondParse;
double firstParse;
boolean options = true;
String[][] buttonStrings = // use these Strings to create a JButton grid
{
{"CE", "\u221A", "1/x", "inv"},
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", ".", "=", "+"},
};
public Main()
{
int rowLength = buttonStrings.length;
int colLength = buttonStrings[0].length;
MainPanel.setLayout(new GridLayout(rowLength, colLength));
MainPanel.setSize(200,200);
buttonListener = new ButtonListener(); // create our ActionListener
for (int row = 0; row < rowLength; row++)
{
for (int col = 0; col < colLength; col++)
{
// for each String in the array, create a button that holds the string
String buttonString = buttonStrings[row][col];
JButton button = new JButton(buttonString); // this button here
button.addActionListener(buttonListener); // add the listener
MainPanel.add(button); // and add it to the panel
}
}
Main.about.addActionListener(new MenuActionListener());
Main.advanced.addActionListener(new MenuActionListener());
Main.simple.addActionListener(new MenuActionListener());
Main.exit.addActionListener(new MenuActionListener());
Main.copy.addActionListener(new MenuActionListener());
}
class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((e.getActionCommand().equals("About")) && (options == true)){
JOptionPane.showMessageDialog(null, "Java calculator created by Isaac Flaum");
options = false;
}
if ((e.getActionCommand().equals("Advanced"))){
advancedButtons.setVisible(true);
advanced.setSelected(true);
simple.setSelected(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
if ((e.getActionCommand().equals("Simple"))){
advancedButtons.setVisible(false);
advanced.setSelected(false);
simple.setSelected(true);
frame.pack();
}
if ((e.getActionCommand().equals("Copy"))){
String s = screen.getText();
StringSelection ss = new StringSelection(s);
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
systemClipboard.setContents(ss,ss);
}
if ((e.getActionCommand().equals("Exit"))){
System.exit(0);
}
}
}
public JComponent getComponent()
{
return MainPanel;
}
// here's the button listener class that of course implements action listener
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
options = true;
String screentext = screen.getText();
// we can easily extract the String held by the button pressed
String actionCommand = e.getActionCommand(); // here's the string
if (actionCommand == "0"){
screen.setText(screentext + "0");
}
if (actionCommand == "1"){
screen.setText(screentext + "1");
}
if (actionCommand == "2"){
screen.setText(screentext + "2");
}
if (actionCommand == "3"){
screen.setText(screentext + "3");
}
if (actionCommand == "4"){
screen.setText(screentext + "4");
}
if (actionCommand == "5"){
screen.setText(screentext + "5");
}
if (actionCommand == "6"){
screen.setText(screentext + "6");
}
if (actionCommand == "7"){
screen.setText(screentext + "7");
}
if (actionCommand == "8"){
screen.setText(screentext + "8");
}
if (actionCommand == "9"){
screen.setText(screentext + "9");
}
if (actionCommand == "*"){
firstNum = screen.getText();
operation = 1;
screen.setText("");
}
if (actionCommand == "/"){
firstNum = screen.getText();
operation = 2;
screen.setText("");
}
if (actionCommand == "+"){
firstNum = screen.getText();
operation = 3;
screen.setText("");
}
if (actionCommand == "-"){
firstNum = screen.getText();
operation = 4;
screen.setText("");
}
if (actionCommand == "."){
screen.setText(screentext + ".");
}
if (actionCommand == "CE"){
screen.setText("");
secondNum = "";
firstNum = "";
firstParse = 0;
secondParse = 0;
operation = 0;
writeAnswer = "";
}
if (actionCommand == "1/x"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = 1 / secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "inv"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = -1 * secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "\u221A"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
if (secondParse < 0){
screen.setText("Error");
}
else{
oppAnswer = Math.sqrt(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
}
if (actionCommand == "x²"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = secondParse * secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
//3.1415926535897932384626433832795 = pi
if (actionCommand == "sin"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.sin(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "asin"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.asin(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "cos"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.cos(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "acos"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.acos(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "tan"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.tan(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
//³
if (actionCommand == "x³"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = secondParse * secondParse * secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "atan"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.atan(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "log"){
secondNum = screen.getText();
secondParse = Double.parseDouble(secondNum);
oppAnswer = Math.log10(secondParse);
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if (actionCommand == "\u03C0"){
screen.setText("3.1415926535897932384626433832795");
}
if (actionCommand == "="){
secondNum = screen.getText();
screen.setText("");
firstParse = Double.parseDouble(firstNum);
secondParse = Double.parseDouble(secondNum);
if(operation == 1){
oppAnswer = firstParse * secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if(operation == 2){
oppAnswer = firstParse / secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if(operation == 4){
oppAnswer = firstParse - secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
if(operation == 3){
oppAnswer = firstParse + secondParse;
writeAnswer = Double.toString(oppAnswer);
screen.setText(writeAnswer);
}
}
}
}
// this calls our Swing code in a thread-safe manner
private static void createAndShowUI()
{
Main.frame = new JFrame("Calculator");
frame.setLayout(new BorderLayout());
frame.getContentPane().add(new Main().getComponent());
Main.screenPanel = new JPanel(); //panel to hold screen
Main.screenPanel.setLayout(new GridLayout(2, 1));
Main.screen = new JTextField(20);
Main.menu = new JMenu("File");
Main.view = new JMenu("View");
Main.menuBar = new JMenuBar();
Main.edit = new JMenu("Edit");
edit.add(copy);
menu.add(exit);
menu.add(about);
menuBar.add(menu);
menuBar.add(edit);
menuBar.add(view);
view.add(advanced);
view.add(simple);
log = new JButton("Log");
Main.screenPanel.add(Main.menuBar);
Main.screenPanel.add(screen);
frame.add(Main.screenPanel, BorderLayout.NORTH);
advancedButtons = new JPanel(new GridLayout(5,2)); //pain in the ass gridlayout manager
log = new JButton("log");
sin = new JButton("sin");
insin = new JButton("asin");
cos = new JButton("cos");
incos = new JButton("acos");
tan = new JButton("tan");
intan = new JButton("atan");
cube = new JButton("x³");
pi = new JButton("\u03C0");
square = new JButton("x²");
advancedButtons.add(log);
advancedButtons.add(square);
advancedButtons.add(sin);
advancedButtons.add(insin);
advancedButtons.add(cos);
advancedButtons.add(incos);
advancedButtons.add(tan);
advancedButtons.add(intan);
advancedButtons.add(cube);
advancedButtons.add(pi);
sin.addActionListener(Main.buttonListener);
cos.addActionListener(Main.buttonListener);
tan.addActionListener(Main.buttonListener);
insin.addActionListener(Main.buttonListener);
incos.addActionListener(Main.buttonListener);
intan.addActionListener(Main.buttonListener);
log.addActionListener(Main.buttonListener);
square.addActionListener(Main.buttonListener);
cube.addActionListener(Main.buttonListener);
pi.addActionListener(Main.buttonListener);
sin.setToolTipText("Calculates the Sine of a number");
cos.setToolTipText("Calculates the Cosine of a number");
tan.setToolTipText("Calculates the Tangent of a number");
insin.setToolTipText("Calculates the Inverse Sine of a number");
incos.setToolTipText("Calculates the Inverse Cosine of a number");
intan.setToolTipText("Calculates the Inverse Tangent of a number");
log.setToolTipText("Calculates the Logarithm of a number");
square.setToolTipText("Squares a number");
cube.setToolTipText("Cubes a number");
frame.add(advancedButtons,BorderLayout.EAST);
advanced.setSelected(false);
simple.setSelected(true);
advancedButtons.setVisible(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

