Download presentation
Presentation is loading. Please wait.
Published byTamsin Lyons Modified over 8 years ago
1
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing
2
First Program import javax.swing.JFrame; class Simple extends JFrame { public Simple() { setSize(300, 200); setTitle("Simple"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public class main { public static void main(String[] args) { Simple simple = new Simple(); simple.setVisible(true); } 2
3
First Program 3
4
Centering window on the screen import java.awt.*; import javax.swing.*; public class CenterOnScreen extends JFrame { public CenterOnScreen() { setSize(300, 200); setTitle("CenterOnScreen"); setDefaultCloseOperation(EXIT_ON_CLOSE); Toolkit toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2); } 4
5
Buttons import java.awt.*; import javax.swing.*; public class Buttons extends JFrame { private Toolkit toolkit; public Buttons() { setTitle("Buttons"); setSize(300, 200); toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation((size.width - getWidth())/2, (size.height - getHeight())/2); setDefaultCloseOperation(EXIT_ON_CLOSE); 5
6
Buttons JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(null); Jbutton beep = new JButton("Beep"); beep.setBounds(150, 60, 80, 30); beep.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); } }); JButton close = new JButton("Close"); close.setBounds(50, 60, 80, 30); 6
7
Buttons close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0);} }); panel.add(beep); panel.add(close); } public static void main(String[] args) { Buttons buttons = new Buttons(); buttons.setVisible(true); } 7
8
Buttons 8
9
Image Icons 9 import javax.swing.*; public class IconFrame extends JFrame { JButton load, save, subscribe, unsubscribe; public IconFrame() { super(“Icon Frame”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); // create icons ImageIcon loadIcon = new ImageIcon(“load.gif”); ImageIcon saveIcon = new ImageIcon(“save.gif”); ImageIcon subscribeIcon = new ImageIcon(“subscribe.gif”); ImageIcon unsubscribeIcon = new ImageIcon(“unsubscribe.gif”);
10
Image Icons 10 // create buttons load = new JButton(“Load”, loadIcon); save = new JButton(“Save”, saveIcon); subscribe = new JButton(“Subscribe”, subscribeIcon); unsubscribe = new JButton(“Unsubscribe”, unsubscribeIcon); // add buttons to panel panel.add(load); panel.add(save); panel.add(subscribe); panel.add(unsubscribe); // add the panel to a frame add(panel); pack(); setVisible(true); }
11
Image Icons 11 public static void main(String[] arguments) { IconFrame ike = new IconFrame(); } }
12
Labels 12 To create a label, you can use the following constructors: JLabel(String)—A label with the specified text JLabel(String, int)—A label with the specified text and alignment JLabel(String, Icon, int)—A label with the specified text, icon, and alignment Examples: JLabel feedsLabel = new JLabel(“Feeds”, SwingConstants.LEFT); JLabel urlLabel = new JLabel(“URL: “, SwingConstants.CENTER); JLabel dateLabel = new JLabel(“Date: “, SwingConstants.RIGHT); The contents of a label can be set with setText(String) or setIcon(Icon) methods retrieve contents with getText() and getIcon() methods.
13
Text Fields 13 Constructors for text fields include the following: JTextField()—An empty text field JTextField(int)—A text field with the specified width JTextField(String, int)—A text field with the specified text and width Examples: JTextField rssUrl = new JTextField(60); JTextField rssUrl2 = new JTextField( “Enter RSS feed URL here”, 60); setEditable(boolean), isEditable(), setText(String), getText(), getSelectedText().
14
Text Areas 14 JTextArea includes the following constructors: JTextArea(int, int)—A text area with the specified number of rows and columns JTextArea(String, int, int)—A text area with the specified text, rows, and columns You can use the getText(), getSelectedText(), and setText(String) methods with text areas as you would text fields. Also, an append(String) method adds the specified text at the end of the current text, and an insert(String, int) method inserts the specified text at the indicated position. Call setLineWrap(true) to cause line wrapping to occur. The setWrapStyleWord(boolean) method determines what wraps to the next line
15
Authenticator Application 15 import javax.swing.*; public class Authenticator extends javax.swing.JFrame { JTextField username = new JTextField(15); JPasswordField password = new JPasswordField(15); JTextArea comments = new JTextArea(4, 15); JButton ok = new JButton(“OK”); JButton cancel = new JButton(“Cancel”); public Authenticator() { super(“Account Information”); setSize(300, 220); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pane = new JPanel();
16
Authenticator Application 16 JLabel usernameLabel = new JLabel(“Username: “); JLabel passwordLabel = new JLabel(“Password: “); JLabel commentsLabel = new JLabel(“Comments: “); comments.setLineWrap(true); comments.setWrapStyleWord(true); pane.add(usernameLabel); pane.add(username); pane.add(passwordLabel); pane.add(password); pane.add(commentsLabel); pane.add(comments);
17
Authenticator Application 17 pane.add(ok); pane.add(cancel); add(pane); setVisible(true); } public static void main(String[] arguments) { Authenticator auth = new Authenticator(); } }
18
Scrolling Panes 18 Text areas in Swing do not include horizontal or vertical scrollbars, and there’s no way to add them using this component alone. Constructors: JScrollPane(Component)—A scrolling pane that contains the specified component JScrollPane(Component, int, int)—A scrolling pane with the specified component, vertical scrollbar configuration, and horizontal scrollbar configuration Scrollbars are configured using static class variables of the ScrollPaneConstants interface with three attributes: VERTICAL_SCROLLBAR_ALWAYS VERTICAL_SCROLLBAR_AS_NEEDED VERTICAL_SCROLLBAR_NEVER
19
Scrolling Panes 19 The following example creates a text area with a vertical scrollbar and no horizontal scrollbar and then adds it to a content pane: JPanel pane = new JPanel(); JTextArea comments = new JTextArea(4, 15); JScrollPane scroll = new JScrollPane(comments, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); pane.add(scroll); setContentPane(pane);
20
Check Boxes and Radio Buttons 20 Both the JCheckBox and JRadioButton classes have several useful methods inherited from a common superclass: setSelected(boolean)—Select the component if the argument is true and deselect it otherwise. isSelected()—Return a boolean indicating whether the component is currently selected. The following constructors are available for the JCheckBox class: JCheckBox(String)—A check box with the specified text label. JCheckBox(String, boolean)—A check box with the specified text label that is selected if the second argument is true. JCheckBox(Icon)—A check box with the specified graphical icon.
21
Check Boxes and Radio Buttons 21 JCheckBox(Icon, boolean)—A check box with the specified graphical icon that is selected if the second argument is true. JCheckBox(String, Icon)—A check box with the specified text label and graphical icon. JCheckBox(String, Icon, boolean)—A check box with the specified text label and graphical icon that is selected if the third argument is true. The JRadioButton class has constructors with the same arguments and functionality.
22
Check Boxes and Radio Buttons 22 import javax.swing.*; public class FormatFrame extends JFrame { JRadioButton[] teams = new JRadioButton[4]; public FormatFrame() { super(“Choose an Output Format”); setSize(320, 120); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); teams[0] = new JRadioButton(“Atom”); teams[1] = new JRadioButton(“RSS 0.92”); teams[2] = new JRadioButton(“RSS 1.0”); teams[3] = new JRadioButton(“RSS 2.0”, true);
23
Check Boxes and Radio Buttons 23 JPanel panel = new JPanel(); JLabel chooseLabel = new JLabel( “Choose an output format for syndicated news items.”); panel.add(chooseLabel); ButtonGroup group = new ButtonGroup(); for (int i = 0; i < teams.length; i++) { group.add(teams[i]); panel.add(teams[i]); } add(panel); setVisible(true); }
24
Check Boxes and Radio Buttons 24 public static void main(String[] arguments) { FormatFrame ff = new FormatFrame(); } }
25
Combo Boxes 25 The following steps show how a combo box can be created: 1. The JComboBox() constructor is used with no arguments. 2. The combo box’s addItem(Object) method adds items to the list. The JComboBox class has several methods that can be used to control a drop-down list or combo box: getItemAt(int)—Return the text of the list item at the index position specified by the integer argument. As with arrays, the first item of a choice list is at index position 0, the second at position 1, and so on. getItemCount()—Return the number of items in the list. getSelectedIndex()—Return the index position of the currently selected item in the list.
26
Combo Boxes 26 getSelectedItem()—Return the text of the currently selected item. setSelectedIndex(int)—Select the item at the indicated index position. setSelectedIndex(Object)—Select the specified object in the list. Example import javax.swing.*; public class FormatFrame2 extends JFrame { String[] formats = { “Atom”, “RSS 0.92”, “RSS 1.0”, “RSS 2.0” }; JComboBox formatBox = new JComboBox(); public FormatFrame2() { super(“Choose a Format”); setSize(220, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27
Combo Boxes 27 JPanel pane = new JPanel(); JLabel formatLabel = new JLabel(“Output formats:”); pane.add(formatLabel); for (int i = 0; i < formats.length; i++) formatBox.addItem(formats[i]); pane.add(formatBox); add(pane); setVisible(true); } public static void main(String[] arguments) { FormatFrame2 ff = new FormatFrame2();} }
28
Combo Boxes 28
29
Lists 29 The following constructors are available: JList()—Create an empty list. JList(Object[])—Create a list that contains an array of the specified class (such as String). JList(Vector)—Create a list that contains the specified java.util.Vector object. Example import javax.swing.*; public class Subscriptions extends JFrame { String[] subs = { “0xDECAFBAD”, “Cafe au Lait”, “Hack the Planet”, “Ideoplex”, “Inessential”, “Intertwingly”, “Markpasc”, “Postneo”, “RC3”, “Workbench” }; JList subList = new JList(subs);
30
Lists 30 public Subscriptions() { super(“Subscriptions”); setSize(150, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JLabel subLabel = new JLabel(“RSS Subscriptions:”); panel.add(subLabel); subList.setVisibleRowCount(8); JScrollPane scroller = new JScrollPane(subList); panel.add(scroller); add(panel); setVisible(true); }
31
Lists 31 public static void main(String[] arguments) { Subscriptions app = new Subscriptions(); } }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.