/* */ Date: 03/01/13 Purpose: Demonstrate GUI and Event Handling Input: GUI user input numeric values Output: GUI Resulting calculations Author: Crosby, David Class: CS 1302A Program: TestMyOwnCalculator.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; import javax.swing.jfilechooser.*; public class TestMyOwnCalculator_trySave{ public static void main(string[] args)throws Exception{ CalcFrame frame = new CalcFrame(); catch(exception e){ System.out.println(e); //End of main method //End of Driver class CalcFrame extends JFrame{ //Declare variables double num1 = 0, num2 = 0, result = 0; String outputtext = ""; JTextField jtxtfield1, jtxtfield2; JTextArea textpane; Scanner input ; PrintWriter outfile; //no-arg constructor public CalcFrame()throws Exception{ getcontentpane().setlayout(null);
setupgui(); setdefaultcloseoperation(jframe.exit_on_close); //builds gui using separate methods public void setupgui()throws Exception{ //get items getaddbutton(); getsubtractbutton(); getmultiplybutton(); getdividebutton(); getremainderbutton(); getexponentbutton(); getsavebutton(); getexitbutton(); gettxtfield1(); gettxtfield2(); getinstlabel(); gettextarea(); get1stlabel(); get2ndlabel(); //set frame specs settitle("my Own Calculator"); setsize(370,320); setlocationrelativeto(null); // centers GUI on screen setvisible(true); setresizable(true); //Gets add button and implements listener public void getaddbutton(){ JButton jbtadd = new JButton(); jbtadd.setlocation(10,40); jbtadd.setsize(100,25); jbtadd.settext("add"); jbtadd.addactionlistener(new ActionListener(){ compadd();
); getcontentpane().add(jbtadd); //Gets subtract button and implements listener public void getsubtractbutton(){ JButton jbtsubtract = new JButton(); jbtsubtract.setlocation(10,70); jbtsubtract.setsize(100,25); jbtsubtract.settext("subtract"); jbtsubtract.addactionlistener(new ActionListener(){ compsub(); ); getcontentpane().add(jbtsubtract); //Gets multiply button and implements listener public void getmultiplybutton(){ JButton jbtmultiply = new JButton(); jbtmultiply.setlocation(10,100); jbtmultiply.setsize(100,25); jbtmultiply.settext("multiply"); jbtmultiply.addactionlistener(new ActionListener(){ compmult(); ); getcontentpane().add(jbtmultiply); //Gets divide button and implements listener public void getdividebutton(){ JButton jbtdivide = new JButton(); jbtdivide.setlocation(10,130); jbtdivide.setsize(100,25); jbtdivide.settext("divide");
getcontentpane().add(jbtdivide); jbtdivide.addactionlistener(new ActionListener(){ compdiv(); ); //Gets remainder button and implements listener public void getremainderbutton(){ JButton jbtremainder = new JButton(); jbtremainder.setlocation(10,160); jbtremainder.setsize(100,25); jbtremainder.settext("remainder"); jbtremainder.addactionlistener(new ActionListener(){ compremain(); ); getcontentpane().add(jbtremainder); //Gets exponent button and implements listener public void getexponentbutton(){ JButton jbtexponent = new JButton(); jbtexponent.setlocation(10,190); jbtexponent.setsize(100,25); jbtexponent.settext("exponent"); jbtexponent.addactionlistener(new ActionListener(){ compexpon(); ); getcontentpane().add(jbtexponent); //Gets Save button and implements listener
public void getsavebutton(){ JButton jbtsave = new JButton(); jbtsave.setlocation(10,220); jbtsave.setsize(100,25); jbtsave.settext("save"); jbtsave.addactionlistener(new ActionListener(){ compsave(); ); getcontentpane().add(jbtsave); //Gets Exit button and implements listener public void getexitbutton(){ JButton jbtexit = new JButton(); jbtexit.setlocation(10,250); jbtexit.setsize(100,25); jbtexit.settext("exit"); jbtexit.addactionlistener(new ActionListener(){ System.exit(0); ); getcontentpane().add(jbtexit); // adds 1st numeric text field public void gettxtfield1(){ jtxtfield1 = new JTextField(); jtxtfield1.setlocation(200,10); jtxtfield1.setsize(55,25); jtxtfield1.settext(""); jtxtfield1.setcolumns(1); getcontentpane().add(jtxtfield1); // adds 2nd numeric text field
public void gettxtfield2(){ jtxtfield2 = new JTextField(); jtxtfield2.setlocation(290,10); jtxtfield2.setsize(55,25); jtxtfield2.settext(""); jtxtfield2.setcolumns(1); getcontentpane().add(jtxtfield2); // gets instruction label public void getinstlabel(){ JLabel jlbinstructions = new JLabel(); jlbinstructions.setlocation(10,10); jlbinstructions.setsize(150,25); jlbinstructions.settext("enter Two Numeric Values"); getcontentpane().add(jlbinstructions); /* Establishes a feild to display results while keeping any previous results visible*/ public void gettextarea(){ textpane = new JTextArea(); JScrollPane scrollpane = new JScrollPane(textPane); add(scrollpane, BorderLayout.CENTER); scrollpane.setsize(225,240); scrollpane.setlocation(120,40); textpane.settext(""); textpane.setrows(5); textpane.setcolumns(5); getcontentpane().add(scrollpane); // labels 1st entry order public void get1stlabel(){ JLabel jlb1st = new JLabel(); jlb1st.setlocation(175,10); jlb1st.setsize(25,25); jlb1st.settext("1st"); getcontentpane().add(jlb1st);
// labels 2nd entry order public void get2ndlabel(){ JLabel jlb2nd = new JLabel(); jlb2nd.setlocation(265,10); jlb2nd.setsize(25,25); jlb2nd.settext("2nd"); getcontentpane().add(jlb2nd); //parses text into doubles public void parsethem(){ num1 = Double.parseDouble(jtxtField1.getText()); num2 = Double.parseDouble(jtxtField2.getText()); catch (Exception e){ System.out.println(e); // sets output text public void output(){ textpane.settext(outputtext); jtxtfield1.requestfocus(); // calculates Sums public void compadd(){ result = (num1 + num2); outputtext += String.format("%-10s\t%-5.1f%-3s%-5.1f%-3s%-5.2f\n","Sum:",num1,"+", num2, "=", result); // calculates Differences public void compsub(){ result = (num1 - num2); outputtext += String.format("%-10s\t%-5.1f%-3s%-5.1f%-3s%-5.2f\n","Difference:", num1,"-", num2, "=", result);
// calculates Products public void compmult(){ result = (num1 * num2); outputtext += String.format("%-10s\t%-5.1f%-3s%-5.1f%-3s%-5.2f\n","Product:", num1,"x", num2, "=", result); // calculates Quotients public void compdiv(){ if (num2 == 0) throw new ArithmeticException("Divisor cannot be zero"); result = (num1 / num2); outputtext += String.format("%-10s\t%-5.1f%-3s%-5.1f%-3s%-5.2f\n","Quotient:", num1," ", num2, "=", result); catch (ArithmeticException ex){ JOptionPane.showMessageDialog(null,"Divisor cannot be zero"); // calculates Remainders public void compremain(){ if (num2 == 0) throw new ArithmeticException("Divisor cannot be zero"); result = (num1 % num2); outputtext += String.format("%-10s\t%-5.1f%-3s%-5.1f%-3s%-5.2f\n","Remainder:", num1,"%", num2, "=", result); catch (ArithmeticException ex){ JOptionPane.showMessageDialog(null,"Divisor cannot be zero");
// calculates exponential values public void compexpon(){ result = (Math.pow(num1,num2)); outputtext += String.format("%-10s\t%-5.1f%-3s%-5.1f%-3s%-5.2f\n","Exponent:", num1,"^", num2, "=", result); // creates an output file to archive results public void compsave(){ JFileChooser fc = new JFileChooser(); if (fc.showsavedialog(null)== JFileChooser.APPROVE_OPTION){ // Create a PrintWriter for the file & Get the selected file File file = fc.getselectedfile(); PrintWriter outfile = new PrintWriter(new FileWriter(file)); outfile.print(outputtext); outfile.close(); else System.out.println("Save command cancelled by user."); catch (Exception e){ System.out.println(e);