DOC PREVIEW
UMBC CMSC 341 - Building Java GUIs

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 341Building Java GUIs09/26/2007CMSC 341 GUI2Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity to do extra credit and have some fun on the project. GUIs are a good example of Object Oriented Programming. GUIs are another example of a container.09/26/2007CMSC 341 GUI3Java and GUIs There are two packages that generate GUI components in Java. java.awt javax.swing The AWT (Abstract Windows Toolkit)  Came first No platform independence Swing  Part of Java Foundation Classes (released with Java 2)  Built on top of the AWT Offers platform independence09/26/2007CMSC 341 GUI4Containers In Java, all GUI objects go into a Container. A top level container can stand alone in a web browser or in an operating system. JFrame JApplet Some containers may only be added to other containers. JPanel09/26/2007CMSC 341 GUI5JFrame Methods add(Object) - adds objects to the frame. setVisible(boolean) - makes the frame visible setLocation(int x, int y) – aligns top left corner of frame with coordinates on screen setSize(int width, int height) – sets size of frame in pixels  setDefaultCloseOperation(Windows.constants.EXIT_ON_CLOSE);09/26/2007CMSC 341 GUI6JFrame Codeimport javax.swing.*;import java.awt.*;public class UpperCaseConverter extends JFrame{public UpperCaseConverter(String name){super(name);setLocation(300, 100);setSize (400,300);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String args[]){UpperCaseConverter ucc = new UpperCaseConverter("Convert to Upper Case");ucc.setVisible(true);}}Constructor sets the title, size and location of the JFrameInstantiates JFrameand makes it visibleMakes program end when window is closed09/26/2007CMSC 341 GUI7JFrame Example The code on the previous page renders the following:09/26/2007CMSC 341 GUI8LayoutManagers Every container has an underlying default LayoutManager.  The LayoutManager determines the size of the objects in the container and how the objects will be laid out. The default LayoutManager for a JFrame is a BorderLayout.09/26/2007CMSC 341 GUI9BorderLayout Divides container into five regions BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.CENTER BorderLayout.EAST BorderLayout.WEST One component per region Component takes size of region Center region is greedy Components are added to center by default09/26/2007CMSC 341 GUI10BorderLayout Codeimport java.awt.*;import javax.swing.*;public class BorderLayoutExample extends JFrame {public BorderLayoutExample(String name) {super(name);setSize(300,300);add(new JButton("North"), BorderLayout.NORTH);add(new JButton("South"), BorderLayout.SOUTH);add(new JButton("East"), BorderLayout.EAST);add(new JButton("West"), BorderLayout.WEST);add(new JButton("Center"), BorderLayout.CENTER);}public static void main(String args[]) {BorderLayoutExample b = new BorderLayoutExample("BorderLayoutExample");b.setVisible(true);}}Specialized add method for adding to regions09/26/2007CMSC 341 GUI11BorderLayoutExample09/26/2007CMSC 341 GUI12JPanel However, we want to put several buttons in the North region of the GUI, but BorderLayoutonly allows one component per region… Add a second level container like a JPanel. JPanels have a FlowLayout manager by default.09/26/2007CMSC 341 GUI13FlowLayout Lays components in a fluid direction as determined by its orientation. By default, orientation is L -> R, T -> B. Possible to set the horizontal and vertical width between components. Components take preferred size. For buttons, preferred size is the size of the text within them.09/26/2007CMSC 341 GUI14JPanel and FlowLayout Code//omitting code here from previous examplepublic class UpperCaseConverter extends JFrame{//Since we are expecting to make these components to //react to user interaction we make them object dataJButton upper;JButton clear;public UpperCaseConverter(String name){//omitting code here from previous exampleJPanel top;top = new JPanel();upper = new JButton("UPPER");clear = new JButton("CLEAR");top.add(upper);top.add(clear);add(top, BorderLayout.NORTH);}//omitting code here from previous example}09/26/2007CMSC 341 GUI15JPanel and FlowLayout Example Code on previous page renders as follows: But, we also need a text field to enter text.09/26/2007CMSC 341 GUI16Second JPanelpublic class UpperCaseConverter extends JFrame{//code omitted from previous exampleJTextField input;public UpperCaseConverter(String name){//code omitted from previous exampleJPanel bottom = new JPanel();JLabel label = new JLabel("Enter text ->");input = new JTextField(20);bottom.add(label);bottom.add(input);add(bottom, BorderLayout.SOUTH);}//code omitted from previous example}JLabel may also take an Icon or both a String and Icon in its constructorJTextField takes an int which indicates the number of characters to be displayed09/26/2007CMSC 341 GUI17Second JPanel ExampleHow would we add a JTextArea to the center of our frame?09/26/2007CMSC 341 GUI18JTextArea Add JTextArea reference to object data so that it can be referenced by all member methods. Instantiate JTextArea reference in constructor method and add reference to the center of the JFrame.JTextArea output;output = new JTextArea(10, 20);add(output);Declare outside of methods so object dataConstructor for JTextAreatakes number of rows and columns09/26/2007CMSC 341 GUI19JTextArea ExampleNext time, we will make this GUI functional.09/26/2007CMSC 341 GUI20JComponent Methods There exists several JComponent methods that allow you to change the look of a component setBackground(Color) setForeground(Color) setFont(Font) setPreferredSize(Dimension) setAlignmentX(float) setAlignmentY(float)Values for all the argumentsof these methods are alreadydefined in Java.09/26/2007CMSC 341 GUI21More LayoutManagers Seven Basic Layout Managers in Java BorderLayout BoxLayout CardLayout FlowLayout GridLayout GridBagLayout OverlayLayout We will only focus on two more of these. GridLayout BoxLayout09/26/2007CMSC 341 GUI22GridLayout Creates a grid with number of rows and columns given in the constructor One component per cell Cells of equal size Component take the size of the cell09/26/2007CMSC 341 GUI23GridLayout Codeimport java.awt.*;import javax.swing.*;public class ButtonGrid extends JFrame {public ButtonGrid()


View Full Document

UMBC CMSC 341 - Building Java GUIs

Download Building Java GUIs
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Building Java GUIs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Building Java GUIs 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?