Swing Refresher slides

skatechildrenSoftware and s/w Development

Nov 3, 2013 (4 years and 6 days ago)

72 views

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

1

Swing Refresher

David Merrill

5/14/2002

http://cs377a.stanford.edu/

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

2

What is Swing?


The Java Foundation Classes (JFC) API extends the
original Abstract Window Toolkit (AWT) by adding a
comprehensive set of graphical user interface class
libraries.

JFC/Swing components include:


Pluggable Look and Feel


Accessibility API


Java 2DTM API (Java 2 only)


Drag and Drop (Java 2 only)


AWT


Internationalization

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

3

JFC/Swing GUI Components


Written in the Java programming language


Customizable look
-
and
-
feel


No reliance on the native windowing system


Incorporated in the Java 2 platform

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

4

JFC/Swing GUI Components

Applet

Dialog

Frame

Top
-
Level Containers

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

5

JFC/Swing GUI Components

Scroll Pane

Split Pane

Tabbed Pane

General
-
Purpose Containers

Toolbar

Panel

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

6

JFC/Swing GUI Components

Internal Frame

Layered Pane

Root Pane

Special
-
Purpose Containers

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

7

JFC/Swing GUI Components

Buttons

Combobox

List

Basic Controls

Menu

Slider

Text Fields

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

8

JFC/Swing GUI Components

Label

Progress Bar

Tooltip

Uneditable Information Displays

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

9

JFC/Swing GUI Components

Table

Text

Tree

Editable Displays of Formatted Information

Color Chooser

File Chooser

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

10

JFrame

public static void main(String s[]) {


JFrame frame = new JFrame("FrameDemo");



frame.addWindowListener(new WindowAdapter() {


public void windowClosing(WindowEvent e) {System.exit(0);}


});


//...create a blank label, set its preferred size...


frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);



frame.pack();


frame.setVisible(true);


}

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

11

JTabbedPane


ImageIcon icon = new ImageIcon("images/middle.gif");


JTabbedPane tabbedPane = new JTabbedPane();



Component panel1 = makeTextPanel("Blah"); //(returns a JPanel
)


tabbedPane.addTab("One", icon, panel1, "Does nothing");


tabbedPane.setSelectedIndex(0);



Component panel2 = makeTextPanel("Blah blah");


tabbedPane.addTab("Two", icon, panel2, "Does twice as much nothing");



Component panel3 = makeTextPanel("Blah blah blah");


tabbedPane.addTab("Three", icon, panel3, "Still does nothing");



Component panel4 = makeTextPanel("Blah blah blah blah");


tabbedPane.addTab("Four", icon, panel4, "Does nothing at all");

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

12

JTabbedPane


ImageIcon icon = new ImageIcon("images/middle.gif");


JTabbedPane tabbedPane = new JTabbedPane();



Component panel1 = makeTextPanel("Blah"); //(returns a JPanel
)


tabbedPane.addTab("One", icon, panel1, "Does nothing");


tabbedPane.setSelectedIndex(0);



Component panel2 = makeTextPanel("Blah blah");


tabbedPane.addTab("Two", icon, panel2, "Does twice as much nothing");



Component panel3 = makeTextPanel("Blah blah blah");


tabbedPane.addTab("Three", icon, panel3, "Still does nothing");



Component panel4 = makeTextPanel("Blah blah blah blah");


tabbedPane.addTab("Four", icon, panel4, "Does nothing at all");

CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

13

JGlassPane


private void redispatchMouseEvent(MouseEvent e, boolean repaint) {




// get the point from the MouseEvent


int eventID = e.getID();



if (containerPoint.y < 0) {


inMenuBar = true;


//...set container and containerPoint accordingly...


testForDrag(eventID);


}


component = SwingUtilities.getDeepestComponentAt(


container,


containerPoint.x,


containerPoint.y);



if (component.equals(liveButton)) {


inButton = true;


testForDrag(eventID);


}


if (inMenuBar || inButton || inDrag) {


...//Redispatch the event to component...


}

Etc…


CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

14

JMenuBar


//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);


menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");


menuBar.add(menu);



//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));


menuItem.getAccessibleContext().setAccessibleDescription(“This doesn't really do anything");


menu.add(menuItem);


CS377A: A Programming Approach to HCI • Jan Borchers • Spring 2002

15

URL



http://java.sun.com/docs/books/tutorial/uiswing/components/components.html