• Tidak ada hasil yang ditemukan

Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "Programming in Java"

Copied!
17
0
0

Teks penuh

(1)

Programming in Java

CBCS BHCS03

By Parul Chachra

(2)

Chapter 32

Exploring Swing

Reference: Schildt, H. Java: The Complete Reference. 7th ediFon. McGraw-

Hill EducaFon

(3)

IntroducFon

•  The Swing components provide rich funcFonality and allow a high level of customizaFon

•  These components are all lightweight, which

means that they are all derived from Jcomponent

•  Swing component classes are:

(4)

JLabel and ImageIcon

•  JLabel is Swing’s easiest-to-use component

•  JLabel can be used to display text and/or an icon

•  It is a passive component in that it does not respond to user input

•  JLabel defines several constructors. Here are three of them:

JLabel(Icon icon) JLabel(String str)

JLabel(String str, Icon icon, int align)

•  Here, str and icon are the text and icon used for the label

•  The align argument specifies the horizontal alignment of the text and/or icon within the dimensions of the label

(5)

JLabel and ImageIcon

•  NoFce that icons are specified by objects of type Icon, which is an interface defined by Swing

•  The easiest way to obtain an icon is to use the ImageIcon class

•  ImageIcon implements Icon and encapsulates an image

•  An object of type ImageIcon can be passed as an argument to the Icon parameter of JLabel’s

constructor

•  Here is the ImageIcon constructor used by the example in this secFon:

ImageIcon(String filename)

•  It obtains the image in the file named filename.

(6)

JLabel and ImageIcon

•  The icon and text associated with the label can be obtained by the following methods:

Icon getIcon( ) String getText( )

•  The icon and text associated with a label can be set by these methods:

void setIcon(Icon icon) void setText(String str)

•  Here, icon and str are the icon and text, respecFvely

•  Therefore, using setText( ) it is possible to change the text inside a label during program execuFon

(7)

JLabel and ImageIcon - Program

(8)

JLabel and ImageIcon - Program

(9)

JLabel and ImageIcon - Program

(10)

JTextField

•  JTextField allows you to edit one line of text

•  It is derived from JTextComponent

•  JTextField uses the Document interface for its model

•  Three of JTextField’s constructors are shown here:

JTextField(int cols)

JTextField(String str, int cols) JTextField(String str)

•  Here, str is the string to be iniFally presented, and cols is the number of columns in the text field

•  If no string is specified, the text field is iniFally empty.

•  If the number of columns is not specified, the text field is sized to fit the specified string

•  JTextField generates events in response to user interacFon

•  To obtain the text currently in the text field, call getText( )

(11)

Swing BuXons

•  Swing defines four types of buXons:

JBu<on

JToggleBu<on JCheckBox

JRadioBu<on

•  All are subclasses of the AbstractBu<on class, which extends Jcomponent

•  AbstractBu<on contains many methods that

allow you to control the behavior of buXons

(12)

Swing BuXons

•  For example, you can define different icons that are displayed for the buXon when it is disabled, pressed, or selected

•  The following methods set these icons:

void setDisabledIcon(Icon di) void setPressedIcon(Icon pi) void setSelectedIcon(Icon si) void setRolloverIcon(Icon ri)

•  Here, di, pi, si, and ri are the icons to be used

for the indicated purpose

(13)

Swing BuXons

•  The text associated with a buXon can be read and wriXen via the following methods:

String getText( )

void setText(String str)

•  Here, str is the text to be associated with the

buXon

(14)

JBuXon

•  The JBu<on class provides the funcFonality of a push buXon

•  JBu<on allows an icon, a string, or both to be associated with the push buXon

•  Three of its constructors are shown here:

JBuXon(Icon icon) JBuXon(String str)

JBuXon(String str, Icon icon)

•  Here, str and icon are the string and icon used

for the buXon.

(15)

JBuXon

•  When the buXon is pressed, an AcBonEvent is generated

•  Using the AcBonEvent object passed to the acBonPerformed( ) method of the registered AcBonListener, you can obtain the ac2on

command string associated with the buXon

•  You can set the acFon command by calling setAcBonCommand( ) on the buXon

•  You can obtain the acFon command by calling getAcBonCommand( ) on the event object

•  When using two or more buXons within the same applicaFon, the acFon command gives you an

easy way to determine which buXon was pressed

(16)

JToggleBuXon

•  A toggle buXon looks just like a push buXon, but it acts differently because it has two

states: pushed and released

•  That is, when you press a toggle buXon, it

stays pressed rather than popping back up as a regular push buXon does

•  When you press the toggle buXon a second Fme, it releases (pops up)

•  Toggle buXons are objects of the JToggleBu<on class

•  JToggleBu<on implements AbstractBu<on

(17)

JToggleBuXon

•  JToggleBu<on is a superclass for two other Swing components that also represent two- state controls – JCheckBox and JRadioBu<on

•  JToggleBu<on defines several constructors

•  The one used by the example in this secFon is shown here:

JToggleBuXon(String str)

•  By default, the buXon is in the off posiFon

Referensi

Dokumen terkait

Foreground and Background Colors • To set the background color of an applet’s window, use setBackground • To set the foreground color the color in which text is shown, for example,

Responding to Controls • Except for labels, which are passive, all controls generate events when they are accessed by the user • For example, when the user clicks on a push buQon,

Lightweight Containers • Lightweight containers do inherit JComponent • An example of a lightweight container is JPanel, which is a general-purpose container • Lightweight

Color Methods • You can obtain the red, green, and blue components of a color independently using getRed , getGreen , and getBlue , shown here: int getRed int getGreen int

Adapter Classes • Adapter class can simplify the creaHon of event handlers in certain situaHons • An adapter class provides an empty implementaHon of all methods in an event

• This is done by using addMouseListener and addMouseMo4onListener , which, as menFoned, are members of Component • They are shown here: void addMouseListenerMouseListener ml

Top – Level Containers • The layered pane is an instance of JLayeredPane • The layered pane allows components to be given a depth value • The layered pane holds the content pane

By merging into everyday objects and environments, the presence of an interface can be minimized, making our everyday life more interactive without increasing its complexity.. To obtain