JLayeredPane. Depth Constants in JLayeredPane

Similar documents
Introduction to the JAVA UI classes Advanced HCI IAT351

CS 251 Intermediate Programming GUIs: Components and Layout

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Packages: Putting Classes Together

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Part 3: Graphical User Interface (GUI) & Java Applets

Window Interfaces Using Swing Objects

import javax.swing.*; import java.awt.*; import java.awt.event.*;

Graphical User Interfaces. Swing. Jose Jesus García Rueda

H212 Introduction to Software Systems Honors

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Chapter 13 Lab Advanced GUI Applications

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphical User Interfaces 2

Window Interfaces Using Swing Objects

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Building Graphical User Interfaces. GUI Principles

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

Attempt FOUR questions Marking Scheme Time: 120 mins

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Graphical User Interfaces 2

encompass a group of features for building Graphical User Interfaces (GUI).

Graphical User Interfaces 2

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

Building Graphical User Interfaces. Overview

Using Several Components

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Chapter 12 GUI Basics

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Programming graphics

Advanced Java Programming. Swing. Introduction to Swing. Swing libraries. Eran Werner, Tel-Aviv University Summer, 2005

CSIS 10A Assignment 7 SOLUTIONS

CHAPTER 2. Java Overview

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Event Driven Programming

Systems Programming Graphical User Interfaces

CS 251 Intermediate Programming GUIs: Event Listeners

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Java. GUI building with the AWT

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

AP CS Unit 11: Graphics and Events

FirstSwingFrame.java Page 1 of 1

Graphical User Interface (GUI)

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Jonathan Aldrich Charlie Garrod

Layouts and Components Exam

Java: Graphical User Interfaces (GUI)

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 1325 Project Description

Swing Programming Example Number 2

DM503 Programming B. Peter Schneider-Kamp.

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

User interfaces and Swing

More Swing. CS180 Recitation 12/(04,05)/08

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

We are on the GUI fast track path

JAVA NOTES GRAPHICAL USER INTERFACES

Java Graphical User Interfaces

Summary Chapter 25 GUI Components: Part 2

Widgets. Widgets Widget Toolkits. User Interface Widget

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

PIC 20A GUI with swing

Midterm assessment - MAKEUP Fall 2010

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Chapter 14. More Swing

Programmierpraktikum

CS 180 Final Exam Review 12/(11, 12)/08

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Chapter 9 Designing Graphical User Interfaces (GUIs)

Building Java Programs Bonus Slides

University of Cape Town Department of Computer Science Computer Science CSC1017F

Programming Languages and Techniques (CIS120)

Graphical User Interfaces. Comp 152

Chapter #1. Program to demonstrate applet life cycle

The Java Swing Toolkit

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

2010 가을학기부산대학교정보컴퓨터공학부 OVERVIEW OF GUI PROGRAMMING

Java Programming Lecture 6

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Today. cisc3120-fall2012-parsons-lectiii.3 2

CS108, Stanford Handout #19. Swing GUI

2.4 Input and Output. Input and Output. Standard Output. Standard Output Abstraction. Input devices. Output devices. Our approach.

A Simple Text Editor Application

Transcription:

JLayeredPane Continuing on Swing Components A layered pane is a Swing container that provides a third dimension for positioning components depth or Z order. The class for the layered pane is JLayeredPane. The higher the Z order, closer the component is to the top position within the container. The relationship between components at the same depth is determined by their positions within the depth. Depth Constants in JLayeredPane Depth Constants in JLayeredPane

depth = 2 change the depth of the ICE logo import java.awt.event.*; import java.awt.image.*; import java.applet.applet; import javax.imageio.*; import java.io.*; depth = 4 depth = 5 initial depth = 5 public class TestLayeredPaneApplet extends JApplet{ JLabel icelabel,label; Icon iceicon; JLayeredPane layeredpane; MyHandler handler = new MyHandler(); public void init(){ setmouseicongui(); setlayeredpanegui(); public void setmouseicongui(){ iceicon = new ImageIcon("ICE.png"); icelabel = new JLabel(iceIcon); setlayout(null); icelabel.setbounds(200,200, iceicon.geticonwidth(), iceicon.geticonheight()); layeredpane = getlayeredpane(); layeredpane.addmousemotionlistener(handler); layeredpane.add(icelabel,new Integer(5)); public void setlayeredpanegui(){ JPanel panel6 = new JPanel(); panel6.setbounds(120,120,200,200); panel6.setbackground(color.magenta); layeredpane.add(panel6,new Integer(6)); JPanel panel2 = new JPanel(); panel2.setbounds(10,10,200,200); panel2.setbackground(color.yellow); layeredpane.add(panel2,new Integer(2)); JPanel panel4 = new JPanel(); panel4.setbounds(60,60,200,200); panel4.setbackground(color.cyan); layeredpane.add(panel4,new Integer(4));

label = new JLabel("Current Depth for ICE = +layeredpane.getlayer(icelabel)); label.setbounds(2,2,150,20); layeredpane.add(label,new Integer(100)); JButton incbutton = new JButton("+"); incbutton.setbounds(155,2,50,50); incbutton.addactionlistener(handler); layeredpane.add(incbutton,new Integer(100)); JButton decbutton = new JButton("-"); decbutton.setbounds(210,2,50,50); decbutton.addactionlistener(handler); layeredpane.add(decbutton,new Integer(100)); class MyHandler implements MouseMotionListener, ActionListener{ public void mousemoved(mouseevent e){ icelabel.setlocation(e.getx()-iceicon.geticonwidth()/2, e.gety()-iceicon.geticonheight()/2); public void mousedragged(mouseevent e){ icelabel.setlocation(e.getx()-iceicon.geticonwidth()/2, e.gety()-iceicon.geticonheight()/2); Menus public void actionperformed(actionevent e){ JButton src = (JButton)e.getSource(); if(src.gettext().equals("+")){ layeredpane.setlayer(icelabel, layeredpane.getlayer(icelabel)+1,0); else{ layeredpane.setlayer(icelabel, layeredpane.getlayer(icelabel)-1,0); label.settext("current Depth for ICE = "+ layeredpane.getlayer(icelabel)); Menus are not placed with the other components in the UI but usually along the top of a window A menu bar contains one or more menus. Swing classes related to menus include (but not limited to) JMenuBar, JMenu, JMenuItem

JMenu Example: Using Menus JMenuBar JButton import java.awt.event.*; public class TestMenuBarApplet extends JApplet{ JMenuBar menubar; JMenu editmenu,aboutmenu; JMenuItem action1, action2, action3, action4; JTextArea text = new JTextArea(); JButton button1, button2; MyButtonHandler handler; JTextArea JMenuItem public void init(){ menubar = new JMenuBar(); editmenu = new JMenu("Edit"); menubar.add(editmenu); action1 = new JMenuItem("Action 1"); editmenu.add(action1); action2 = new JMenuItem("Action 2"); editmenu.add(action2); editmenu.addseparator(); action3 = new JMenuItem("Clear"); editmenu.add(action3); aboutmenu = new JMenu("About"); menubar.add(aboutmenu); action4 = new JMenuItem("Credit"); aboutmenu.add(action4); setjmenubar(menubar); button1 = new JButton("+"); button2 = new JButton("-"); menubar.add(button1); menubar.add(button2); handler = new MyButtonHandler(); setlayout(new BorderLayout()); add(text,borderlayout.center); class MyButtonHandler implements ActionListener{ public MyButtonHandler(){ action1.addactionlistener(this); : button2.addactionlistener(this); public void actionperformed(actionevent e){ AbstractButton src = (AbstractButton)e.getSource(); if(src.gettext().equals("clear")){ text.settext(""); else if(src.gettext().equals("+")){ text.setfont(text.getfont().derivefont( text.getfont().getsize2d()+5f)); else if(src.gettext().equals("-")){ text.setfont(text.getfont().derivefont( text.getfont().getsize2d()-5f)); else{ text.append(src.gettext()+" is selected.\n");

A GUI application starts with a frame. A frame is a top-level window with a border, a title bar, and control buttons. In the Swing framework, frames are provided by the JFrame class. Swing Applications frame control menu frame title bar frame control buttons Using JFrame in Swing Applications Observe the following code. The program creates and show an instance of JFrame with a white JPanel. public class SimpleFrame{ public static void main(string[] args){ JFrame mainframe = new JFrame("A simple JFrame example"); mainframe.setdefaultcloseoperation(jframe.exit_on_close); JPanel panel = new JPanel(); panel.setpreferredsize(new Dimension(400,400)); mainframe.add(panel); mainframe.pack(); mainframe.setvisible(true); Using JFrame in Swing Applications Using JFrame in Swing Applications mainframe.setdefaultcloseoperation(jframe.exit_on_close); panel.setpreferredsize(new Dimension(400,400)); This specifies what happens when mainframe is closed. The EXIT_ON_CLOSE operation exits the program when the user closes the frame. This is good for this program since the program has only one frame. setpreferredsize() set the preferred dimension of a JComponent mainframe.pack(); pack() sizes the frame so that all its contents are at or above their preferred sizes. Using pack() is preferable to setsize() or setbounds(), since it still leaves the layout manager in charge.

Dialogs A Dialog window is an independent subwindow meant to carry temporary notice apart from the main Swing Application Window. Using JOptionPane, different kinds of dialogs can be simply created and customized. import java.awt.event.*; public class DialogTest extends JFrame{ JButton button1, button2, button3; public DialogTest(String s){ super(s); JPanel panel = new JPanel(); panel.setlayout(new GridLayout(3,1,5,5)); panel.setpreferredsize(new Dimension(200,200)); button1 = new JButton("1) Default Title and Icon"); button2 = new JButton("2) Custom Title, Warning Icon"); button3 = new JButton("3) Show Option Dialog"); panel.add(button1); panel.add(button2); panel.add(button3); MyButtonHandler handler = new MyButtonHandler(); add(panel); public static void main(string[] args){ DialogTest frame = new DialogTest("Various Dialogs"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setvisible(true); class MyButtonHandler implements ActionListener{ public MyButtonHandler(){ button1.addactionlistener(this); button2.addactionlistener(this); button3.addactionlistener(this); : public void actionperformed(actionevent e){ System.out.println("Action"); JButton src = (JButton)e.getSource(); switch(src.gettext().charat(0)){ case '1': JOptionPane.showMessageDialog(DialogTest.this, "Please go read the textbook."); break; case '2': JOptionPane.showMessageDialog(DialogTest.this, "You MUST read the textbook.","error", JOptionPane.ERROR_MESSAGE); break;

case '3': Object[] options = {"Okey!","No, thanks","cancel"; int n = JOptionPane.showOptionDialog(DialogTest.this, "Would you go reading?","a Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,null, options,options[2]); System.out.println(n); break;