Swing GUI Swing Components and the Containment Hierarchy

  • Slides: 33
Download presentation
Swing GUI

Swing GUI

Swing Components and the Containment Hierarchy Swing provides many standard GUI components such as

Swing Components and the Containment Hierarchy Swing provides many standard GUI components such as buttons, lists, menus, and text areas, which you combine to create your program's GUI. It also includes containers such as windows and tool bars. Swing. Application creates four commonly used Swing components: l a frame, or main window (JFrame) l a panel, sometimes called a pane (JPanel) l a button (JButton) l a label (JLabel)

A diagram of the containment hierarchy for the window shown by Swing. Application .

A diagram of the containment hierarchy for the window shown by Swing. Application .

How to Make Frames (Main Windows)

How to Make Frames (Main Windows)

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

import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Frame. Demo { public static void main(String s[]) { JFrame frame = new JFrame("Frame. Demo"); frame. add. Window. Listener(new Window. Adapter() { public void window. Closing(Window. Event e) { System. exit(0); } }); JLabel empty. Label = new JLabel(""); empty. Label. set. Preferred. Size(new Dimension(175, 100)); frame. get. Content. Pane(). add(empty. Label, Border. Layout. CENTER); frame. pack(); frame. set. Visible(true); } }

JPanel a. Panel = new JPanel(); a. Panel. set. Layout(new Border. Layout()); Or JPanel

JPanel a. Panel = new JPanel(); a. Panel. set. Layout(new Border. Layout()); Or JPanel a. Panel = new JPanel(new Border. Layout());

How to Use Buttons To create a button, you can instantiate one of the

How to Use Buttons To create a button, you can instantiate one of the many classes that descend from the Abstract. Button class. Class Description JButton A common button JCheck. Box A check box button. JRadio. Button One of a group of radio buttons JMenu. Item An item in a menu. JCheck. Box. Menu. Item A menu item that has a checkbox. l JRadio. Button. Menu. Item A menu item that has a radio button

Button(JButton) l Normal Button My. Button= new JButton (“Click Me!”); pane. add(My. Button); l

Button(JButton) l Normal Button My. Button= new JButton (“Click Me!”); pane. add(My. Button); l Image Button Image. Icon left. Button. Icon = new Image. Icon(“flower. gif"); b 1 = new JButton(“Bunga", left. Button. Icon);

Label (JLabel) Image. Icon icon = new Image. Icon("images/middle. gif"); . . . label

Label (JLabel) Image. Icon icon = new Image. Icon("images/middle. gif"); . . . label 1 = new JLabel("Image and Text", icon, JLabel. CENTER); //Set the position of the text, relative to the icon: label 1. set. Vertical. Text. Position(JLabel. BOTTOM); label 1. set. Horizontal. Text. Position(JLabel. CENTER); label 2 = new JLabel("Text-Only Label"); label 3 = new JLabel(icon); //Add labels to the JPanel. add(label 1); add(label 2); add(label 3);

Check Boxes chin. Button = new JCheck. Box("Chin"); chin. Button. set. Selected(true); glasses. Button

Check Boxes chin. Button = new JCheck. Box("Chin"); chin. Button. set. Selected(true); glasses. Button = new JCheck. Box("Glasses"); glasses. Button. set. Selected(true); hair. Button = new JCheck. Box("Hair"); hair. Button. set. Selected(true); teeth. Button = new JCheck. Box("Teeth"); teeth. Button. set. Selected(true);

JPanel check. Panel = new JPanel(); check. Panel. set. Layout(new Grid. Layout(0, 1)); check.

JPanel check. Panel = new JPanel(); check. Panel. set. Layout(new Grid. Layout(0, 1)); check. Panel. add(chin. Button); check. Panel. add(glasses. Button); check. Panel. add(hair. Button); check. Panel. add(teeth. Button);

Example JCheck. Box import java. awt. *; import javax. swing. *; public class Kotak.

Example JCheck. Box import java. awt. *; import javax. swing. *; public class Kotak. Pilihan extends JApplet { private JCheck. Box java, c cobol, vbscript, asp; public void init() { Container pane = get. Content. Pane(); pane. set. Background(Color. white); pane. set. Layout(new Flow. Layout());

Java C Cobol VBscript java = new JCheck. Box (“Java”, true); pane. add(java); c

Java C Cobol VBscript java = new JCheck. Box (“Java”, true); pane. add(java); c = new JCheck. Box (“C”); pane. add(c); cobol= new JCheck. Box (“Cobol”); pane. add(cobol); vbscript = new JCheck. Box (“Vbscript”); pane. add(vbscript); asp = new JCheck. Box (“ASP”, true); pane. add(asp); } } ASP

Radio Button JRadio. Button bird. Button = new JRadio. Button(“Bird”); JRadio. Button cat. Button

Radio Button JRadio. Button bird. Button = new JRadio. Button(“Bird”); JRadio. Button cat. Button = new JRadio. Button(“Cat”); JRadio. Button dog. Button = new JRadio. Button(“Dog”); dog. Button. set. Selected(true); JRadio. Button rabbit. Button = new JRadio. Button(“Rabbit”); JRadio. Button pig. Button = new JRadio. Button(“Pig”);

// Group the radio buttons. Button. Group group = new Button. Group(); group. add(bird.

// Group the radio buttons. Button. Group group = new Button. Group(); group. add(bird. Button); group. add(cat. Button); group. add(dog. Button); group. add(rabbit. Button); group. add(pig. Button);

List (JList) l Define List JList Short. List String item[] = {“ one”, “two”,

List (JList) l Define List JList Short. List String item[] = {“ one”, “two”, ”three”, ”four”, ”five”, ”six”} Short. List = new JList(item); Short. List. set. Visible. Row. Count(5); Short. List. set. Selection. Mode(List. Selection. Model. SINGLE_SELECTION); JScroll. Pane scroll = new JScroll. Pane(Short. List);

Example: JList import java. awt. *; import javax. swing. *; public class Senarai extends

Example: JList import java. awt. *; import javax. swing. *; public class Senarai extends JApplet{ Container pane = get. Content. Pane(); JList bentuk; String item [] = { “Garis”, “Segiempat”, “Bulatan”, ”Segitiga”, “Segienam”, “Segilapan”};

public void init() { pane. set. Background(Color. white); pane. set. Layout (new Flow. Layout());

public void init() { pane. set. Background(Color. white); pane. set. Layout (new Flow. Layout()); bentuk = new JList (item); bentuk. set. Visible. Row. Count (4); bentuk. set. Selection. Model(List. Selection. Model. SINGLE_SELECTION); JScroll. Pane skrol = new JScroll. Pane(bentuk); pane. add(skrol); } }

Selecting Items in a List SINGLE_SELECTION Only one item can be selected at a

Selecting Items in a List SINGLE_SELECTION Only one item can be selected at a time. When the user selects an item, any previously selected item is deselected first. l SINGLE_INTERVAL_SELECTION Multiple, contiguous items can be selected. When the user begins a new selection range, any previously selected items are deselected first. l

n. MULTIPLE_INTERVAL_SELECTION The default. Any combination of items can be selected. The user must

n. MULTIPLE_INTERVAL_SELECTION The default. Any combination of items can be selected. The user must explicitly deselect items.

JCombo. Box String[] pet. Strings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; //

JCombo. Box String[] pet. Strings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; // Create the combo box, select item at index 4. JCombo. Box pet. List = new JCombo. Box(pet. Strings); pane. add(pet. List) ;

JSlider static final int FPS_INIT = 10; JSlider frames. Per. Second = new JSlider(JSlider.

JSlider static final int FPS_INIT = 10; JSlider frames. Per. Second = new JSlider(JSlider. HORIZONTAL, 0, 30, FPS_INIT); frames. Per. Second. set. Major. Tick. Spacing(10); frames. Per. Second. set. Minor. Tick. Spacing(1); frames. Per. Second. set. Paint. Ticks(true); frames. Per. Second. set. Paint. Labels(true); //add the slider to the content pane content. Pane. add(frames. Per. Second);

JText. Field text. Field = new JText. Field(20); text. Field. add. Action. Listener(this); .

JText. Field text. Field = new JText. Field(20); text. Field. add. Action. Listener(this); . . . content. Pane. add(text. Field);

JText. Area ruang. Teks; ruang. Teks =new JText. Area(5, 20); JScroll. Pane skrol =new

JText. Area ruang. Teks; ruang. Teks =new JText. Area(5, 20); JScroll. Pane skrol =new JScroll. Pane(ruang. Teks, JScroll. Pane. VERTICAL_SCROLLBAR_ALWAYS, JScroll. Pane. HORIZONTAL_SCROLLBAR_ALWAYS); pane. add(skrol);

JPassword. Field password. Field = new JPassword. Field(10); password. Field. set. Echo. Char('#'); password.

JPassword. Field password. Field = new JPassword. Field(10); password. Field. set. Echo. Char('#'); password. Field. add. Action. Listener(new Action. Listener() {. . . }); …… private static boolean is. Password. Correct(char[] input) { char[] correct. Password = { 'b', 'u', 'g', 'a', 'b', 'o' }; if (input. length != correct. Password. length) return false; for (int i = 0; i < input. length; i ++) if (input[i] != correct. Password[i]) return false; return true; }

Border. Layout Container content. Pane = get. Content. Pane(); //Use the content pane's default

Border. Layout Container content. Pane = get. Content. Pane(); //Use the content pane's default Border. Layout. //content. Pane. set. Layout(new Border. Layout()); content. Pane. add(new JButton("Button 1 (NORTH)"), Border. Layout. NORTH); content. Pane. add(new JButton("2 (CENTER)"), Border. Layout. CENTER); content. Pane. add(new. JButton("Button 3 (WEST)"), Border. Layout. WEST); content. Pane. add(new JButton("Long-Named Button 4 (SOUTH)"), Border. Layout. SOUTH); content. Pane. add(new JButton("Button 5 (EAST)"), Border. Layout. EAST);

Box. Layout Container content. Pane = get. Content. Pane(); content. Pane. set. Layout(new Box.

Box. Layout Container content. Pane = get. Content. Pane(); content. Pane. set. Layout(new Box. Layout(content. Pane, Box. Layout. Y_AXIS)); add. AButton("Button 1", content. Pane); add. AButton("2", content. Pane); add. AButton("Button 3", content. Pane); add. AButton("Long-Named Button 4", content. Pane); add. AButton("Button 5", content. Pane);

Card. Layout

Card. Layout

Card. Layout Program JPanel cards; final static String BUTTONPANEL = "JPanel with JButtons"; final

Card. Layout Program JPanel cards; final static String BUTTONPANEL = "JPanel with JButtons"; final static String TEXTPANEL = "JPanel with JText. Field"; public Card. Window() { Container content. Pane = get. Content. Pane(); //Put the JCombo. Box in a JPanel to get a nicer look. String combo. Box. Items[] = { BUTTONPANEL, TEXTPANEL }; JPanel cbp = new JPanel(); JCombo. Box c = new JCombo. Box(combo. Box. Items); c. set. Editable(false); c. add. Item. Listener(this); cbp. add(c);

//Use the default layout manager, Border. Layout content. Pane. add(cbp, Border. Layout. NORTH); cards

//Use the default layout manager, Border. Layout content. Pane. add(cbp, Border. Layout. NORTH); cards = new JPanel(); cards. set. Layout(new Card. Layout()); Jpanel p 1 = new JPanel(); p 1. add(new JButton("Button 1")); p 1. add(new JButton("Button 2")); p 1. add(new JButton("Button 3")); JPanel p 2 = new JPanel(); p 2. add(new JText. Field("Text. Field", 20)); cards. add(p 1, BUTTONPANEL); cards. add(p 2, TEXTPANEL); content. Pane. add(cards, Border. Layout. CENTER);

Flow. Layout Container content. Pane = get. Content. Pane(); content. Pane. set. Layout(new Flow.

Flow. Layout Container content. Pane = get. Content. Pane(); content. Pane. set. Layout(new Flow. Layout()); content. Pane. add(new JButton("Button 1")); content. Pane. add(new JButton("2")); content. Pane. add(new JButton("Button 3")); content. Pane. add(new JButton("Long. Named Button 4")); content. Pane. add(new JButton("Button 5"));

Grid. Layout Container content. Pane = get. Content. Pane(); content. Pane. set. Layout(new Grid.

Grid. Layout Container content. Pane = get. Content. Pane(); content. Pane. set. Layout(new Grid. Layout(0, 2)); content. Pane. add(new JButton("Button 1")); content. Pane. add(new JButton("2")); content. Pane. add(new JButton("Button 3")); content. Pane. add(new JButton("Long-Named Button 4")); content. Pane. add(new JButton("Button 5"));