2013.03.21.  Java GUI API  Layout Managers [Sample code] ShowFlowLayout.java 、 ShowGripLayout.java 、 ShowBorderLayout.java TestImageIcon.java.

Slides:



Advertisements
Similar presentations
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Advertisements

GUI Programming Two forms of graphics in Java – Graphics object to draw on – GUI components to interact with we visit GUI in chapter 12 and Graphics in.
Corresponds with Chapter 12
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 13 GUI Basics.
Lecture 16: GUI programming JFrame windows (p.247) Containers and layout managers (p.290) Inheriting GUI components (p.330) Chapter 12: GUI programming.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
1 GUI Basics First three slides are repeated from previous lecture notes! You will probably (in fact should) never write Java codes like the program examples.
Introduction to Java Programming Lecture 19 JAVA GUI Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Layout Managers Outline Java GUI API Layout Managers [Sample code] ShowFlowLayout.java 、 ShowGripLayout.java 、 ShowBorderLayout.java.
Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
CS3 - AWT/Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Lesson 5 GUI Programming AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
GUI Basics.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 GUI Basics.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Basics of GUI Programming Chapter 11 and Chapter 22.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
1 Chapter 16: Creating Windows. 2 Basics of GUI Programming How to create a Window. TryWindow.java You always need a JFrame component before creating.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Import javax.swing.*; class Check { public static void main(String[] args) { JFrame frame = new JFrame("Check"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//DO_NOTHING_ON_CLOSE.
Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
CIS265/506 Cleveland State University – Prof. Victor Matos
Swing JComponents.
Chapter 14 GUI Basics.
Graphical user interface with Swing by jose maria gonzalez pinto
Chapter 13: Advanced GUIs and Graphics
SWING Layout trong SWING.
Chapter 10 Getting Started with Graphics Programming
Chapter 12 GUI Basics.
Chapter 12 GUI Basics.
Advanced GUIs and Graphics
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

 Java GUI API  Layout Managers [Sample code] ShowFlowLayout.java 、 ShowGripLayout.java 、 ShowBorderLayout.java TestImageIcon.java

 GUI: Graphical User Interface  GUI classes can be classified into three group: 1. container classes 2. component classes 3. helper classes

 Container classes: ex: JFrame, Jpanel, Japplet  Component classes ex: JButton, JTextFiels, JTextArea, JComboBox, Jlist, JRadioButton  Helper classes ex: Graphics, Color, Font, FontMetrics, Dimension

//Add Component(JButton) to the JFrame import javax.swing.*; public class MyFrameWithComponents { public static void main(String[] args) { JFrame frame = new JFrame(); JButton jbtOK = new JButton("OK"); //add a button into frame frame.add(jbtOK); frame.setSize(400,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null);//ceter the frame frame.setVisible(true); }

 FlowLayout: ( 水流 ) 最簡單的 layout 方式, 方向由左到右  GridLayout: ( 格子 ) 以指定行與列的矩陣方式排列  BorderLayout: ( 邊緣 ) 分為五種邊緣, North,West,East,South, 指定其一

//Show FlowLayout 從左到右, 從上到下的佈置 import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JFrame; import java.awt.FlowLayout; import javax.swing.*; public class ShowFlowLayout extends JFrame { public ShowFlowLayout() { //Set FlowLayout, aligned left with horizontal gap 10 // and vertical gap 20 between components setLayout(new FlowLayout(FlowLayout.LEFT,10,20)); //add labels and text field to the frame add(new JLabel("First Name")); add(new JTextField(8)); add(new JLabel("MI")); add(new JTextField(1)); add(new JLabel("Last Name")); add(new JTextField(8)); } public static void main(String[] args) { ShowFlowLayout frame = new ShowFlowLayout(); frame.setTitle("ShowFlowLayout"); frame.setSize(200,200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

//Show GridLayout 格子狀的佈置, 指定行數和列數, 還有垂直跟水平間距 import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JFrame; import java.awt.GridLayout; import javax.swing.*; public class ShowGridLayout extends JFrame { public ShowGridLayout() { setLayout(new GridLayout(3,2,5,5));// (row, column, hgap, vgap) //add labels and text field to the frame add(new JLabel("First Name")); add(new JTextField(8)); add(new JLabel("MI")); add(new JTextField(1)); add(new JLabel("Last Name")); add(new JTextField(8)); } public static void main(String[] args) { ShowGridLayout frame = new ShowGridLayout(); frame.setTitle("ShowGridLayout"); frame.setSize(200,125); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

//Show BorderLayout 邊緣的佈置法 import javax.swing.JButton; import javax.swing.JFrame; import java.awt.BorderLayout; import javax.swing.*; public class ShowBorderLayout extends JFrame { public ShowBorderLayout() { setLayout(new BorderLayout(5,10));// Set BorderLayout with horizontal gap 5 and vertical gap 10 //add labels and text field to the frame add(new JButton("East"),BorderLayout.EAST); add(new JButton("South"),BorderLayout.SOUTH); add(new JButton("West"),BorderLayout.WEST); add(new JButton("North"),BorderLayout.NORTH); add(new JButton("CENTER"),BorderLayout.CENTER); } public static void main(String[] args) { ShowBorderLayout frame = new ShowBorderLayout(); frame.setTitle("ShowBorderLayout"); frame.setSize(300,200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

 Java currently supports three image formats: GIF 、 JPEG 、 PNG ImageIcon icon = new ImageIcon(“image/us.gif”);  An image icon can be displayed in a label or a button using: new JLabel(imageIcon) new JButton(imageIcon)

// 圖形介面加入 Image import javax.swing.*; import java.awt.*; public class TestImageIcon extends JFrame { private ImageIcon usIcon = new ImageIcon("./us.gif"); private ImageIcon myIcon = new ImageIcon("./my.jpg"); private ImageIcon frIcon = new ImageIcon("./fr.gif"); private ImageIcon ukIcon = new ImageIcon("./uk.gif"); public TestImageIcon() { setLayout(new GridLayout(1, 4, 5, 5)); add(new JLabel(usIcon)); add(new JLabel(myIcon)); add(new JButton(frIcon)); add(new JButton(ukIcon)); } /** Main method */ public static void main(String[] args) { TestImageIcon frame = new TestImageIcon(); frame.setTitle("TestImageIcon"); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1200, 250); frame.setVisible(true); }

 Ppt 下載: 