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

Similar documents
Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

Building a Horizontal Menu in Dreamweaver CS3 Using Spry R. Berdan

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

Programming with Java GUI components

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

DIRECTIONS FOR SETTING UP LABELS FOR MARCO S INSERT STOCK IN WORD PERFECT, MS WORD AND ACCESS

JIDE Action Framework Developer Guide

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

CS 335 Lecture 06 Java Programming GUI and Swing

Microsoft Word 2010: How to Resize and Move Clip Art


Swing. A Quick Tutorial on Programming Swing Applications

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

First Bytes Programming Lab 2

Graphical User Interfaces

ADOBE DREAMWEAVER CS3 TUTORIAL

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

Microsoft Word defaults to left justified (aligned) paragraphs. This means that new lines automatically line up with the left margin.

Assignment # 2: Design Patterns and GUIs

Instructions for Formatting MLA Style Papers in Microsoft Word 2010

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

Using A Frame for Output

Participant Guide RP301: Ad Hoc Business Intelligence Reporting

When To Work Scheduling Program

Instructions for Formatting APA Style Papers in Microsoft Word 2010

How to create pop-up menus

Homework/Program #5 Solutions

Mouse Event Handling (cont.)

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

Manual For Using the NetBeans IDE

CONTENTM WEBSITE MANAGEMENT SYSTEM. Getting Started Guide

Editing your Website User Guide

Netigate User Guide. Setup Introduction Questions Text box Text area Radio buttons Radio buttons Weighted...

Karsten Lentzsch JGoodies SWING WITH STYLE

Interactive Voting System. IVS-Basic IVS-Professional 4.4

How To Change Your Site On Drupal Cloud On A Pcode On A Microsoft Powerstone On A Macbook Or Ipad (For Free) On A Freebie (For A Free Download) On An Ipad Or Ipa (For

Tips for optimizing your publications for commercial printing

Summer Internship 2013

How to create labels using a Microsoft Access data file?

Using Adobe Dreamweaver CS4 (10.0)

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

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

How to Use the Drawing Toolbar in Microsoft Word

Mobile Web Site Style Guide

Pivot Tables & Pivot Charts

Adobe Dreamweaver CC 14 Tutorial

Creating Personal Web Sites Using SharePoint Designer 2007

How to create buttons and navigation bars

GUI Components: Part 2

The first program: Little Crab

Raffle tickets How to set up the ticket body

SECTION 5: Finalizing Your Workbook

GelAnalyzer 2010 User s manual. Contents

1001ICT Introduction To Programming Lecture Notes

Joomla Article Advanced Topics: Table Layouts

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

2 The first program: Little Crab

Managing your Joomla! 3 Content Management System (CMS) Website Websites For Small Business

Learning Services IT Guide. Access 2013

How to - Newsletter & Memo Section

Millennium Learning Centres How to scan documents you want to edit

Caldes CM12: Content Management Software Introduction v1.9

Working with Video in PowerPoint 2010

Create a Poster Using Publisher

Course Project Lab 3 - Creating a Logo (Illustrator)

LaGuardia Community College Thomson Ave, Long Island City, New York Created by ISMD s Dept. Training Team. Overview

Publisher 2010 Cheat Sheet

How to Create a Resume Using Microsoft Word

Working with the new enudge responsive styles

Customizing your Blackboard Course

Programming Methods & Java Examples

Introduction to OpenOffice Writer 2.0 Jessica Kubik Information Technology Lab School of Information University of Texas at Austin Fall 2005

OpenOffice Installation and Usage Guide

Excel 2007 A Beginners Guide

Design. in NetBeans 6.0

Instructions for Creating a Poster for Arts and Humanities Research Day Using PowerPoint

Recreate your Newsletter Content and Layout within Informz (Workshop) Monica Capogna and Dan Reade. Exercise: Creating two types of Story Layouts

Organizing image files in Lightroom part 2

Introduction to MS WINDOWS XP

Ad Hoc Reporting. Usage and Customization

KOMPOZER Web Design Software

HOW TO WRITE A THESIS IN WORD?

Introduction to Open Atrium s workflow

Drupal Training Guide

paragraph(s). The bottom mark is for all following lines in that paragraph. The rectangle below the marks moves both marks at the same time.

TheFinancialEdge. Reports Guide for Accounts Receivable

Joomla! 2.5.x Training Manual

If you know exactly how you want your business forms to look and don t mind detail

HIT THE GROUND RUNNING MS WORD INTRODUCTION

JOOMLA 2.5 MANUAL WEBSITEDESIGN.CO.ZA

Creating Forms with Acrobat 10

customer community Getting started Visual Editor Guide!

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

Practical Example: Building Reports for Bugzilla

WebFOCUS BI Portal: S.I.M.P.L.E. as can be

BookMaker. User Guide Windows & Mac OSX. Devalipi Software The Easiest Professional Digital Book Printing Tool

Create a Web Page with Dreamweaver

Utilizing Microsoft Access Forms and Reports

Transcription:

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

So, what are we up to Today, we are going to create the GUI for the calculator you all know and love. It will basically be a crash course in putting together simple interfaces using Swing. You will learn... Making and placing GUI components Creating GUIs using JPanel to break down the design Decorating GUI Components, via Fonts, Colors (sic) and Insets Using Layout Managers JMenuBar, and JMenu

The Calculator

The Calculator, broken into 3 JPanels

So What? Well, we have now gone from 1 large problem, to three small ones. We will do our best to create each JPanel, one at a time. So, starting with the simplest, the Jpanel containing the single JTextField.

textpanel.java class textpanel extends JPanel { JTextField output; textpanel() { // set size, initial text, font and text alignment output = new JTextField("0.",28); output.setfont(new Font("Courier", Font.PLAIN, 16)); output.sethorizontalalignment(4); //add to panel this.add(output); // change background color to be that gray color Color background_color = new Color(241,237,222); this.setbackground(background_color); } // main removed. }

So lets have a look at that (goto text editor, show main etc ) There were some new methods there, that you wouldn't have seen before. You do not need to learn these off by heart, they are all available at http://java.sun.com You can look up this site all the time during labs, but not during your lab exam, or your final exam. I will provide the signatures of any of the necessary methods in these occasions (I think).

worked that out after a while) Happy Enough with textpanel? Great, next is the first row of buttons These bad boys... So, what we have here is 4 buttons, 3 with red text, and 2 of them are heavily padded Normally buttons shrink/expand to fit their label. CE and C aren't doing that. The first yoke, is actually a disabled button (I

buttonpanel1.java /* The code for the whole Panel won't fit in a slide, so I'll explain one Jbutton and hopefully you genii can work out the rest */ Jbutton CE; // This is RGB calue of the windows button color Color background_color = new Color(241,237,222); /* The string passed in to constructor is the label that appears on the Jbutton */ CE = new Jbutton("CE"); // This bit sets the button text to be red. CE.setForeground(Color.red); // This sets the background color to be the color above CE.setBackground(buttons_color); /* This sets the borders, we'll be talking about this in a second */ CE.setMargin(new Insets(6,29,6,29)); // Then add it to buttonpanel1 add(ce);

Insets? Insets is the amount of space between the component, and its border. For a button its like You create them like so Insets(int top, int left, int bottom, int right) All distances are in pixels.

So lets have a look at buttonpanel1.java (goto text editor and explain) (this is the bit where you realise that attending lectures was probably a good idea)

2 down, one to go. Now for the slight challenge, the 24 buttons Some buttons are blue, some aren't, lot of copying and pasting to specify this. Basically every button has the following things... Insets buttonmargin = new Insets(5,1,5,1); MC = new Jbutton("MC"); //name MC.setBackground(buttons_color); //color fg MC.setForeground(Color.red); // color bg MC.setMargin(buttonMargin); //margins

The GridLayout we are using setlayout(new GridLayout(4,6,5,5)); GridLayout places components as you add them, starting top right, ending bottom left. You can not directly place a component in a specific place easily. The 4 and 6 relate the to the 4x6 layout, the 5 and 5 relate to the horizontal and vertical gap between components ( in pixels.) Again, lets take a look at the code itself. (buttonpanel2.java)

Putting it all together Well, we have built the 3 sub components, so we now need to build the Frame that contains the three of them, in the correct fashion. We will also add a JMenu bar to complete the look of the program.

CalcFrame.java (the class members) class CalcFrame extends JFrame { /* These are our three panels that we already wrote */ textpanel tp; buttonpanel1 bp1; buttonpanel2 bp2; /** This is the menu stuff **/ JMenuBar jmb; JMenu jmi1,jmi2,jmi3;

ClassFrame.java (the constructor) CalcFrame() { super("calculator"); // set title to Calculator Container cp = getcontentpane(); // we saw this already cp.setlayout(new FlowLayout()); // we have to use flow cp.setbackground(new Color(241,237,222) ); tp = new textpanel(); // create our three components bp1 = new buttonpanel1(); bp2 = new buttonpanel2(); cp.add(tp); cp.add(bp1);// and add them cp.add(bp2);// in order... // not finished

CalcFrame.java (the menu) The JMenuBar is the top level line. It is initially empty. A JMenu is a single menu (e.g File, or Edit ) A JMenuItem is an item inside a JMenu, (e.g Open, Cut) To get a submenu, simply add a JMenu to a JMenu.

JMenus and their associates One JMenuBar 5 JMenus 7 JMenuItems

So lets have a look at our Menu Goto Text Editor Explain stuff.

The final bits of code this.setdefaultcloseoperation(exit_on_close); this.setsize(340,320); // I worked out the size afterward setvisible(true); setresizable(false); // This prevents it getting messed up To make the actual CalcFrame appear... public static void main(string args[]) { } CalcFrame cf = new CalcFrame();

So, how did we do? Microsoft Windows Calculator Version 5.1 The CS211 Calculator

Good enough It's close. Certainly not perfect. We could have set the Look And Feel to be MS Windows, that would get the buttons spot on. (If we have time, I'll elaborate) Its off a few pixels here and there, but nothing major. If we spent longer it'd be a waste of time. So, you can now build uncomplicated GUIs. Cant you?

What do you mean Look and Feel Java has 3 looks, (by looks I mean how the components look). Windows, Motif, and Metal. These are good/bad. If you use them, you end up with a few difficulties when you go cross platform. But seeing as its only 2 lines of code I'll give you all a look.

Altering Look and Feel // First an import statement import com.sun.java.swing.plaf.windows.windowslookandfeel; /* And then, in your Frame class ( whatever your base container is (calcframe.java in this case) */ try { // This line sets the look and feel UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.windowslookandfeel"); } catch(exception cnfe) // you have to catch the exceptions { System.out.println("Error changing look and feel"); } // This line tells the application to update itself SwingUtilities.updateComponentTreeUI(getContentPane()); // and thats it!

Lets look at the results! If it wasn't for the little java icon, you'd be struggling! There you have it a (near) perfect clone.

End of Lecture So, what did we learn? How to break a user interface into sub components, making it easier to create How to decorate buttons and text fields so they look appropriate How to use Insets to change the padding on components How JMenuBars work GridLayout and FlowLayout How to alter the Look and Feel of an GUI

Your Lab this week... Needs to be decided upon.