Vous êtes sur la page 1sur 34

The Java Swing tutorial

Introduction
First Programs
Menus and Toolbars
Introduction to the Java Swing Toolkit
About this tutorial
This is an introductory Swing tutorial. The purpose of this tutorial is to get you started with the Java
Swing toolkit. The tutorial has been created and tested on Linux.
About Swing
Swing library is an official Java GUI toolkit released by Sun icrosyste!s.
The !ain characteristics of the Swing toolkit
platfor! independent
custo!i"able
extensible
configurable
lightweight
Swing consists of the following packages
#avax.swing
#avax.swing.border
#avax.swing.colorchooser
#avax.swing.event
#avax.swing.filechooser
#avax.swing.plaf
#avax.swing.plaf.basic
#avax.swing.plaf.!etal
#avax.swing.plaf.!ulti
#avax.swing.plaf.synth
#avax.swing.table
#avax.swing.text
#avax.swing.text.ht!l
#avax.swing.text.ht!l.parser
#avax.swing.text.rtf
#avax.swing.tree
#avax.swing.undo
Page 1 of 34
Swing is probably the !ost advanced toolkit on this planet. It has a rich set of widgets. $ro! basic
widgets like %uttons& Labels& Scrollbars to advanced widgets like Trees and Tables.
Swing is written in '(() #ava.
Swing is a part of J$*& Java $oundation *lasses. It is a collection of packages for creating full featured
desktop applications. J$* consists of +,T& Swing& +ccessibility& Java -.& and .rag and .rop. Swing was
released in '//0 with J.1 '.-. It is a !ature toolkit.
The Java platfor! has Java-. library& which enables developers to create advanced -. graphics and
i!aging.
There are basically two types of widget toolkits.
Lightweight
2eavyweight
+ heavyweight toolkit uses 3S4s +5I to draw the widgets. $or exa!ple %orland4s 6*L is a heavyweight
toolkit. It depends on ,I78- +5I& the built in ,indows application progra!!ing interface. 3n Unix
syste!s& we have GT19 toolkit& which is built on top of :'' library. Swing is a lightweight toolkit. It
paints it4s own widgets. It is in fact the only lightweight toolkit I know about.
SWT library
There is also another GUI library for the Java progra!!ing language. It is called S,T. The Standard
widget toolkit. The S,T library was initially developed by the I% corporation. 7ow it is an open source
pro#ect& supported by I%. The S,T is an exa!ple of a heavyweight toolkit. It lets the underlying 3S
to create GUI. S,T uses the #ava native interface to do the #ob. The !ain advantages of the S,T are
speed and native look and feel. The S,T is on the other hand !ore error prone. It is less powerful
then Swing. It is also ;uite ,indows centric library.
Java Swing first programs
In this chapter& we will progra! our first progra!s in Swing toolkit. The exa!ples are going to be very
si!ple. ,e will cover so!e basic functionality.
Our first example
In our first exa!ple& we will show a basic window.
import javax.swing.JFrame;
public class Simple extends JFrame {
public Simple() {
setSize(3! ");
Page 2 of 34
set#itle($Simple$);
set%e&ault'lose(peration()*+#,(FF,'-(S));
.
public static void main(String/0 args) {
Simple simple 1 new Simple();
simple.set2isible(true);
.
.
,hile this code is very s!all& the application window can do ;uite a lot. It can be resi"ed& !axi!i"ed&
!ini!i"ed. +ll the co!plexity that co!es with it has been hidden fro! the application progra!!er.
import javax.swing.JFrame;
2ere we i!port the J$ra!e widget. It is a top level container& which is used for placing other widgets.
setSize(3! ");
set#itle($Simple$);
This code will resi"e the window to be 8((px wide and -((px tall. It will set the title of the window to
Si!ple.
set%e&ault'lose(peration()*+#,(3,'-(S));
This !ethod will close the window& if we click on the close button. %y default nothing happens.
$igure< Si!ple
Page 3 of 34
Centering window on the screen
%y default& when the window is shown on the screen& it is not centered. It will pop up !ost likely in the
upper left corner. The following code exa!ple will show the window centered on the screen.
import java.awt.%imension;
import java.awt.#ool4it;
import javax.swing.JFrame;
public class 'enter(nScreen extends JFrame {
public 'enter(nScreen() {
setSize(3! ");
set#itle($'enter(nScreen$);
set%e&ault'lose(peration()*+#,(3,'-(S));
#ool4it tool4it 1 get#ool4it();
%imension size 1 tool4it.getScreenSize();
set-ocation(size.widt56" 7 get8idt5()6"!
size.5eig5t6" 7 get9eig5t()6");
.
public static void main(String/0 args) {
'enter(nScreen cos 1 new 'enter(nScreen();
cos.set2isible(true);
.
.
To center the window on the screen& we !ust know the resolution of the !onitor. $or this& we use the
Toolkit class.
#ool4it tool4it 1 get#ool4it();
%imension size 1 tool4it.getScreenSize();
Page 4 of 34
,e get the toolkit and figure out the screen si"e.
set-ocation(size.widt56" 7 get8idt5()6"! size.5eig5t6" 7 get9eig5t()6");
To place the window on the screen& we call the setLocation() !ethod.
Buttons
In our next exa!ple& we will show two buttons. The first button will beep a sound and the second
button will close the window.
import java.awt.%imension;
import java.awt.#ool4it;
import java.awt.event.:ction)vent;
import java.awt.event.:ction-istener;
import javax.swing.J;utton;
import javax.swing.JFrame;
import javax.swing.J<anel;
public class ;uttons extends JFrame {
private #ool4it tool4it;
public ;uttons() {
set#itle($;uttons$);
setSize(3! ");
tool4it 1 get#ool4it();
%imension size 1 tool4it.getScreenSize();
set-ocation((size.widt5 7 get8idt5())6"! (size.5eig5t 7 get9eig5t())6");
set%e&ault'lose(peration()*+#,(3,'-(S));
J<anel panel 1 new J<anel();
get'ontent<ane().add(panel);
panel.set-a=out(null);
Page 5 of 34
J;utton beep 1 new J;utton($;eep$);
beep.set;ounds(>?! @! A! 3);
beep.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent event) {
tool4it.beep();
.
.);
J;utton close 1 new J;utton($'lose$);
close.set;ounds(?! @! A! 3);
close.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent event) {
S=stem.exit();
.
.);
panel.add(beep);
panel.add(close);
.
public static void main(String/0 args) {
;uttons buttons 1 new ;uttons();
buttons.set2isible(true);
.
.
Page 6 of 34
$igure< %uttons
In this exa!ple& we will see two new topics. Layout !anage!ent and event handling. They will be
touched only briefly. %oth of the topics will have their own chapter.
J<anel panel 1 new J<anel();
get'ontent<ane().add(panel);
,e create a JPanel co!ponent. It is a generic lightweight container. ,e add the J5anel to the J$ra!e.
panel.set-a=out(null);
%y default& the J5anel has a FlowLayout !anager. The layout !anager is used to place widgets onto
the containers. If we call setLayout(null) we can position our co!ponents absolutely. $or this& we use
the setBounds() !ethod.
J;utton beep 1 new J;utton($;eep$);
beep.set;ounds(>?! @! A! 3);
beep.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent event) {
tool4it.beep();
.
.);
2ere we create a button. ,e position it by calling the setBounds() !ethod. Then we add an action
listener. The action listener will be called& when we perfor! an action on the button. In our case& if we
click on the button. The beep button will play a si!ple beep sound.
S=stem.exit();
The close button will exit the application. $or this& we call the System.exit() !ethod.
Page 7 of 34
panel.add(beep);
panel.add(close);
In order to show the buttons& we !ust add the! to the panel.
A tooltip
Tooltips are part of the internal application4s help syste!. The Swing shows a s!all rectangular
window& if we hover a !ouse pointer over an ob#ect.
import java.awt.%imension;
import java.awt.#ool4it;
import javax.swing.J;utton;
import javax.swing.JFrame;
import javax.swing.J<anel;
public class #ooltip extends JFrame {
private #ool4it tool4it;
public #ooltip() {
set#itle($#ooltip$);
setSize(3! ");
tool4it 1 get#ool4it();
%imension size 1 tool4it.getScreenSize();
set-ocation((size.widt5 7 get8idt5())6"! (size.5eig5t 7 get9eig5t())6");
set%e&ault'lose(peration()*+#,(3,'-(S));
J<anel panel 1 new J<anel();
get'ontent<ane().add(panel);
panel.set-a=out(null);
panel.set#ool#ip#ext($: <anel container$);
Page 8 of 34
J;utton button 1 new J;utton($;utton$);
button.set;ounds(>! @! A! 3);
button.set#ool#ip#ext($: button component$);
panel.add(button);
.
public static void main(String/0 args) {
#ooltip tooltip 1 new #ooltip();
tooltip.set2isible(true);
.
.
In the exa!ple& we set the tooltip for the fra!e and the button.
panel.set#ool#ip#ext($: <anel container$);
To enable a tooltip& we call the setTooltipText() !ethod.
$igure< Tooltip
Menus and toolbars in Java Swing
Page 9 of 34
Creating a menubar
+ !enubar is one of the !ost visible parts of the GUI application. It is a group of co!!ands located in
various !enus. ,hile in console applications you had to re!e!ber all those arcane co!!ands& here
we have !ost of the co!!ands grouped into logical parts. There are accepted standards that further
reduce the a!ount of ti!e spending to learn a new application.
In Java Swing& to i!ple!ent a !enubar& we use three ob#ects. + JMenuBar& a JMenu and a
JMenutem.
Simple menubar
,e begin with a si!ple !enubar exa!ple.
pac4age com.zetcode;
import java.awt.event.:ction)vent;
import java.awt.event.:ction-istener;
import java.awt.event.Be=)vent;
import javax.swing.+mage+con;
import javax.swing.JFrame;
import javax.swing.JCenu;
import javax.swing.JCenu;ar;
import javax.swing.JCenu+tem;
public class Cenu extends JFrame {
public Cenu() {
set#itle($JCenu;ar$);
JCenu;ar menubar 1 new JCenu;ar();
+mage+con icon 1 new +mage+con($exit.png$);
JCenu &ile 1 new JCenu($File$);
&ile.setCnemonic(Be=)vent.2B,F);
JCenu+tem &ile'lose 1 new JCenu+tem($'lose$! icon);
&ile'lose.setCnemonic(Be=)vent.2B,');
Page 10 of 34
&ile'lose.set#ool#ip#ext($)xit application$);
&ile'lose.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent event) {
S=stem.exit();
.
.);
&ile.add(&ile'lose);
menubar.add(&ile);
setJCenu;ar(menubar);
setSize("?! ");
set-ocationDelative#o(null);
set%e&ault'lose(peration()*+#,(3,'-(S));
set2isible(true);
.
public static void main(String/0 args) {
new Cenu();
.
.
3ur exa!ple will show a !enu with one ite!. Selecting the close !enu ite! we close the application.
JCenu;ar menubar 1 new JCenu;ar();
2ere we create a !enubar.
+mage+con icon 1 new +mage+con($exit.png$);
,e will display an icon in the !enu.
JCenu &ile 1 new JCenu($File$);
Page 11 of 34
&ile.setCnemonic(Be=)vent.2B,F);
,e create a !enu ob#ect. The !enus can be accessed via the keybord as well. To bind a !enu to a
particular key& we use the setMnemonic !ethod. In our case& the !enu can be opened with the A!T
" # shortcut.
&ile'lose.set#ool#ip#ext($)xit application$);
This code line creates a tooltip for a !enu ite!.
$igure< Jenu%ar
Submenu
=ach !enu can also have a sub!enu. This way we can group si!ilar co!!nads into groups. $or
exa!ple we can place co!!ands that hide>show various toolbars like personal bar& address bar& status
bar or navigation bar into a sub!enu called toolbars. ,ithin a !enu& we can seperate co!!ands with
a separator. It is a si!ple line. It is co!!on practice to separate co!!ands like new& open& save fro!
co!!ands like print& print preview with a single separator. enus co!!ands can be launched via
keyboard shortcuts. $or this& we define !enu ite! accelerators.
import java.awt.event.:ction)vent;
import java.awt.event.:ction-istener;
import java.awt.event.Be=)vent;
import javax.swing.+mage+con;
import javax.swing.JFrame;
import javax.swing.JCenu;
import javax.swing.JCenu;ar;
import javax.swing.JCenu+tem;
import javax.swing.Be=Stro4e;
Page 12 of 34
public class Submenu extends JFrame {
public Submenu() {
set#itle($Submenu$);
JCenu;ar menubar 1 new JCenu;ar();
+mage+con icon3ew 1 new +mage+con($new.png$);
+mage+con icon(pen 1 new +mage+con($open.png$);
+mage+con iconSave 1 new +mage+con($save.png$);
+mage+con icon'lose 1 new +mage+con($exit.png$);
JCenu &ile 1 new JCenu($File$);
&ile.setCnemonic(Be=)vent.2B,F);
JCenu imp 1 new JCenu($+mport$);
imp.setCnemonic(Be=)vent.2B,C);
JCenu+tem news& 1 new JCenu+tem($+mport news&eed list...$);
JCenu+tem boo4m 1 new JCenu+tem($+mport boo4mar4s...$);
JCenu+tem mail 1 new JCenu+tem($+mport mail...$);
imp.add(news&);
imp.add(boo4m);
imp.add(mail);
JCenu+tem &ile3ew 1 new JCenu+tem($3ew$! icon3ew);
&ile3ew.setCnemonic(Be=)vent.2B,3);
JCenu+tem &ile(pen 1 new JCenu+tem($(pen$! icon(pen);
&ile3ew.setCnemonic(Be=)vent.2B,();
JCenu+tem &ileSave 1 new JCenu+tem($Save$! iconSave);
Page 13 of 34
&ileSave.setCnemonic(Be=)vent.2B,S);
JCenu+tem &ile'lose 1 new JCenu+tem($'lose$! icon'lose);
&ile'lose.setCnemonic(Be=)vent.2B,');
&ile'lose.set#ool#ip#ext($)xit application$);
&ile'lose.set:ccelerator(Be=Stro4e.getBe=Stro4e(Be=)vent.2B,8!
:ction)vent.'#D-,C:SB));
&ile'lose.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent event) {
S=stem.exit();
.
.);
&ile.add(&ile3ew);
&ile.add(&ile(pen);
&ile.add(&ileSave);
&ile.addSeparator();
&ile.add(imp);
&ile.addSeparator();
&ile.add(&ile'lose);
menubar.add(&ile);
setJCenu;ar(menubar);
setSize(3@! "?);
set-ocationDelative#o(null);
set%e&ault'lose(peration()*+#,(3,'-(S));
set2isible(true);
Page 14 of 34
.
public static void main(String/0 args) {
new Submenu();
.
.
In this exa!ple& we create a sub!enu& a !enu separator and an accelerator key.
JCenu imp 1 new JCenu($+mport$);
...
&ile.add(imp);
+ sub!enu is #ust like any other nor!al !enu. It is created the sa!e way. ,e si!ply add a !enu to
existing !enu.
&ile'lose.set:ccelerator(Be=Stro4e.getBe=Stro4e(Be=)vent.2B,8!
:ction)vent.'#D-,C:SB));
+n accelerator is a key shortcut that launches a !enu ite!. In our case& by pressing Ctrl " W we close
the application.
&ile.addSeparator();
+ separator is a vertical line that visually separates the !enu ite!s. This way we can group ite!s into
so!e logical places.
Page 15 of 34
$igure< Sub!enu
JChec$BoxMenutem
+ !enu ite! that can be selected or deselected. If selected& the !enu ite! typically appears with a
check!ark next to it. If unselected or deselected& the !enu ite! appears without a check!ark. Like a
regular !enu ite!& a check box !enu ite! can have either text or a graphic icon associated with it& or
both.
import java.awt.;order-a=out;
import java.awt.event.:ction)vent;
import java.awt.event.:ction-istener;
import java.awt.event.Be=)vent;
import javax.swing.;orderFactor=;
import javax.swing.J'5ec4;oxCenu+tem;
import javax.swing.JFrame;
import javax.swing.J-abel;
import javax.swing.JCenu;
import javax.swing.JCenu;ar;
import javax.swing.E+Canager;
import javax.swing.border.)tc5ed;order;
Page 16 of 34
public class '5ec4Cenu+tem extends JFrame {
private J-abel statusbar;
public '5ec4Cenu+tem() {
set#itle($'5ec4;oxCenu+tem$);
JCenu;ar menubar 1 new JCenu;ar();
JCenu &ile 1 new JCenu($File$);
&ile.setCnemonic(Be=)vent.2B,F);
JCenu view 1 new JCenu($2iew$);
view.setCnemonic(Be=)vent.2B,2);
J'5ec4;oxCenu+tem sbar 1 new J'5ec4;oxCenu+tem($S5ow Statu;ar$);
sbar.setState(true);
sbar.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent event) {
i& (statusbar.is2isible()) {
Page 17 of 34
statusbar.set2isible(&alse);
. else {
statusbar.set2isible(true);
.
.
.);
view.add(sbar);
menubar.add(&ile);
menubar.add(view);
setJCenu;ar(menubar);
statusbar 1 new J-abel($ Statusbar$);
statusbar.set;order(;orderFactor=.create)tc5ed;order(
)tc5ed;order.D:+S)%));
add(statusbar! ;order-a=out.S(E#9);
setSize(3@! "?);
set-ocationDelative#o(null);
set%e&ault'lose(peration()*+#,(3,'-(S));
Page 18 of 34
set2isible(true);
.
public static void main(String/0 args) {
new '5ec4Cenu+tem();
.
.
The exa!ple shows a JChec$BoxMenutem%. %y selecting the !enu ite!& we toggle the visibility of
the statusbar.
J'5ec4;oxCenu+tem sbar 1 new J'5ec4;oxCenu+tem($S5ow Statu;ar$);
sbar.setState(true);
,e create the JChec$BoxMenutem and check it by default. The statusbar is initially visible.
i& (statusbar.is2isible()) {
statusbar.set2isible(&alse);
. else {
statusbar.set2isible(true);
.
2ere we toggle the visibility of the statusbar.
statusbar 1 new J-abel($ Statusbar$);
statusbar.set;order(;orderFactor=.create)tc5ed;order()tc5ed;order.D:+S)%));
The statusbar is a si!ple J!abel co!ponent. ,e put a raised &tchedBorder around the label& so that
it is discernible.
Page 19 of 34
$igure< J*heck%oxenuIte!
A popup menu
+nother type of a !enu is a popup !enu. It is so!eti!es called a context !enu. It is usually shown&
when we right click on a co!ponent. The idea is to provide only the co!!ands& that are relevant to
the current context. Say we have an i!age. %y right clicking on the i!age& we get a window with
co!!ands to save& rescale& !ove etc the i!age.
import java.awt.#ool4it;
import javax.swing.F;
import java.awt.event.F;
public class <opupCenu {
private J<opupCenu menu;
private #ool4it tool4it;
public <opupCenu(){
Page 20 of 34
JFrame &rame 1 new JFrame($J<opupCenu$);
&rame.set%e&ault'lose(peration(JFrame.)*+#,(3,'-(S));
tool4it 1 &rame.get#ool4it();
menu 1 new J<opupCenu();
JCenu+tem menu+tem;eep 1 new JCenu+tem($;eep$);
menu+tem;eep.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent e) {
tool4it.beep();
.
.);
menu.add(menu+tem;eep);
JCenu+tem menu+tem'lose 1 new JCenu+tem($'lose$);
menu+tem'lose.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent e) {
S=stem.exit();
.
.);
Page 21 of 34
menu.add(menu+tem'lose);
&rame.addCouse-istener(new Couse:dapter() {
public void mouseDeleased(Couse)vent e) {
i& (e.get;utton() 11 e.;E##(33) {
menu.s5ow(e.get'omponent()! e.get*()! e.getG());
.
.
.);
&rame.setSize("?! ");
&rame.set-ocationDelative#o(null);
&rame.set2isible(true);
.
public static void main(String/0 args) {
new <opupCenu();
.
.
3ur exa!ple shows a de!onstrational popup !enu with two co!!ands. The first option of the popup
!enu will beep a sound& the second one will close the window.
In our exa!ple& we create a sub!enu& !enu separators and create an accelerator key.
menu 1 new J<opupCenu();
Page 22 of 34
To create a popup !enu& we have a class called J'opupMenu.
JCenu+tem menu+tem;eep 1 new JCenu+tem($;eep$);
The !enu ite! is the sa!e& as with the standard JMenu
&rame.addCouse-istener(new Couse:dapter() {
public void mouseDeleased(Couse)vent e) {
i& (e.get;utton() 11 e.;E##(33) {
menu.s5ow(e.get'omponent()! e.get*()! e.getG());
.
.
.);
The popup !enu is shown& where we clicked with the !ouse button. The B(TTO)* constant is here to
enable the popup !enu only for the !ouse right click.
$igure< 5opup !enu
JToolbar
enus group co!!ands that we can use in an application. Toolbars provide a ;uick access to the !ost
fre;uently used co!!ands. In Java Swing& the JToolBar class creates a toolbar in an application.
import java.awt.;order-a=out;
import java.awt.event.:ction)vent;
import java.awt.event.:ction-istener;
Page 23 of 34
import javax.swing.+mage+con;
import javax.swing.J;utton;
import javax.swing.JFrame;
import javax.swing.JCenu;
import javax.swing.JCenu;ar;
import javax.swing.J#ool;ar;
public class Simple#oolbar extends JFrame {
public Simple#oolbar() {
set#itle($Simple#oolbar$);
JCenu;ar menubar 1 new JCenu;ar();
JCenu &ile 1 new JCenu($File$);
menubar.add(&ile);
setJCenu;ar(menubar);
J#ool;ar toolbar 1 new J#ool;ar();
+mage+con icon 1 new +mage+con(get'lass().getDesource($exit.png$));
Page 24 of 34
J;utton exit 1 new J;utton(icon);
toolbar.add(exit);
exit.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent event) {
S=stem.exit();
.
.);
add(toolbar! ;order-a=out.3(D#9);
setSize(3! ");
set-ocationDelative#o(null);
set%e&ault'lose(peration()*+#,(3,'-(S));
set2isible(true);
.
public static void main(String/0 args) {
new Simple#oolbar();
.
.
The exa!ple creates a toolbar with one exit button.
J#ool;ar toolbar 1 new J#ool;ar();
Page 25 of 34
This is the JToolBar constructor.
J;utton exit 1 new J;utton(icon);
toolbar.add(exit);
,e create a button and add it to the toolbar.
$igure< Si!ple JTool%ar
Toolbars
Say& we wanted to create two toolbars. The next exa!ple shows& how we could do it.
import java.awt.;order-a=out;
import java.awt.event.:ction)vent;
import java.awt.event.:ction-istener;
import javax.swing.;ox-a=out;
import javax.swing.+mage+con;
import javax.swing.J;utton;
import javax.swing.JFrame;
import javax.swing.J<anel;
import javax.swing.J#ool;ar;
Page 26 of 34
public class #oolbars extends JFrame {
public #oolbars() {
set#itle($#oolbars$);
J#ool;ar toolbar> 1 new J#ool;ar();
J#ool;ar toolbar" 1 new J#ool;ar();
J<anel panel 1 new J<anel();
panel.set-a=out(new ;ox-a=out(panel! ;ox-a=out.G,:*+S));
+mage+con newi 1 new +mage+con(
get'lass().getDesource($new.png$));
+mage+con open 1 new +mage+con(
get'lass().getDesource($open.png$));
+mage+con save 1 new +mage+con(
get'lass().getDesource($save.png$));
+mage+con exit 1 new +mage+con(
get'lass().getDesource($exit.png$));
J;utton newb 1 new J;utton(newi);
J;utton openb 1 new J;utton(open);
Page 27 of 34
J;utton saveb 1 new J;utton(save);
toolbar>.add(newb);
toolbar>.add(openb);
toolbar>.add(saveb);
toolbar>.set:lignment*();
J;utton exitb 1 new J;utton(exit);
toolbar".add(exitb);
toolbar".set:lignment*();
exitb.add:ction-istener(new :ction-istener() {
public void action<er&ormed(:ction)vent event) {
S=stem.exit();
.
.);
panel.add(toolbar>);
panel.add(toolbar");
add(panel! ;order-a=out.3(D#9);
setSize(3! ");
Page 28 of 34
set-ocationDelative#o(null);
set%e&ault'lose(peration()*+#,(3,'-(S));
set2isible(true);
.
public static void main(String/0 args) {
new #oolbars();
.
.
,e show only one way& how we could create toolbars. 3f course& there are several possibilities. ,e put
a J'anel to the north of the Border!ayout !anager. The panel has a vertical Box!ayout. ,e add the
two toolbars into this panel.
J#ool;ar toolbar> 1 new J#ool;ar();
J#ool;ar toolbar" 1 new J#ool;ar();
*reation of two toolbars.
J<anel panel 1 new J<anel();
panel.set-a=out(new ;ox-a=out(panel! ;ox-a=out.G,:*+S));
The panel has a vertical Box!ayout.
toolbar>.set:lignment*();
The toolbar is left aligned.
panel.add(toolbar>);
panel.add(toolbar");
add(panel! ;order-a=out.3(D#9);
,e add the toolbars to the panel. $inally& the panel is located into the north part of the fra!e.
Page 29 of 34
$igure< Toolbars
A +ertical toobar
The following exa!ple shows a vertical toobar.
import java.awt.;order-a=out;
import javax.swing.+mage+con;
import javax.swing.J;utton;
import javax.swing.JFrame;
import javax.swing.J#ool;ar;
import javax.swing.E+Canager;
public class 2ertical#oolbar extends JFrame {
public 2ertical#oolbar() {
set#itle($2ertical toolbar$);
Page 30 of 34
J#ool;ar toolbar 1 new J#ool;ar(J#ool;ar.2)D#+':-);
+mage+con select 1 new +mage+con(
get'lass().getDesource($select.gi&$));
+mage+con &ree5and 1 new +mage+con(
get'lass().getDesource($&ree5and.gi&$));
+mage+con s5apeed 1 new +mage+con(
get'lass().getDesource($s5apeed.gi&$));
+mage+con pen 1 new +mage+con(
get'lass().getDesource($pen.gi&$));
+mage+con rectangle 1 new +mage+con(
get'lass().getDesource($rectangle.gi&$));
+mage+con ellipse 1 new +mage+con(
get'lass().getDesource($ellipse.gi&$));
+mage+con Hs 1 new +mage+con(
get'lass().getDesource($Hs.gi&$));
+mage+con text 1 new +mage+con(
get'lass().getDesource($text.gi&$));
J;utton selectb 1 new J;utton(select);
J;utton &ree5andb 1 new J;utton(&ree5and);
J;utton s5apeedb 1 new J;utton(s5apeed);
J;utton penb 1 new J;utton(pen);
Page 31 of 34
J;utton rectangleb 1 new J;utton(rectangle);
J;utton ellipseb 1 new J;utton(ellipse);
J;utton Hsb 1 new J;utton(Hs);
J;utton textb 1 new J;utton(text);
toolbar.add(selectb);
toolbar.add(&ree5andb);
toolbar.add(s5apeedb);
toolbar.add(penb);
toolbar.add(rectangleb);
toolbar.add(ellipseb);
toolbar.add(Hsb);
toolbar.add(textb);
add(toolbar! ;order-a=out.8)S#);
setSize("?! 3?);
set-ocationDelative#o(null);
set%e&ault'lose(peration()*+#,(3,'-(S));
set2isible(true);
.
public static void main(String/0 args) {
tr= {
Page 32 of 34
E+Canager.set-oo4:ndFeel(
E+Canager.getS=stem-oo4:ndFeel'lass3ame());
.
catc5 ()xception e) {
S=stem.out.println($)rrorI$ J e.getStac4#race());
.
new 2ertical#oolbar();
.
.
In the exa!ple& we put a vertical toolbar to the left side of the window. This is typical for a graphics
applications like ,ara &xtreme or n$scape. In our exa!ple& we use icons fro! :ara =xtre!e
application.
J#ool;ar toolbar 1 new J#ool;ar(J#ool;ar.2)D#+':-);
,e create a vertical toolbar.
add(toolbar! ;order-a=out.8)S#);
The toolbar is placed into the left part of the window.
E+Canager.set-oo4:ndFeel(
E+Canager.getS=stem-oo4:ndFeel'lass3ame());
I used the syste! look and feel. In !y case& it was the gtk the!e. The icons are not transparent& and
they would not look ok on a different the!e.
Page 33 of 34
$igure< 6ertical toolbar
Page 34 of 34

Vous aimerez peut-être aussi