More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces Java’s AWT and Swing APIs.
Advertisements

Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
Graphical User Interfaces
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
Graphical User Interfaces Java’s AWT and Swing APIs.
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.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI programming Graphical user interface-based programming.
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 Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
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.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
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.
Class Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
A simple swing example GETTING STARTED WITH WIND CHILL.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUIs & Event-Driven Programming Chapter 11 Review.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
회원가입 - GUI ** 오지*.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Web Design & Development Lecture 11
Swing JComponents.
A First Look at GUI Applications
A Quick Java Swing Tutorial
A Quick Java Swing Tutorial
Advanced Programming in Java
어서와 Java는 처음이지! 제13장 실전프로젝트.
Component-Based Software Engineering
Presentation transcript:

More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004

Celsius Converter import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CelsiusConverter implements ActionListener { JFrame converterFrame; JPanel converterPanel; JTextField textfieldCelsius; JLabel celsiusLabel, fahrenheitLabel; JButton convertButton; public static void main(String[] args) { CelsiusConverter converter = new CelsiusConverter(); } … }

Celsius Converter public CelsiusConverter() { converterFrame = new JFrame("Convert Celsius to Fahrenheit"); converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the panel. converterPanel = new JPanel(new GridLayout(2, 2)); //Add the components. addComponents(); //Set the default button. converterFrame.getRootPane().setDefaultButton(convertButton); //Add the panel to the window. converterFrame.getContentPane().add(converterPanel, BorderLayout.CENTER); //Display the window. converterFrame.pack(); converterFrame.setVisible(true); }

Celsius Converter private void addComponents() { //Create components. textfieldCelsius = new JTextField(2); celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT); convertButton = new JButton("Convert"); fahrenheitLabel = new JLabel("Fahrenheit ", SwingConstants.LEFT); //Listen to events from the Convert button. convertButton.addActionListener(this); //Add the components to the container. converterPanel.add(textfieldCelsius); converterPanel.add(celsiusLabel); converterPanel.add(convertButton); converterPanel.add(fahrenheitLabel); celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); }

Painting import java.awt.*; import javax.swing.*; public class paintingExample1 extends JFrame { public static void main(String args[]) { paintingExample frame = new paintingExample(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,400); frame.setVisible(true); } public void paint(Graphics g) { int framewidth = this.getWidth(); int frameheight = this.getHeight(); Graphics2D g2d = (Graphics2D) g; g2d.setBackground(Color.WHITE); g2d.setColor(Color.BLUE); g2d.clearRect(0,0, framewidth, frameheight); g2d.drawLine(10, 40, 150, 300); g2d.drawRect(200, 200, 50, 100); }

Adding Interactivity We ’ ll make the following changes: public class paintingExample extends JFrame implements MouseMotionListener { int x = 150; int y = 300; –In main() add: frame.addMouseMotionListener(frame); g2d.drawLine(90, 140, x, y);

Event Handlers public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); this.repaint(); } public void mouseMoved(MouseEvent e) { }

Multiple Buttons // create the buttons JButton buttonInc = new JButton("Increase"); buttonInc.setActionCommand("increase"); buttonInc.addActionListener(mainObj); JButton buttonDec = new JButton("Decrease"); buttonDec.addActionListener(mainObj); buttonDec.setActionCommand("decrease");

Multiple Buttons public void actionPerformed(ActionEvent e) { if(e.getActionCommand() == "increase") numClicks++; else if (e.getActionCommand() == "decrease") numClicks--; label.setText(labelPrefix + numClicks); }

Check Boxes

Initialization Code: chinButton = new JCheckBox("Chin"); chinButton.setSelected(true); chinButton.addItemListener(this); If you want to do something in the event the user clicks a check box –The class should implement ItemListener –Create an event handler method as follows: public void itemStateChanged(ItemEvent e) { } To find out if the box is checked call: –chinButton.isSelected();

Radio Buttons

Initialization Code: JRadioButton birdButton = new JRadioButton(birdString); birdButton.setActionCommand(birdString); birdButton.setSelected(true); JRadioButton catButton = new JRadioButton(catString); catButton.setActionCommand(catString); ButtonGroup group = new ButtonGroup(); group.add(birdButton); group.add(catButton); birdButton.addActionListener(this); catButton.addActionListener(this);

Radio Buttons If you want to do something in the event the user clicks a radio button –Same as a regular JButton (implement ActionListener and the method actionPerformed() ) To find out if the radio button is selected –birdButton.isSelected() ;

Combo Boxes

Creating String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; //Create the combo box, select item at index 4. //Indices start at 0, so 4 specifies the pig. JComboBox petList = new JComboBox(petStrings); petList.setSelectedIndex(4); petList.addActionListener(this);

Combo Boxes Handling Events public class ComboBoxDemo... implements ActionListener {... petList.addActionListener(this) {... public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String petName = (String)cb.getSelectedItem(); // code to handle the event }... }

Further Reading