Chapter 10 GUI1 例 : LunarPhases, 月亮盈亏 (JB demo) 如何使用 Compound Bounder 如何使用 Combo Boxes 如何使用 Loading Multiple Images.

Slides:



Advertisements
Similar presentations
Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Advertisements

Graphical User Interfaces (Part IV)
TCU CoSc Programming with Java Visual Design (Chapter 5)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Graphic User Interfaces Layout Managers Event Handling.
JAVA 程式語言入門 (II).  版面配置  事件驅動  Ch14_01.java 1. import javax.swing.*; 2. import java.awt.*; 3. class Ch14_01 4. { 5. public static void main(String.
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.
Corresponds with Chapter 12
 Java GUI API  Layout Managers [Sample code] ShowFlowLayout.java 、 ShowGripLayout.java 、 ShowBorderLayout.java TestImageIcon.java.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Introduction to Java Programming Lecture 19 JAVA GUI Programming.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
7-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : User Interface Components with.
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.
CS377A: A Programming Approach to HCI Jan Borchers Spring Swing Refresher David Merrill 5/14/2002
1CS 338: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 15: Menus, Toolbars, and Text.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Layout Managers Arranges and lays out the GUI components on a container.
Layout Manager Summary
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
University of Limerick1 Software Architecture Java Layout Managers.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
表单自定义 “ 表单自定义 ” 功能是用于制作表单的 工具,用数飞 OA 提供的表单自定义 功能能够快速制作出内容丰富、格 式规范、美观的表单。
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 9: Swing Components.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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 in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Modern Programming Language Java
University of Central Florida COP 3330 Object Oriented Programming
Chapter 13: Advanced GUIs and Graphics
Containers and Components
Creating Graphical User Interfaces
Graphical User Interface
Chapter 7-3 (Book Chapter 14)
GUI Layouts By: Leonard & Saif.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Chapter 10 GUI1 例 : LunarPhases, 月亮盈亏 (JB demo) 如何使用 Compound Bounder 如何使用 Combo Boxes 如何使用 Loading Multiple Images

Chapter 10 GUI2 使用多屏 (Panel) 来分组图形组件 // Create the phase selection and display panels. selectPanel = new JPanel(); displayPanel = new JPanel(); // Add various widgets to the subpanels. addWidgets();

Chapter 10 GUI3 // Create the main panel to contain the two subpanels. mainPanel = new JPanel(); mainPanel.setLayout(new GridLayout(2,1,5,5)); // (rows, cols, hgap, vgap) mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); // Add the select and display panels to the main panel. mainPanel.add(selectPanel); mainPanel.add(displayPanel); Compound Boarder

Chapter 10 GUI4 // Add border around the select panel selectPanel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Select Phase"), BorderFactory.createEmptyBorder(5,5,5,5))); Combo Boxes

Chapter 10 GUI5 JComboBox phaseChoices = null;... // Create combo box with lunar phase choices String[ ] phases = { "New", "Waxing Crescent", "First Quarter", "Waxing Gibbous", "Full", "Waning Gibbous", "Third Quarter", "Waning Crescent" }; phaseChoices = new JComboBox(phases); phaseChoices.setSelectedIndex(START_INDEX); Handling Events on a Combo Box phaseChoices.addActionListener(this);...

Chapter 10 GUI6 public void actionPerformed(ActionEvent event) { if ("comboBoxChanged".equals(event.getActionCommand())) { // update the icon to display the new phase phaseIconLabel.setIcon( images[phaseChoices.getSelectedIndex()]); } Multiple Images final static int NUM_IMAGES = 8; final static int START_INDEX = 3; ImageIcon[ ] images = new ImageIcon[NUM_IMAGES];...

Chapter 10 GUI7 // Create the widgets to select and display the phases of the moon. private void addWidgets() { // Get the images and put them into an array of ImageIcon. for (int i = 0; i < NUM_IMAGES; i++) { String imageName = "images/image" + i + ".jpg"; URL iconURL = ClassLoader.getSystemResource(imageName); ImageIcon icon = new ImageIcon(iconURL); images[i] = icon; } 注 : getSystemResource 会搜索 classpath 中的路径去获取指定图象 例 : VoteDialog 怎样使用 Radio Buttons 怎样使用 Dialogs

Chapter 10 GUI8 怎样使用 Radio Buttons 怎样使用 Dialogs

Chapter 10 GUI9 Radio Buttons final int numButtons = 4; JRadioButton[] radioButtons = new JRadioButton[numButtons]; final ButtonGroup group = new ButtonGroup();... final String defaultMessageCommand = "default"; final String yesNoCommand = "yesno"; final String yeahNahCommand = "yeahnah";

Chapter 10 GUI10 final String yncCommand = "ync“; radioButtons[0] = new JRadioButton(" Candidate 1: Sparky the Dog "); radioButtons[0].setActionCommand(defaultMessageCommand); radioButtons[1] = new JRadioButton(" Candidate 2: Shady Sadie "); radioButtons[1].setActionCommand(yesNoCommand);

Chapter 10 GUI11 radioButtons[2] = new JRadioButton(" Candidate 3: R.I.P. McDaniels "); radioButtons[2].setActionCommand(yeahNahCommand); radioButtons[3] = new JRadioButton(" Candidate 4: Duke the Java TM Platform Mascot "); radioButtons[3].setActionCommand(yncCommand); for (int i = 0; i < numButtons; i++) { group.add(radioButtons[i]); } // Select the first button by default. radioButtons[0].setSelected(true);

Chapter 10 GUI12 Dialogs 1.showMessageDialog –Component parentComponent –Object message

Chapter 10 GUI13 2.showOptionDialog –Component parentComponent –Object message –String title: title of the dialog –int optionType –int messageType –Icon icon: custom icon –Object[] options –Object initialValue

Chapter 10 GUI14 Getting User Input from a Dialog // yes/no dialog } else if (command == yesNoCommand) { int n = JOptionPane.showConfirmDialog( frame, "This candidate is a convicted felon.\n Do you still want to vote for her?", "A Follow-up Question", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) { setLabel("OK. Keep an eye on your wallet."); } else if (n == JOptionPane.NO_OPTION) { setLabel("Whew! Good choice."); } else { setLabel("It is your civic duty to cast your vote."); }

Chapter 10 GUI15 图形组件的布局管理 – 五种不同布局管理选项的效果

Chapter 10 GUI16 1.BorderLayout –A BorderLayout has five areas available to hold components: north, south, east, west, and center. All extra space is placed in the center area 例 : Panel p = new Panel(); p.setLayout(new BorderLayout()); p.add(new Button("Okay"), BorderLayout.SOUTH); 2.BoxLayout –The BoxLayout class puts components in a single row or column. This class respects the components' requested maximum sizes and also lets you align components. 例 : // Lay out the label and scroll pane from top to bottom. JPanel listPane = new JPanel(); listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS));

Chapter 10 GUI17 3. FlowLayout –FlowLayout is the default layout manager for every JPanel. This layout manager simply lays out components from left to right, starting new rows, if necessary 4.GridLayout –GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns 例 : public class ButtonGrid extends Applet { public void init() { setLayout(new GridLayout(3,2)); add(new Button("1")); add(new Button("2")); add(new Button("3")); add(new Button("4")); add(new Button("5")); add(new Button("6")); }

Chapter 10 GUI18 5.GridBagLayout –GridBagLayout is the most sophisticated, flexible layout manager the Java platform provides. This layout manager aligns components by placing them within a grid of cells, allowing some components to span more than one cell. The rows in the grid aren't necessarily all the same height; similarly, grid columns can have different widths 关于使用菜单的有关问题 菜单以节省空间的方式给用户提供多种选择 ( MenuLookDemo.java )

Chapter 10 GUI19 菜单图形组件类的层次结构

Chapter 10 GUI20 创建一个菜单 // in the constructor for a JFrame subclass: JMenuBar menuBar; JMenu menu, submenu; JMenuItem menuItem; JCheckBoxMenuItem cbMenuItem; JRadioButtonMenuItem rbMenuItem;... //Create the menu bar. menuBar = new JMenuBar(); setJMenuBar(menuBar); //Build the first menu. menu = new JMenu("A Menu"); menu.setMnemonic(KeyEvent.VK_A); menuBar.add(menu);

Chapter 10 GUI21 // a group of JMenuItems menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T); menuItem.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_1,ActionEvent.ALT_MASK)); menu.add(menuItem); menuItem = new JMenuItem( "Both text and icon", new ImageIcon("images/middle.gif")); menuItem.setMnemonic(KeyEvent.VK_B); menu.add(menuItem); menuItem = new JMenuItem(new ImageIcon("images/middle.gif")); menuItem.setMnemonic(KeyEvent.VK_D); menu.add(menuItem);

Chapter 10 GUI22 //a group of radio button menu items menu.addSeparator(); ButtonGroup group = new ButtonGroup(); rbMenuItem = new JRadioButtonMenuItem( "A radio button menu item"); rbMenuItem.setSelected(true); rbMenuItem.setMnemonic(KeyEvent.VK_R); group.add(rbMenuItem); menu.add(rbMenuItem); rbMenuItem = new JRadioButtonMenuItem("Another one"); rbMenuItem.setMnemonic(KeyEvent.VK_O); group.add(rbMenuItem); menu.add(rbMenuItem);

Chapter 10 GUI23 //a group of check box menu items menu.addSeparator(); cbMenuItem = new JCheckBoxMenuItem("A check box menu item"); cbMenuItem.setMnemonic(KeyEvent.VK_C); menu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Another one"); cbMenuItem.setMnemonic(KeyEvent.VK_H); menu.add(cbMenuItem); //a submenu menu.addSeparator(); submenu = new JMenu("A submenu"); submenu.setMnemonic(KeyEvent.VK_S); menuItem = new JMenuItem("An item in the submenu"); menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_2, ActionEvent.ALT_MASK)); submenu.add(menuItem);

Chapter 10 GUI24 menuItem = new JMenuItem("Another item"); submenu.add(menuItem); menu.add(submenu); //Build second menu in the menu bar. menu = new JMenu("Another Menu"); menu.setMnemonic(KeyEvent.VK_N); menuBar.add(menu); 处理来自菜单的事件 (MenuDemo.java) – 向 JMenuItem 对象注册以获取它的 Action 事件 – 向 JRadioButtonMenuItem 对象注册以获取它的 Action 或 Item 事 件 – 向 CheckBoxMenuItem 对象注册以获取它的 Item 事件

Chapter 10 GUI25 // JB demo public class MenuDemo extends JFrame implements ActionListener, ItemListener { public MenuDemo() { //...for each JMenuItem instance: menuItem.addActionListener(this);... //for each JRadioButtonMenuItem: rbMenuItem.addActionListener(this);... //for each JCheckBoxMenuItem: cbMenuItem.addItemListener(this);... }

Chapter 10 GUI26 public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem)(e.getSource()); String s = "Action event detected." + newline + " Event source: " + source.getText() + " (an instance of " + getClassName(source) + ")"; output.append(s + newline); } public void itemStateChanged(ItemEvent e) { //...Get information from the item event... //...Display it in the text area... }

Chapter 10 GUI27 文本图形部件的使用 1.Text Controls – 单行文本输入和编辑 2.Plain Text Controls – 任意行文本输入和编辑, 单字体 3.Styled Text Controls – 任意行文本输入和编辑, 多字体, 图形, 声音

Chapter 10 GUI28

Chapter 10 GUI29 1.An Example of Using a Text Field JTextField textField = new JTextField(10); textField.setActionCommand(textFieldString); textField.addActionListener(this); … public void actionPerformed(ActionEvent e) { String prefix = "You typed \""; if (e.getActionCommand().equals(textFieldString)) { JTextField source = (JTextField)e.getSource(); actionLabel.setText(prefix + source.getText() + "\""); } else { JPasswordField source = (JPasswordField)e.getSource(); actionLabel.setText(prefix + new String(source.getPassword()) + "\""); }

Chapter 10 GUI30 2.An Example of Using a Password Field JPasswordField passwordField = new JPasswordField(10); passwordField.setActionCommand(passwordFieldString); passwordField.addActionListener(this); … 3.An Example of Using a Text Area JTextArea textArea = new JTextArea( "This is an editable JTextArea " + "that has been initialized with the setText method. " + "A text area is a \"plain\" text component, " + "which means that although it can display text " + "in any font, all of the text is in the same font." ); textArea.setFont(new Font("Serif", Font.ITALIC, 16)); textArea.setLineWrap(true); textArea.setWrapStyleWord(true);

Chapter 10 GUI31 给 JTextArea 增加上下滚动功能 (JScrollPane) JScrollPane areaScrollPane = new JScrollPane(textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(250, 250)); areaScrollPane.setBorder(...create border...); 完整的程序清单 : TextSamplerDemo.java (JB demo)