Sie sind auf Seite 1von 20

A Visual Guide to Swing Components (Java Look and Feel)

This page shows Swing components in the Java look and feel. The following page shows the same components in the Windows look and feel.

Basic Controls
Simple components that are used primarily to get input from the user; they may also show simple state.

JButton JCheckBox JCom oBox J!ist

JSlider

J#adioButton

J"enu

JSpinner

JText$ield

J%assword$ield

JBUTTON:
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import import import import import import javax.swing.ImageIcon; javax.swing.JButton; javax.swing.JFrame; javax.swing.JPanel; javax.swing.JTextField; javax.swing.Swing tilities;

public class !ain"lass# $$ $$public static void main%String&'$a(# $$$$JFrame$)$*$new JFrame%(; $$$$).set+e)ault"lose,-eration%JFrame.E.IT/,0/"L,SE(; $$$$).add%new Button+emo%((; $$$$).setSi1e%2334233(; $$$$).set5isi6le%true(; $$7 7 class Button+emo$extends JPanel$implements ActionListener$# $$JTextField$jt); $$public Button+emo%($# $$$$try # $$$$$$Swing tilities.invo8eAnd9ait%new :unna6le%($# $$$$$$$$public void run%($# $$$$$$$$$$ma8e; I%(; $$$$$$$$7 $$$$$$7(; $$$$7$catch %Exce-tion$exc($# $$$$$$System.out.-rintln%<"an=t$create$6ecause$o)$<$>$exc(; $$$$7 $$7 $$private void ma8e; I%($# $$$$setLayout%new FlowLayout%((; $$$$ImageIcon$)rance$*$new ImageIcon%<)rance.gi)<(; $$$$JButton$j6$*$new JButton%)rance(; $$$$j6.setAction"ommand%<France<(; $$$$j6.addActionListener%this(; $$$$add%j6(; $$$$ImageIcon$germany$*$new ImageIcon%<germany.gi)<(; $$$$j6$*$new JButton%germany(; $$$$j6.setAction"ommand%<;ermany<(; $$$$j6.addActionListener%this(; $$$$add%j6(; $$$$ImageIcon$italy$*$new ImageIcon%<italy.gi)<(; $$$$j6$*$new JButton%italy(;

$$$$j6.setAction"ommand%<Italy<(; $$$$j6.addActionListener%this(; $$$$add%j6(; $$$$ImageIcon$ja-an$*$new ImageIcon%<ja-an.gi)<(; $$$$j6$*$new JButton%ja-an(; $$$$j6.setAction"ommand%<Ja-an<(; $$$$j6.addActionListener%this(; $$$$add%j6(; $$$$jt)$*$new JTextField%?2(; $$$$add%jt)(; $$7 $$public void actionPer)ormed%ActionEvent$ae($# $$$$jt).setText%ae.getAction"ommand%((; $$7 7

JLabel
1. e text. The javax.swing.JLabel class has the following constructors. .

1.

If you want to use multifonts or multicolors in a JLabel, you can pass HT L tags,

! JLabel subclass is use" as the "efault ren"erer for each of the JList, J#ombo$ox, JTable, an" JT components.

!lignments have an effect only if there%s extra space for the layout manager to position the compone

import$java.awt."olor; import$java.awt.+imension; import$java.awt.FlowLayout; import$javax.swing.BorderFactory; import$javax.swing.JFrame; import$javax.swing.JLa6el; import$javax.swing.6order.Border;

public$class$!yLa6el$# $$public$static$void$main%String$args&'($# $$$$JFrame$)$*$new$JFrame%<La6el$+emo<(; $$$$).setLayout%new$FlowLayout%((; $$$$).setSi1e%@334$AB3(; $$$$).set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$ $$$$JLa6el$la6el*$new$JLa6el%<asd)<(; $$$$Border$6order$*$BorderFactory.createLineBorder%"olor.BLACK(; $$$$la6el.setBorder%6order(; $$$$la6el.setPre)erredSi1e%new$+imension%?234$?33((; $$$$ $$$$la6el.setText%<"entered<(; $$$$la6el.setCori1ontalAlignment%JLa6el.LEFT(; $$$$la6el.set5erticalAlignment%JLa6el.TOP(; $$$$ $$$$).add%la6el(; $$$$).set5isi6le%true(; $$7 7

TEXT FIELD:
import$javax.swing.BoxLayout; import$javax.swing.JFrame; import$javax.swing.JScrollPane; import$javax.swing.JTextArea; import$javax.swing.text.+ocument; public$class$SDare!odel$# $$public$static$void$main%String$args&'($# $$$$JFrame$)rame$*$new$JFrame%<SDaring$Sam-le<(; $$$$)rame.set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$JTextArea$textarea?$*$new$JTextArea%(; $$$$+ocument$document$*$textarea?.get+ocument%(; $$$$JTextArea$textarea@$*$new$JTextArea%document(; $$$$JTextArea$textareaA$*$new$JTextArea%document(; $$$$)rame.setLayout%new$BoxLayout%)rame.get"ontentPane%(4$ BoxLayout.Y_AXIS((; $$$$)rame.add%new$JScrollPane%textarea?((; $$$$)rame.add%new$JScrollPane%textarea@((; $$$$)rame.add%new$JScrollPane%textareaA((; $$$$)rame.setSi1e%A334$E33(; $$$$)rame.set5isi6le%true(; $$7 7

JText!rea& for multiple'line input.


JTextArea$textArea$*$new JTextArea%(; JScrollPane$scrollPane$*$new JScrollPane%textArea(; content.add%scrollPane(;

(ut JText!rea within a J)croll(ane to allow a user to properly scroll through the contents of a JText

import$java.awt.+imension; import$java.awt.FlowLayout; import$javax.swing.JFrame; import$javax.swing.JScrollPane; import$javax.swing.JTextArea; public$class$JTextAreaTest$# $$public$static$void$main%String&'$args($# $$$$JFrame.setDefaultLookAndFeelDecorated%true(; $$$$JFrame$)rame$*$new$JFrame%<JTextArea$Test<(; $$$$)rame.setLayout%new$FlowLayout%((; $$$$)rame.set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$String$text$*$<A$JTextArea$o6ject$re-resents$a$multiline$area$)or$ dis-laying$text.$< $$$$$$$$>$<Fou$can$cDange$tDe$num6er$o)$lines$tDat$can$6e$dis-layed$at$ a$time4$< $$$$$$$$>$<as$well$as$tDe$num6er$o)$columns.$Fou$can$wra-$lines$and$ words$too.$< $$$$$$$$>$<Fou$can$also$-ut$your$JTextArea$in$a$JScrollPane$to$ma8e$it$ scrolla6le.<; $$$$JTextArea$textAreal$*$new$JTextArea%text4$24$?3(; $$$$textAreal.setPre)erredSi1e%new$+imension%?334$?33((; $$$$JTextArea$textArea@$*$new$JTextArea%text4$24$?3(; $$$$textArea@.setPre)erredSi1e%new$+imension%?334$?33((; $$$$JScrollPane$scrollPane$*$new$JScrollPane%textArea@4$ JScrollPane. E!TICAL_SC!OLLBA!_AL"AYS4 $$$$$$$$JScrollPane.#O!I$ONTAL_SC!OLLBA!_AL"AYS(; $$$$textAreal.setLine9ra-%true(; $$$$textArea@.setLine9ra-%true(; $$$$)rame.add%textAreal(; $$$$)rame.add%scrollPane(; $$$$)rame.-ac8%(; $$$$)rame.set5isi6le%true(; $$7 7 PASSWORD FIE D! import$java.awt.;ridLayout; import$javax.swing.JFrame; import$javax.swing.JLa6el; import$javax.swing.JPasswordField; import$javax.swing.JTextField;

import$javax.swing.Swing"onstants; public$class$JPasswordFieldTest$extends$JFrame$# $$public$static$void$main%String&'$args($# $$$$JFrame.setDefaultLookAndFeelDecorated%true(; $$$$JFrame$)rame$*$new$JFrame%(; $$$$)rame.set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$)rame.setTitle%<JTextField$Test<(; $$$$)rame.setLayout%new$;ridLayout%@4$@((; $$$$JLa6el$la6el$*$new$JLa6el%< ser$0ameG<4$Swing"onstants.!I%#T(; $$$$JLa6el$la6el@$*$new$JLa6el%<PasswordG<4$Swing"onstants.!I%#T(; $$$$JTextField$user0ameField$*$new$JTextField%@3(; $$$$JPasswordField$-asswordField$*$new$JPasswordField%(; $$$$)rame.add%la6el(; $$$$)rame.add%user0ameField(; $$$$)rame.add%la6el@(; $$$$)rame.add%-asswordField(; $$$$)rame.setSi1e%@334$H3(; $$$$)rame.set5isi6le%true(; $$7 7

"#$E#%&O'!

#reating J#hec*$ox #omponents


public J"Dec8Box%( J"Dec8Box$a"Dec8Box$*$new J"Dec8Box%(; public J"Dec8Box%Icon$icon( J"Dec8Box$a"Dec8Box$*$new J"Dec8Box%new +iamondIcon%"olor.:E+4$(alse((; a"Dec8Box.setSelectedIcon%new +iamondIcon%"olor.PI0I4$true((; public J"Dec8Box%Icon$icon4$boolean selected( J"Dec8Box$a"Dec8Box$*$new J"Dec8Box%new +iamondIcon%"olor.:E+4$(alse(4$true(; a"Dec8Box.setSelectedIcon%new +iamondIcon%"olor.PI0I4$true((; public J"Dec8Box%String$text( J"Dec8Box$a"Dec8Box$*$new J"Dec8Box%<S-inacD<(; public J"Dec8Box%String$text4$boolean selected( J"Dec8Box$a"Dec8Box$*$new J"Dec8Box%<,nions<4$true(; public J"Dec8Box%String$text4$Icon$icon( J"Dec8Box$a"Dec8Box$*$new J"Dec8Box%<;arlic<4$new +iamondIcon%"olor.:E+4$(alse((; a"Dec8Box.setSelectedIcon%new +iamondIcon%"olor.PI0I4$true((;

public J"Dec8Box%String$text4$Icon$icon4$boolean selected( J"Dec8Box$a"Dec8Box$*$new J"Dec8Box%<AncDovies<4$new +iamondIcon%"olor.:E+4$$(alse(4$tr a"Dec8Box.setSelectedIcon%new +iamondIcon%"olor.PI0I4$true((; public J"Dec8Box%Action$action( Action$action$*$...;

J"Dec8Box$a"Dec8Box$*$new J"Dec8Box%action(;

+sing J#hec*$ox

import java.awt.FlowLayout; import javax.swing.J"Dec8Box; import javax.swing.JFrame; import javax.swing.JLa6el; public class J"Dec8BoxTest$# $$public static void main%String&'$args($# $$$$JFrame.set+e)aultLoo8AndFeel+ecorated%true(; $$$$JFrame$)rame$*$new JFrame%<J"Dec8Box$Test<(; $$$$)rame.setLayout%new FlowLayout%((; $$$$)rame.set+e)ault"lose,-eration%JFrame.E.IT/,0/"L,SE(; $$$$J"Dec8Box$ac$*$new J"Dec8Box%<AJ"<(; $$$$ac.setSelected%true(; $$$$J"Dec8Box$cdPlayer$*$new J"Dec8Box%<A<(; $$$$J"Dec8Box$cruise"ontrol$*$new J"Dec8Box%<B<(; $$$$J"Dec8Box$8eylessEntry$*$new J"Dec8Box%<"<(; $$$$J"Dec8Box$antiTDe)t$*$new J"Dec8Box%<+<(; $$$$J"Dec8Box$centralLoc8$*$new J"Dec8Box%<E<(; $$$$)rame.add%new JLa6el%<"ar$Features<((; $$$$)rame.add%ac(; $$$$)rame.add%cdPlayer(; $$$$)rame.add%cruise"ontrol(; $$$$)rame.add%8eylessEntry(; $$$$)rame.add%antiTDe)t(; $$$$)rame.add%centralLoc8(; $$$$)rame.-ac8%(; $$$$)rame.set5isi6le%true(; $$7 7

)) "#hec*&oxItem istener
import$java.awt."olor; import$java.awt.event.ItemEvent; import$java.awt.event.ItemListener; import$javax.swing.A6stractButton; import$javax.swing.J"Dec8Box; import$javax.swing.JFrame; public$class$"#hec*&oxItem istener$#

$$public$static$void$main%String$args&'($# $$$$JFrame$)rame$*$new$JFrame%<Iconi1ing$"Dec8Box<(; $$$$)rame.set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$J"Dec8Box$a"Dec8BoxE$*$new$J"Dec8Box%<Stu))ed$"rust<(; $$$$ItemListener$itemListener$*$new$ItemListener%($# $$$$$$public$void$itemState"Danged%ItemEvent$itemEvent($# $$$$$$$$A6stractButton$a6stractButton$*$ %A6stractButton(itemEvent.getSource%(; $$$$$$$$"olor$)oreground$*$a6stractButton.getForeground%(; $$$$$$$$"olor$6ac8ground$*$a6stractButton.getBac8ground%(; $$$$$$$$int$state$*$itemEvent.getState"Dange%(; $$$$$$$$i($%state$**$ItemEvent.SELECTED($# $$$$$$$$$$a6stractButton.setForeground%6ac8ground(; $$$$$$$$$$a6stractButton.setBac8ground%)oreground(; $$$$$$$$7 $$$$$$7 $$$$7; $$$$a"Dec8BoxE.addItemListener%itemListener(; $$$$)rame.add%a"Dec8BoxE(; $$$$)rame.setSi1e%A334$@33(; $$$$)rame.set5isi6le%true(; $$7 7

//COMBO BOX:
import import import import java.awt.event.ActionEvent; java.awt.event.ActionListener; java.awt.event.9indowAda-ter; java.awt.event.9indowEvent;

import javax.swing.J"om6oBox; import javax.swing.JFrame; public class !ain$extends JFrame$# $$J"om6oBox$com6o$*$new J"om6oBox%(; $$public !ain%($# $$$$set+e)ault"lose,-eration%JFrame.E.IT/,0/"L,SE(; $$$$com6o.addItem%<A<(; $$$$com6o.addItem%<C<(; $$$$com6o.addItem%<P<(; $$$$com6o.setEdita6le%true(; $$$$System.out.-rintln%<Kitems*<$>$com6o.getItem"ount%((; $$$$com6o.addActionListener%new ActionListener%($# $$$$$$public void actionPer)ormed%ActionEvent$e($# $$$$$$$$System.out.-rintln%<Selected$index*<$>$com6o.getSelectedIndex%( $$$$$$$$$$$$>$<$Selected$item*<$>$com6o.getSelectedItem%((; $$$$$$7 $$$$7(;

$$$$get"ontentPane%(.add%com6o(; $$$$-ac8%(; $$$$set5isi6le%true(; $$7 $$public static void main%String$arg&'($# $$$$new !ain%(; $$7 7

#reating J,a"io$utton #omponents


1. ! J,a"io$utton represents a ra"io button. -. .ou can use multiple J,a"io$uttons to represents a selection from which only one item can /. To in"icate a logical grouping of ra"io buttons, you use a javax.swing.$utton0roup object. 1. To programmatically select a J,a"io$utton, you pass true to its set)electe" metho".
public J:adioButton%( J:adioButton$a:adioButton$*$new J:adioButton%(; public J:adioButton%Icon$icon( J:adioButton$a:adioButton$*$new J:adioButton%new +iamondIcon%"olor."FA04$(alse((; a:adioButton.setSelectedIcon%new +iamondIcon%"olor.BL E4$true((;

public J:adioButton%Icon$icon4$boolean selected( J:adioButton$a:adioButton$*$new J:adioButton%new +iamondIcon%"olor."FA04$(alse(4$$true( a:adioButton.setSelectedIcon%new +iamondIcon%"olor.BL E4$true((; public J:adioButton%String$text( J:adioButton$a:adioButton$*$new J:adioButton%<E$slices<(; public J:adioButton%String$text4$boolean selected( J:adioButton$a:adioButton$*$new J:adioButton%<L$slices<4$true(;

public J:adioButton%String$text4$Icon$icon( J:adioButton$a:adioButton$*$new J:adioButton%<?@$slices<4$$new +iamondIcon%"olor."FA04$ a:adioButton.setSelectedIcon%new +iamondIcon%"olor.BL E4$true((;

public J:adioButton%String$text4$Icon$icon4$boolean selected( J:adioButton$a:adioButton$*$new J:adioButton%<?B$slices<4$$new +iamondIcon%"olor."FA04$ a:adioButton.setSelectedIcon%new +iamondIcon%"olor.BL E4$true((; public J:adioButton%Action$action( Action$action$*$...; J:adioButton$a:adioButton$*$new J:adioButton%action(;

import$java.awt.FlowLayout; import$javax.swing.Button;rou-;

import$javax.swing.JFrame; import$javax.swing.JLa6el; import$javax.swing.J:adioButton; public$class$+ain # $$public$static$void$main%String&'$args($# $$$$JFrame.setDefaultLookAndFeelDecorated%true(; $$$$JFrame$)rame$*$new$JFrame%<J:adioButton$Test<(; $$$$)rame.setLayout%new$FlowLayout%((; $$$$)rame.set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$J:adioButton$6utton?$*$new$J:adioButton%<:ed<(; $$$$J:adioButton$6utton@$*$new$J:adioButton%<;reen<(; $$$$J:adioButton$6uttonA$*$new$J:adioButton%<Blue<(; $$$$Button;rou-$colorButton;rou-$*$new$Button;rou-%(; $$$$colorButton;rou-.add%6utton?(; $$$$colorButton;rou-.add%6utton@(; $$$$colorButton;rou-.add%6uttonA(; $$$$6utton?.setSelected%true(; $$$$)rame.add%new$JLa6el%<"olorG<((; $$$$)rame.add%6utton?(; $$$$)rame.add%6utton@(; $$$$)rame.add%6uttonA(; $$$$)rame.-ac8%(; $$$$)rame.set5isi6le%true(; $$7 7

Listening to J,a"io$utton 2vents with an !ctionListener

import java.awt.;ridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import import import import import javax.swing.A6stractButton; javax.swing.Button;rou-; javax.swing.JFrame; javax.swing.JPanel; javax.swing.J:adioButton;

public class J:adioButtonActionListener$# $$public static void main%String$args&'($# $$$$JFrame$)rame$*$new JFrame%<;rou-ing$Exam-le<(; $$$$)rame.set+e)ault"lose,-eration%JFrame.E.IT/,0/"L,SE(; $$$$JPanel$-anel$*$new JPanel%new ;ridLayout%34$?((; $$$$Button;rou-$grou-$*$new Button;rou-%(; $$$$J:adioButton$a:adioButton$*$new J:adioButton%<A<(; $$$$J:adioButton$6:adioButton$*$new J:adioButton%<B<(; $$$$ActionListener$sliceActionListener$*$new ActionListener%($# $$$$$$public void actionPer)ormed%ActionEvent$actionEvent($# $$$$$$$$A6stractButton$aButton$*$%A6stractButton($actionEvent.getSource%(; $$$$$$$$System.out.-rintln%<SelectedG$<$>$aButton.getText%((; $$$$$$7 $$$$7; $$$$-anel.add%a:adioButton(; $$$$grou-.add%a:adioButton(; $$$$-anel.add%6:adioButton(; $$$$grou-.add%6:adioButton(; $$$$a:adioButton.addActionListener%sliceActionListener(; $$$$6:adioButton.addActionListener%sliceActionListener(; $$$$)rame.add%-anel(; $$$$)rame.setSi1e%A334$@33(; $$$$)rame.set5isi6le%true(; $$7 7

JList

The JList component is the basic )wing component for selecting one or more items from a set of ch *ey elements an" their implementations "efine the JList structure& 1. ! "ata mo"el for hol"ing the JList "ata, as "efine" by the List o"el interface -. ! cell ren"erer for "rawing the elements of the JList, as "escribe" by the List#ell,en"erer in
public JList%( JList$jlist$*$new JList%(; public JList%,6ject$list+ata&'( String$la6els&'$*$#$<A<4$<B<4$<"<4$<+<4$<E<4$<F<4$<;<4$<C<7; JList$jlist$*$new JList%la6els(; public JList%5ector$list+ata( 5ector$vector$*$aBu))eredImage.getSources%(; JList$jlist$*$new JList%vector(;

/. ! selection mo"el for selecting elements of the JList, as "escribe" by the List)election o"el

public JList%List!odel$model( :esultSet$results$*$aJ+B"Statement.executeMuery%<SELE"T$col0ame$F:,!$ta 6le0ame<(; +e)aultList!odel$model$*$new +e)aultList!odel%(; while %result.next%((# $$model.addElement%result.getString%?((; 7$$ JList$jlist$*$new JList%model(;

//JlistTEst
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; public class JListTest$# $$public static void main%String&'$args($# $$$$JFrame.set+e)aultLoo8AndFeel+ecorated%true(; $$$$JFrame$)rame$*$new JFrame%<JList$Test<(; $$$$)rame.setLayout%new FlowLayout%((; $$$$)rame.set+e)ault"lose,-eration%JFrame.E.IT/,0/"L,SE(; $$$$String&'$selections$*$#$<green<4$<red<4$<orange<4$<dar8$6lue<$7; $$$$JList$list$*$new JList%selections(; $$$$list.setSelectedIndex%?(; $$$$System.out.-rintln%list.getSelected5alue%((; $$$$)rame.add%new JScrollPane%list((; $$$$)rame.-ac8%(; $$$$)rame.set5isi6le%true(; $$7 7

The J enu component is the basic menu item container that is pla J enu$ar

1. In various places within the "ifferent menus, menu separators "ivi"e the options into logical s -. 2ach of the menu options has a mnemonic associate" with it to help with *eyboar" navigatio selection. /. The mnemonic allows users to ma*e menu selections via the *eyboar", for instance, by pres a 4in"ows platform to open the 3ile menu. 1. ! *eystro*e associate" with several options acts as a *eyboar" accelerator. +nli*e the mnem accelerator can "irectly activate a menu option, even when the menu option isn%t visible. 5. The 6ptions submenu has an icon associate" with it.
J!enu$)ile!enu$*$new J!enu%<File<(;

J!enuItem$new!enuItem$*$new J!enuItem%<0ew<(; )ile!enu.add%new!enuItem(; J!enuItem$o-en!enuItem$*$new J!enuItem%<,-en<(; )ile!enu.add%o-en!enuItem(; J!enuItem$close!enuItem$*$new J!enuItem%<"lose<(; )ile!enu.add%close!enuItem(; )ile!enu.addSe-arator%(; J!enuItem$save!enuItem$*$new J!enuItem%<Save<(; )ile!enu.add%save!enuItem(; )ile!enu.addSe-arator%(; J!enuItem$exit!enuItem$*$new J!enuItem%<Exit<(; )ile!enu.add%exit!enuItem(;

Insert them at specific positions or insert a separator at a specific position


public J!enuItem$insert%J!enuItem$menuItem4$int -os(; public J!enuItem$insert%Action$a4$int -os(; public void insertSe-arator%int -os(;

!""ing a

enu to a 4in"ow

import javax.swing.JFrame; import javax.swing.J!enu; import javax.swing.J!enuBar; public class Add!enuBarToFrame$extends JFrame$# $$private J!enuBar$menuBar$*$new J!enuBar%(;$JJ$9indow$menu$6ar $$public Add!enuBarToFrame%String$title($# $$$$setTitle%title(;

$$$$set+e)ault"lose,-eration%E.IT/,0/"L,SE(; $$$$setJ!enuBar%menuBar(;$JJ$Add$tDe$menu$6ar$to$tDe$window $$$$J!enu$)ile!enu$*$new J!enu%<File<(;$JJ$"reate$File$menu $$$$J!enu$element!enu$*$new J!enu%<Elements<(;$JJ$"reate$Elements$menu $$$$menuBar.add%)ile!enu(;$JJ$Add$tDe$)ile$menu $$$$menuBar.add%element!enu(;$JJ$Add$tDe$element$menu $$7 $$public static void main%String&'$args($# $$$$Add!enuBarToFrame$window$*$new Add!enuBarToFrame%<S8etcDer<(;$ $$$$window.setBounds%A34$A34$A334$A33(; $$$$window.set5isi6le%true(; $$7 7

/import$java.awt.event.IeyEvent;
import$javax.swing.JFrame; import$javax.swing.J!enu; import$javax.swing.J!enuBar; import$javax.swing.J!enuItem; import$javax.swing.event."DangeEvent; import$javax.swing.event."DangeListener; public$class$"ontruct!enu9itDAction$# $$public$static$void$main%(inal$String$args&'($# $$$$JFrame$)rame$*$new$JFrame%<!enuSam-le$Exam-le<(; $$$$)rame.set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$J!enuBar$menuBar$*$new$J!enuBar%(; $$$$JJ$File$!enu4$F$N$!nemonic $$$$J!enu$)ile!enu$*$new$J!enu%<File<(; $$$$menuBar.add%)ile!enu(; $$$$J!enu$save!enu$*$new$J!enu%<SA5E<(; $$$$menuBar.add%save!enu(; $$$$)ile!enu.add"DangeListener%new$"DangeListener%($# $$$$$$public$void$state"Danged%"DangeEvent$e($# $$$$$$$$System.out.-rintln%<File$!enu$"Danged<(; $$$$$$7 $$$$7(; $$$$save!enu.add"DangeListener%new$"DangeListener%($# $$$$$$$$public$void$state"Danged%"DangeEvent$e($# $$$$$$$$$$System.out.-rintln%<Save!enu$"Danged<(; $$$$$$$$7 $$$$$$7(; $$$$JJ$FileNO0ew4$0$N$!nemonic $$$$J!enuItem$new!enuItem$*$new$J!enuItem%<0ew<(; $$$$)ile!enu.add%new!enuItem(; $$$$new!enuItem.add"DangeListener%new$"DangeListener%($#

$$$$$$public$void$state"Danged%"DangeEvent$e($# $$$$$$$$System.out.-rintln%<new$menu$item$cDanged<(; $$$$$$7 $$$$7(; $$$$)rame.setJ!enuBar%menuBar(; $$$$)rame.setSi1e%A234$@23(; $$$$)rame.set5isi6le%true(; $$7 7

+sing enuListener to listen to& menu cancele", selecte" an" "es events

4ith a registere" enuListener, you%re notifie" when a J enu is selecte" before the pop'up menu i with the menu%s choices.
public inter(ace !enuListener$extends EventListener$# $$public void menu"anceled%!enuEvent$e(; $$public void menu+eselected%!enuEvent$e(; $$public void menuSelected%!enuEvent$e(; 7

import java.awt.event.IeyEvent; import import import import import import javax.swing.JFrame; javax.swing.J!enu; javax.swing.J!enuBar; javax.swing.J!enuItem; javax.swing.event.!enuEvent; javax.swing.event.!enuListener;

public class "ontruct!enu9itDAction$# $$public static void main%(inal String$args&'($#

$$$$JFrame$)rame$*$new JFrame%<!enuSam-le$Exam-le<(; $$$$)rame.set+e)ault"lose,-eration%JFrame.E.IT/,0/"L,SE(; $$$$J!enuBar$menuBar$*$new J!enuBar%(; $$$$JJ$File$!enu4$F$N$!nemonic $$$$J!enu$)ile!enu$*$new J!enu%<File<(; $$$$)ile!enu.set!nemonic%IeyEvent.5I/F(; $$$$menuBar.add%)ile!enu(; $$$$)ile!enu.add!enuListener%new !enuListener%($# $$$$$$public void menuSelected%!enuEvent$e($# $$$$$$$$System.out.-rintln%<menuSelected<(; $$$$$$7 $$$$$$public void menu+eselected%!enuEvent$e($# $$$$$$$$System.out.-rintln%<menu+eselected<(; $$$$$$7 $$$$$$public void menu"anceled%!enuEvent$e($# $$$$$$$$System.out.-rintln%<menu"anceled<(; $$$$$$7 $$$$7(; $$$$JJ$FileNO0ew4$0$N$!nemonic $$$$J!enuItem$new!enuItem$*$new J!enuItem%<0ew<(; $$$$)ile!enu.add%new!enuItem(; $$$$)rame.setJ!enuBar%menuBar(; $$$$)rame.setSi1e%A234$@23(; $$$$)rame.set5isi6le%true(; $$7 7

///SCROLL BAR

import$java.awt.BorderLayout; import$javax.swing.Bounded:ange!odel; import$javax.swing.JFrame; import$javax.swing.JScrollBar; import$javax.swing.event."DangeEvent; import$javax.swing.event."DangeListener; class$Bounded"DangeListener$implements$"DangeListener$# $$public$void$state"Danged%"DangeEvent$cDangeEvent($# $$$$,6ject$source$*$cDangeEvent.getSource%(; $$$$i($%source$instanceo($Bounded:ange!odel($# $$$$$$Bounded:ange!odel$a!odel$*$%Bounded:ange!odel($source;

$$$$$$i($%Pa!odel.get5alueIsAdjusting%(($# $$$$$$$$System.out.-rintln%<"DangedG$<$>$a!odel.get5alue%((; $$$$$$7 $$$$7$else$# $$$$$$System.out.-rintln%<SometDing$cDangedG$<$>$source(; $$$$7 $$7 7 public$class$ScrollBarSam-le$# $$public$static$void$main%String$args&'($# $$$$"DangeListener$cDangeListener$*$new$Bounded"DangeListener%(; $$$$JScrollBar$aJScrollBar$*$new$JScrollBar%JScrollBar.#O!I$ONTAL(; $$$$Bounded:ange!odel$model$*$aJScrollBar.get!odel%(; $$$$model.add"DangeListener%cDangeListener(; $$$$JFrame$)rame$*$new$JFrame%<ScrollBars$:$ s<(; $$$$)rame.set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$)rame.add%aJScrollBar4$BorderLayout.NO!T#(; $$$$)rame.setSi1e%A334$@33(; $$$$)rame.set5isi6le%true(; $$7 7

public JProgressBar%( JProgressBar$aJProgressBar$*$new JProgressBar%(; public JProgressBar%int orientation( JProgressBar$aJProgressBar$*$new JProgressBar%JProgressBar.5E:TI"AL(; JProgressBar$6JProgressBar$*$new JProgressBar%JProgressBar.C,:IQ,0TAL(; public JProgressBar%int minimum4$int maximum( JProgressBar$aJProgressBar$*$new JProgressBar%34$233(; public JProgressBar%int orientation4$int minimum4$int maximum( JProgressBar$aJProgressBar$*$new JProgressBar%JProgressBar.5E:TI"AL4$34$ ?333(; public JProgressBar%Bounded:ange!odel$model( JJ$+ata$model4$initial$value$34$range$3N@234$and$extent$o)$3 +e)aultBounded:ange!odel$model$*$new +e)aultBounded:ange!odel%34$34$34$ @23(; JProgressBar$aJProgressBar$*$new JProgressBar%model(;

import$java.awt.BorderLayout; import$java.awt.event.ActionEvent; import$java.awt.event.ActionListener;

import$javax.swing.JButton; import$javax.swing.JFrame; import$javax.swing.JProgressBar; public$class$JProgressBarSet5alue$extends$JFrame$# $$JProgressBar$6ar$*$new$JProgressBar%(; $$JButton$ste-$*$new$JButton%<Ste-<(; $$public$JProgressBarSet5alue%($# $$$$set+e)ault"lose,-eration%JFrame.EXIT_ON_CLOSE(; $$$$ste-.addActionListener%new$ActionListener%($# $$$$$$public$void$actionPer)ormed%ActionEvent$e($# $$$$$$$$int$value$*$6ar.get5alue%($>$H; $$$$$$$$i($%value$O$6ar.get!aximum%(($# $$$$$$$$$$value$*$6ar.get!aximum%(; $$$$$$$$7 $$$$$$$$6ar.set5alue%value(; $$$$$$7 $$$$7(; $$$$get"ontentPane%(.add%6ar4$BorderLayout.NO!T#(; $$$$get"ontentPane%(.add%ste-4$BorderLayout.EAST(; $$$$-ac8%(; $$$$set5isi6le%true(; $$7 $$public$static$void$main%String$arg&'($# $$$$new$JProgressBarSet5alue%(; $$7 7

1.

// COLOR CHOOSER
import import import import java.awt.BorderLayout; java.awt."olor; java.awt.event.ActionEvent; java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.J"olor"Dooser; import javax.swing.JFrame; public class "olorSam-le$# $$public static void main%String$args&'($# $$$$JFrame$)$*$new JFrame%<J"olor"Dooser$Sam-le<(; $$$$).set+e)ault"lose,-eration%JFrame.E.IT/,0/"L,SE(; $$$$(inal JButton$6utton$*$new JButton%<Pic8$to$"Dange$Bac8ground<(; $$$$ActionListener$actionListener$*$new ActionListener%($#

$$$$$$public void actionPer)ormed%ActionEvent$actionEvent($# $$$$$$$$"olor$initialBac8ground$*$6utton.getBac8ground%(; $$$$$$$$"olor$6ac8ground$*$J"olor"Dooser.sDow+ialog%null4$<J"olor"Dooser$ Sam-le<4$initialBac8ground(; $$$$$$$$i( %6ac8ground$P*$null($# $$$$$$$$$$6utton.setBac8ground%6ac8ground(; $$$$$$$$7 $$$$$$7 $$$$7; $$$$6utton.addActionListener%actionListener(; $$$$).add%6utton4$BorderLayout."E0TE:(; $$$$).setSi1e%A334$@33(; $$$$).set5isi6le%true(; $$7 7

J3ile#hooser
! J3ile#hooser is a "ialog to select a file or files. The return value of the three metho"s is one of the following& 1. J3ile#hooser.#!7#2L86(TI67, if the user clic*s #ancel. -. J3ile#hooser.!((,69286(TI67, if the user clic* an 6:;6pen;)ave button. /. J3ile#hooser.2,,6,86(TI67, if the user closes the "ialog ! return value of J3ile#hooser.!((,69286(TI67, in"icates that you can call its get)electe"3ile get)electe"3iles metho"s&
public java.io.File$getSelectedFile$%( public java.io.File&'$getSelectedFiles$%(

J3ile#hooser has supporting classes& 3ile3ilter class, 3ile)ystem9iew class, 3ile9iew. 3ile3ilter class is for restricting files an" "irectories to be liste" in the 3ile9iew of the J3ile#hooser. controls how the "irectories an" files are liste" within the J3ile#hooser. The 3ile)ystem9iew is an a that tries to hi"e file system'relate" operating system specifics from the file chooser.
import javax.swing.JFile"Dooser; import javax.swing.JFrame; public class !ain"lass$extends JFrame$# $$public !ain"lass%($# $$$$JFile"Dooser$)ile"Dooser$*$new JFile"Dooser%(; $$$$)ile"Dooser.set+ialogTitle%<"Doose$a$)ile<(; $$$$this.get"ontentPane%(.add%)ile"Dooser(; $$$$)ile"Dooser.set5isi6le%true(; $$7 $$public static void main%String&'$args($# $$$$JFrame$)rame$*$new !ain"lass%(; $$$$)rame.set+e)ault"lose,-eration%JFrame.E.IT/,0/"L,SE(;

$$$$)rame.-ac8%(; $$$$)rame.set5isi6le%true(; $$7 7

Das könnte Ihnen auch gefallen