Vous êtes sur la page 1sur 19

Course : Business Application

Development
Effective Period : July 2017

Component and Layout


Manager

Session 7
Learning Objectives

At the end of this session, the students will be able to :


– Use GUI components on Java to create a user
interfaces .
Contents

Outline
• Introduction
• Label
• Button
• Radio Button
• Scroll Bar
• Slider
• Check Box
• Text Area
• Text Field
• Password Field
• List View
• Combo Box
Introduction

• A GUI create a user-friendly system that is easy to use


• Create a GUI required creativity and knowledge on how
GUI components work
• There are some components are introduced in this
section in order to know how to create a GUI
Contents

Outline
• Introduction
• Label
• Button
• Radio Button
• Scroll Bar
• Slider
• Check Box
• Text Area
• Text Field
• Password Field
• List View
• Combo Box
Label (1)
• Keyword ”Jlabel”
• A label is a display area for short writings, an image or
both
• Inheritance of JComponent
Label (2)
• Code
• import javax.swing.*;
import java.awt.*;

public class LabelDemo extends JFrame{


public LabelDemo(){
ImageIcon icon = new ImageIcon("butterfly.png");
JLabel label = new JLabel("Butterfly", icon, SwingConstants.CENTER);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setIconTextGap(5);
add(label);
}
public static void main(String[] args) {
JFrame frame = new LabelDemo();
frame.setTitle("Label Testing");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Text Field, Text Area and Scroll Pane
(1)
• Keyword “JTextField”; “JTextArea”;
“JScrollPane”
• It is used to display a String
• JTextArea and JTextField are inheritance of class
JTextComponent
• JTextArea is an instance of JTextField and it allows to write
text in some rows
• JTextArea does not handle scrolling on its text. In order to
use the scrolling, it is required to create object JScrollPane
to handle it
• Implement interface ActionListener
• Override actionPerformed(ActionEvent e)
Text Field, Text Area and Scroll Pane
•(2)Code
• import javax.swing.*;
import java.awt.*;

public class textDemo extends JFrame{


public textDemo(){
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
JLabel labelName = new JLabel("Full Name : ");
JLabel labelAddress = new JLabel("Address : ");
JLabel labelPassword = new JLabel("Password : ");
JTextField textName = new JTextField("Janz", 10);
JTextArea textAddress;
JScrollPane scrollPane = new JScrollPane(textAddress = new JTextArea("Jl. Kebun Jeruk 27", 5, 20));
JPasswordField textPassword = new JPasswordField(10);
textName.setEditable(true);
textName.setForeground(Color.RED);
textName.setHorizontalAlignment(SwingConstants.RIGHT);
textAddress.setLineWrap(true);
textAddress.setWrapStyleWord(true);
textAddress.setForeground(Color.BLUE);
textAddress.setFont(new Font("Courier", Font.BOLD, 14));
add(labelName);
add(textName);
add(labelAddress);
add(scrollPane);
add(labelPassword);
add(textPassword);
}
public static void main(String[] args) {
JFrame frame = new textDemo();
frame.setTitle("Text Testing");
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Button
• Keyword “Jbutton”
• A button catch an event when it is clicked (Action Event)
• Implement interface ActionListener
• Override actionPerformed(ActionEvent e)
Button(2)
• Code
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonDemo extends JFrame implements ActionListener{


private JPanel panel = new JPanel();
private JLabel labelName;
private JTextField textName;
private JButton buttonOk;

public ButtonDemo(){
labelName = new JLabel("Full Name : ");
textName = new JTextField("Janz", 10);
buttonOk = new JButton("Submit");
textName.setEditable(true);
textName.setForeground(Color.RED);
textName.setHorizontalAlignment(SwingConstants.RIGHT);
panel.setLayout(new FlowLayout());
panel.add(labelName);
panel.add(textName);
panel.add(buttonOk);
add(panel);
buttonOk.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new ButtonDemo();
frame.setTitle("Button Testing");
frame.setSize(300, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Full name is "+ textName.getText());
}
}
Check Box and Radio Button (1)
• Keyword “JCheckBox”; “JRadioButton”
• Check Box
– It allows user to choose more than 1 choices
• Radio Button
– User only allows to choose 1 choice
• Implement interface ItemListener and ActionListener
• Override:
– actionPerformed(ActionEvent e) for ActionListener
– itemStateChanged(ItemEvent e) for ItemListener
Check Box and Radio Button (2)
• Code
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RadioCheckDemo extends JFrame{


private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JRadioButton rdRed, rdYellow, rdGreen;
private JCheckBox checkRed, checkYellow, checkGreen;
private ButtonGroup group = new ButtonGroup();

public RadioCheckDemo(){
setLayout(new FlowLayout());
panel1.setLayout(new GridLayout(3,1));
panel1.add(rdRed = new JRadioButton("Red"));
panel1.add(rdYellow = new JRadioButton("Yellow"));
panel1.add(rdGreen = new JRadioButton("Green"));
group.add(rdRed);
group.add(rdYellow);
group.add(rdGreen);
panel2.setLayout(new GridLayout(3,1));
panel2.add(checkRed = new JCheckBox("Red"));
panel2.add(checkYellow = new JCheckBox("Yellow"));
panel2.add(checkGreen = new JCheckBox("Green"));
add(panel1);
add(panel2);
}
public static void main(String[] args) {
JFrame frame = new RadioCheckDemo();
frame.setTitle("Text Testing");
frame.setSize(300, 150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Combo Boxes and List
• Keyword “JComboBox”; “JList”
• User only allows to have 1 answer
• Choices can be displayed in a form of list or dropdown
list
• For dropdown list:
– Implement interface ItemListener
– Overrid
• For list:
– Implement interface ListSelectionListener (package
javax.swing.event)
– Overtide valueChanged (ActionEvent e)
Check Box and Radio Button (2)
• Code
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class ComboDemo extends JFrame{


private JLabel labelContinent = new JLabel("Continent : ");
private JLabel labelNation = new JLabel("Nation : ");
private JComboBox comboContinent;
private JList listNation;
private String continent[] = {"America", "Africa", "Asia", "Australia", "Europe"};
private String nation[] = {"United States", "South Africa", "China", "Denmark", "India", "Indonesia", "Canada"};
public ComboDemo(){
listNation = new JList(nation);
listNation.setForeground(Color.BLUE);
listNation.setBackground(Color.RED);
listNation.setSelectionForeground(Color.RED);
listNation.setSelectionBackground(Color.GRAY);
setLayout(new FlowLayout());
add(labelContinent);
add(comboContinent = new JComboBox(continent));
add(labelNation);
add(listNation);
comboContinent.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
JOptionPane.showMessageDialog(null, "You choose continent : " + comboContinent.getSelectedItem());
}
});
listNation.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
JOptionPane.showMessageDialog(null, "You choose nation : "+ listNation.getSelectedValue());
}
});
}
public static void main(String[] args) {
JFrame frame = new ComboDemo();
frame.setTitle("Combo Box Testing");
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Scrollbar and Slider
• Keyword “Scrollbar”; “Jslider”
• Scrollbar allows us to create vertical and horizontal
scroll bar
• A Slider is used to let the user easily enter a numeric
value bounded by a minimum and maximum value
Scrollbar
• Code
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class ScrollDemo extends JFrame implements AdjustmentListener{


private JLabel label = new JLabel();
public ScrollDemo(){
label = new JLabel();
setLayout(new BorderLayout());
JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300);
JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300);
hbar.setUnitIncrement(2);
hbar.setBlockIncrement(1);
hbar.addAdjustmentListener(this);
vbar.addAdjustmentListener(this);
add(hbar, BorderLayout.SOUTH);
add(vbar, BorderLayout.EAST);
add(label, BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame frame = new ScrollDemo();
frame.setTitle("Scroll Testing");
frame.setSize(300, 150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
label.setText(" New Value is " + e.getValue() + " ");
repaint();
}
}
JSlider
• Code
• import java.awt.*;
import javax.swing.*;

public class SliderDemo extends JFrame{


public SliderDemo(){
JSlider slider = new JSlider();
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
slider.setPaintTrack(false);
slider.setPaintLabels(true);
add(slider, BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrame frame = new SliderDemo();
frame.setTitle("Slider Testing");
frame.setSize(300, 150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Reference
• Introduction to Java Programming. 10ed. Liang. Chapter 14
• http://www.java2s.com/Code/Java/Swing-
JFC/AquickdemonstrationofJScrollBarbothverticalandhorizontal.html
• http://www.java2s.com/Code/Java/Swing-JFC/SliderSample.html

Bina Nusantara
ISYS6197 19

Vous aimerez peut-être aussi