swing gui. swing components and the containment hierarchy swing provides many standard gui...
TRANSCRIPT
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)
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); }
}
JPanel
JPanel aPanel = new JPanel();aPanel.setLayout(new
BorderLayout());
Or
JPanel aPanel = new JPanel(new BorderLayout());
How to Use Buttons
To create a button, you can instantiate one of the many classes that descend from the AbstractButton class.
Class DescriptionJButton A common buttonJCheckBox A check box button. JRadioButton One of a group of radio buttonsJMenuItem An item in a menu. JCheckBoxMenuItem A menu item that has a checkbox.
JRadioButtonMenuItem A menu item that has a radio button
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);
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);
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);
JPanel checkPanel = new JPanel(); checkPanel.setLayout(new GridLayout(0, 1)); checkPanel.add(chinButton); checkPanel.add(glassesButton); checkPanel.add(hairButton);
checkPanel.add(teethButton);
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());
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);}
}
Java C Cobol VBscript ASP
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”);
// Group the radio buttons. ButtonGroup group = new
ButtonGroup(); group.add(birdButton); group.add(catButton); group.add(dogButton); group.add(rabbitButton); group.add(pigButton);
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);
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”};
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);
}}
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.
MULTIPLE_INTERVAL_SELECTION
The default. Any combination of items can be selected. The user must explicitly deselect items.
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) ;
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);
JTextField
textField = new JTextField(20); textField.addActionListener(this);
... contentPane.add(textField);
JTextArea
JTextArea ruangTeks;
ruangTeks =new JTextArea(5,20);
JScrollPane skrol =new JScrollPane(ruangTeks,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pane.add(skrol);
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; }
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);
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);
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);
//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);
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"));
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"));