Presentation is loading. Please wait.

Presentation is loading. Please wait.

Swing GUI. Swing Components and the Containment Hierarchy Swing provides many standard GUI components such as buttons, lists, menus, and text areas, which.

Similar presentations


Presentation on theme: "Swing GUI. Swing Components and the Containment Hierarchy Swing provides many standard GUI components such as buttons, lists, menus, and text areas, which."— Presentation transcript:

1 Swing GUI

2 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. SwingApplication creates four commonly used Swing components: a frame, or main window ( JFrame ) a panel, sometimes called a pane ( JPanel ) a button ( JButton ) a label ( JLabel )

3 A diagram of the containment hierarchy for the window shown by SwingApplication.

4 How to Make Frames (Main Windows)

5 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FrameDemo { public static void main(String s[]) { JFrame frame = new JFrame("FrameDemo"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }

6 JPanel JPanel aPanel = new JPanel(); aPanel.setLayout(new BorderLayout()); Or JPanel aPanel = new JPanel(new BorderLayout());

7 How to Use Buttons To create a button, you can instantiate one of the many classes that descend from the AbstractButton class. AbstractButton ClassDescription JButtonA common button JCheckBoxA check box button. JRadioButton One of a group of radio buttons JMenuItemAn item in a menu. JCheckBoxMenuItem A menu item that has a checkbox. JRadioButtonMenuItem A menu item that has a radio button

8 Button(JButton) Normal Button MyButton= new JButton (“Click Me!”); pane.add(MyButton); Image Button ImageIcon leftButtonIcon = new ImageIcon(“flower.gif"); b1 = new JButton(“Bunga", leftButtonIcon);

9 Label (JLabel) ImageIcon icon = new ImageIcon("images/middle.gif");... label1 = new JLabel("Image and Text", icon, JLabel.CENTER); //Set the position of the text, relative to the icon: label1.setVerticalTextPosition(JLabel.BOTTOM); label1.setHorizontalTextPosition(JLabel.CENTER); label2 = new JLabel("Text-Only Label"); label3 = new JLabel(icon); //Add labels to the JPanel. add(label1); add(label2); add(label3);

10 Check Boxes chinButton = new JCheckBox("Chin"); chinButton.setSelected(true); glassesButton = new JCheckBox("Glasses"); glassesButton.setSelected(true); hairButton = new JCheckBox("Hair"); hairButton.setSelected(true); teethButton = new JCheckBox("Teeth"); teethButton.setSelected(true);

11 JPanel checkPanel = new JPanel(); checkPanel.setLayout(new GridLayout(0, 1)); checkPanel.add(chinButton); checkPanel.add(glassesButton); checkPanel.add(hairButton); checkPanel.add(teethButton);

12 Example JCheckBox import java.awt.*; import javax.swing.*; public class KotakPilihan extends JApplet { private JCheckBox java, c cobol, vbscript, asp; public void init() { Container pane = getContentPane(); pane.setBackground(Color.white); pane.setLayout(new FlowLayout());

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

14 Radio Button JRadioButton birdButton = new JRadioButton(“Bird”); JRadioButton catButton = new JRadioButton(“Cat”); JRadioButton dogButton = new JRadioButton(“Dog”); dogButton.setSelected(true); JRadioButton rabbitButton = new JRadioButton(“Rabbit”); JRadioButton pigButton = new JRadioButton(“Pig”);

15 // Group the radio buttons. ButtonGroup group = new ButtonGroup(); group.add(birdButton); group.add(catButton); group.add(dogButton); group.add(rabbitButton); group.add(pigButton);

16 List (JList) Define List JList ShortList String item[] = {“ one”, “two”,”three”,”four”,”five”,”six”} ShortList = new JList(item); ShortList.setVisibleRowCount(5); ShortList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane scroll = new JScrollPane(ShortList);

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

18 public void init() { pane.setBackground(Color.white); pane.setLayout (new FlowLayout()); bentuk = new JList (item); bentuk.setVisibleRowCount (4); bentuk.setSelectionModel(ListSelectionModel.SINGLE_SELECTION); JScrollPane skrol = new JScrollPane(bentuk); pane.add(skrol); }

19 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. SINGLE_INTERVAL_SELECTION Multiple, contiguous items can be selected. When the user begins a new selection range, any previously selected items are deselected first.

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

21 JComboBox String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; // Create the combo box, select item at index 4. JComboBox petList = new JComboBox(petStrings); pane.add(petList) ;

22 JSlider static final int FPS_INIT = 10; JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 0, 30, FPS_INIT); framesPerSecond.setMajorTickSpacing(10); framesPerSecond.setMinorTickSpacing(1); framesPerSecond.setPaintTicks(true); framesPerSecond.setPaintLabels(true); //add the slider to the content pane contentPane.add(framesPerSecond);

23 JTextField textField = new JTextField(20); textField.addActionListener(this);... contentPane.add(textField);

24 JTextArea JTextArea ruangTeks; ruangTeks =new JTextArea(5,20); JScrollPane skrol =new JScrollPane(ruangTeks, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); pane.add(skrol);

25 JPassword JPasswordField passwordField = new JPasswordField(10); passwordField.setEchoChar('#'); passwordField.addActionListener(new ActionListener() {... }); …… private static boolean isPasswordCorrect(char[] input) { char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' }; if (input.length != correctPassword.length) return false; for (int i = 0; i < input.length; i ++) if (input[i] != correctPassword[i]) return false; return true; }

26 BorderLayout Container contentPane = getContentPane(); //Use the content pane's default BorderLayout. //contentPane.setLayout(new BorderLayout()); contentPane.add(new JButton("Button 1 (NORTH)"), BorderLayout.NORTH); contentPane.add(new JButton("2 (CENTER)"), BorderLayout.CENTER); contentPane.add(newJButton("Button 3 (WEST)"), BorderLayout.WEST); contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), BorderLayout.SOUTH); contentPane.add(new JButton("Button 5 (EAST)"), BorderLayout.EAST);

27

28 BoxLayout Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); addAButton("Button 1", contentPane); addAButton("2", contentPane); addAButton("Button 3", contentPane); addAButton("Long-Named Button 4", contentPane); addAButton("Button 5", contentPane);

29 CardLayout

30 CardLayout Program JPanel cards; final static String BUTTONPANEL = "JPanel with JButtons"; final static String TEXTPANEL = "JPanel with JTextField"; public CardWindow() { Container contentPane = getContentPane(); //Put the JComboBox in a JPanel to get a nicer look. String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; JPanel cbp = new JPanel(); JComboBox c = new JComboBox(comboBoxItems); c.setEditable(false); c.addItemListener(this); cbp.add(c);

31 //Use the default layout manager, BorderLayout contentPane.add(cbp, BorderLayout.NORTH); cards = new JPanel(); cards.setLayout(new CardLayout()); Jpanel p1 = new JPanel(); p1.add(new JButton("Button 1")); p1.add(new JButton("Button 2")); p1.add(new JButton("Button 3")); JPanel p2 = new JPanel(); p2.add(new JTextField("TextField", 20)); cards.add(p1, BUTTONPANEL); cards.add(p2, TEXTPANEL); contentPane.add(cards, BorderLayout.CENTER);

32 FlowLayout Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5"));

33 GridLayout Container contentPane = getContentPane(); contentPane.setLayout(new GridLayout(0,2)); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5"));


Download ppt "Swing GUI. Swing Components and the Containment Hierarchy Swing provides many standard GUI components such as buttons, lists, menus, and text areas, which."

Similar presentations


Ads by Google