import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; import javax.swing.jfilechooser.

Similar documents
// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Homework/Program #5 Solutions

Advanced Network Programming Lab using Java. Angelos Stavrou

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

CS 335 Lecture 06 Java Programming GUI and Swing

Programming with Java GUI components

Using A Frame for Output

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;


public class demo1swing extends JFrame implements ActionListener{

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Graphical User Interfaces

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

Chapter 2 Introduction to Java programming

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Swing. A Quick Tutorial on Programming Swing Applications

Informatik II. // ActionListener hinzufügen btnconvert.addactionlistener(this); super.setdefaultcloseoperation(jframe.

Tema: Encriptación por Transposición

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Extending Desktop Applications to the Web

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

CS108, Stanford Handout #33. Sockets

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Essentials of the Java(TM) Programming Language, Part 1

Dev Articles 05/25/07 11:07:33

GOPAL RAMALINGAM MEMORIAL ENGINEERING COLLEGE. Rajeshwari nagar, Panapakkam, Near Padappai, Chennai

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Lösningsförslag till tentamen

How To Write A Program For The Web In Java (Java)

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

CS506 Web Design and Development Solved Online Quiz No. 01

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Object-Oriented Programming in Java

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Remote Method Invocation

Schueler-Organisiertes Lernen am Beispiel von Grafischen Benutzer-Schnittstellen in Java Tag der Offenen Tür - GTS 2008

13 File Output and Input

Event-Driven Programming

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Creating a Simple, Multithreaded Chat System with Java

Analysis Of Source Lines Of Code(SLOC) Metric

Building Java Programs

How Scala Improved Our Java

An Overview of Java. overview-1

CSE 8B Midterm Fall 2015

Assignment # 2: Design Patterns and GUIs

Tutorial Reference Manual. Java WireFusion 4.1

Building Java Programs

Essentials of the Java Programming Language

Division of Informatics, University of Edinburgh

Syllabus for CS 134 Java Programming

Chapter 2: Elements of Java

Introduction to the Java Programming Language

How to Convert an Application into an Applet.

Artificial Intelligence. Class: 3 rd

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Building a Multi-Threaded Web Server

AP Computer Science Java Subset

During the process of creating ColorSwitch, you will learn how to do these tasks:

Course Intro Instructor Intro Java Intro, Continued

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Using Files as Input/Output in Java 5.0 Applications

The Basic Java Applet and JApplet

Illustration 1: An applet showing constructed responses in an intuitive mode. Note the misprint!

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Skills and Topics for TeenCoder: Java Programming

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

public static void main(string[] args) { System.out.println("hello, world"); } }

Altas, Bajas y Modificaciones de Registros en tabla MYSQL

Building Java Programs

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Chapter 3. Input and output. 3.1 The System class

J a v a Quiz (Unit 3, Test 0 Practice)

Software Development in Java

GUI Components: Part 2

Factoring Numbers. Factoring numbers means that we break numbers down into the other whole numbers that multiply

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

JiST Graphical User Interface Event Viewer. Mark Fong

Crash Course in Java

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

MATH-0910 Review Concepts (Haugen)

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

CS 1302 Ch 19, Binary I/O

MVC Table Model / View / Controller

Medientechnik. Übung MVC

Lesson: All About Sockets

Assignment 4 Solutions

11. Applets, normal window applications, packaging and sharing your work

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Section 1.5 Exponents, Square Roots, and the Order of Operations

Explain the relationship between a class and an object. Which is general and which is specific?

!"# $ %!&' # &! ())*!$

Transcription:

/* */ 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);