1 Java for GUI Programming
3
4
5
Java for GUI Programming P. 1
1 Getting started
2 – a one-button applet
3
4
5
6 // OneButton_Applet.java
7 import [Link].*;
8 import [Link].*;
9
10 import [Link].*;
11
12 public class OneButton_Applet extends JApplet
13 {
14 public void init()
15 {
16 JButton button1 = new JButton("Welcome to GUI");
17
18 // setBackground([Link]);
19 getContentPane().add(button1,"Center");
20 }//init()
21
22
23 }//OneButton_Applet
24
25
26
27
28
29
30
31
32
Java for GUI Programming P. 2
1 More buttons with event handling
2
3 // [Link]
4 // Creating JButtons.
5 import [Link].*;
6 import [Link].*;
7 import [Link].*;
8
9 public class ButtonTest extends JFrame {
10 private JButton plainButton, fancyButton;
11
12 public ButtonTest()
13 {
14 super( "Testing Buttons" );
15
16 Container c = getContentPane();
17 [Link]( new FlowLayout() );
18
19 // create buttons
20 plainButton = new JButton( "Plain Button" );
21 [Link]( plainButton );
22
23 Icon bug1 = new ImageIcon( "[Link]" );
24 Icon bug2 = new ImageIcon( "[Link]" );
25 fancyButton = new JButton( "Fancy Button", bug1 );
26 [Link]( bug2 );
27 [Link]( fancyButton );
28
29 // create an instance of inner class ButtonHandler
30 // to use for button event handling
31 ButtonHandler handler = new ButtonHandler();
32 [Link]( handler );
33 [Link]( handler );
34
35 setSize( 275, 100 );
36 show();
37 }
38
39 public static void main( String args[] )
40 {
41 ButtonTest app = new ButtonTest();
42
43 [Link](
44 new WindowAdapter() {
45 public void windowClosing( WindowEvent e )
46 {
47 [Link]( 0 );
48 }
49 }
50 );
51 }
52
Java for GUI Programming P. 3
1 // inner class for button event handling
2 private class ButtonHandler implements ActionListener {
3 public void actionPerformed( ActionEvent e )
4 {
5 [Link]( null,
6 "You pressed: " + [Link]() );
7 }
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
Java for GUI Programming P. 4
1 Using layout manager
2
3 // [Link]
4 import [Link].*;
5 import [Link].*;
6 import [Link].*;
7
8
9 public class FlowLayout1 extends JPanel
10 {
11 public FlowLayout1()
12 {
13 JButton btnOne;
14 JButton btnTwo;
15 JButton btnThree;
16 JButton btnFour;
17
18 [Link](new FlowLayout([Link]));
19
20 btnOne = new JButton("One");
21 add(btnOne);
22
23 btnTwo = new JButton("Two");
24 add(btnTwo);
25
26 btnThree = new JButton("Three");
27 add(btnThree);
28
29 btnFour = new JButton("Four");
30 add(btnFour);
31
32 }//constructor
33
34
35
36 public static void main(String s[])
37 {
38 JFrame f = new JFrame("Flow Layout");
39 FlowLayout1 panel = new FlowLayout1();
40
41 [Link]([Link]);
42 [Link]([Link]);
43 [Link]().add(panel, "Center");
44
45 [Link](400,100);
46 [Link](true);
47 [Link](new WindowCloser());
48
49
50 }//main
51 }//class
52
Java for GUI Programming P. 5
1
2 class WindowCloser extends WindowAdapter
3 {
4 public void windowClosing(WindowEvent e)
5 {
6 Window win = [Link]();
7 [Link](false);
8 [Link]();
9 [Link](0);
10 }//windowClosing
11 }//class WindowCloser
12
13
14
15
16
17
Java for GUI Programming P. 6
1 Using checkbox control
2
3 // [Link]
4 // Creating Checkbox buttons.
5 import [Link].*;
6 import [Link].*;
7 import [Link].*;
8
9 public class CheckBoxTest extends JFrame {
10 private JTextField t;
11 private JCheckBox bold, italic;
12
13 public CheckBoxTest()
14 {
15 super( "JCheckBox Test" );
16
17 Container c = getContentPane();
18 [Link](new FlowLayout());
19
20 t = new JTextField( "Watch the font style change", 20 );
21 [Link]( new Font( "TimesRoman", [Link], 14 ) );
22 [Link]( t );
23
24 // create checkbox objects
25 bold = new JCheckBox( "Bold" );
26 [Link]( bold );
27
28 italic = new JCheckBox( "Italic" );
29 [Link]( italic );
30
31 CheckBoxHandler handler = new CheckBoxHandler();
32 [Link]( handler );
33 [Link]( handler );
34
35 setSize( 275, 100 );
36 show();
37 }
38
39 public static void main( String args[] )
40 {
41 CheckBoxTest app = new CheckBoxTest();
42
43 [Link](
44 new WindowAdapter() {
45 public void windowClosing( WindowEvent e )
46 {
47 [Link]( 0 );
48 }
49 }
50 );
51 }
52
Java for GUI Programming P. 7
1 private class CheckBoxHandler implements ItemListener {
2 private int valBold = [Link];
3 private int valItalic = [Link];
4
5 public void itemStateChanged( ItemEvent e )
6 {
7 if ( [Link]() == bold )
8 if ( [Link]() == [Link] )
9 valBold = [Link];
10 else
11 valBold = [Link];
12
13 if ( [Link]() == italic )
14 if ( [Link]() == [Link] )
15 valItalic = [Link];
16 else
17 valItalic = [Link];
18
19 [Link](
20 new Font( "TimesRoman", valBold + valItalic, 14 ) );
21 [Link]();
22 }
23 }
24 }
25
26
27
28
29
30
31
32
Java for GUI Programming P. 8
1 Using radio button control
2
3 // [Link]
4 // Creating radio buttons using ButtonGroup and JRadioButton.
5 import [Link].*;
6 import [Link].*;
7 import [Link].*;
8
9 public class RadioButtonTest extends JFrame {
10 private JTextField t;
11 private Font plainFont, boldFont,
12 italicFont, boldItalicFont;
13 private JRadioButton plain, bold, italic, boldItalic;
14 private ButtonGroup radioGroup;
15
16 public RadioButtonTest()
17 {
18 super( "RadioButton Test" );
19
20 Container c = getContentPane();
21 [Link]( new FlowLayout() );
22
23 t = new JTextField( "Watch the font style change", 25 );
24 [Link]( t );
25
26 // Create radio buttons
27 plain = new JRadioButton( "Plain", true );
28 [Link]( plain );
29 bold = new JRadioButton( "Bold", false);
30 [Link]( bold );
31 italic = new JRadioButton( "Italic", false );
32 [Link]( italic );
33 boldItalic = new JRadioButton( "Bold/Italic", false );
34 [Link]( boldItalic );
35
36 // register events
37 RadioButtonHandler handler = new RadioButtonHandler();
38 [Link]( handler );
39 [Link]( handler );
40 [Link]( handler );
41 [Link]( handler );
42
43 // create logical relationship between JRadioButtons
44 radioGroup = new ButtonGroup();
45 [Link]( plain );
46 [Link]( bold );
47 [Link]( italic );
48 [Link]( boldItalic );
49
50 plainFont = new Font( "TimesRoman", [Link], 14 );
51 boldFont = new Font( "TimesRoman", [Link], 14 );
52 italicFont = new Font( "TimesRoman", [Link], 14 );
Java for GUI Programming P. 9
1 boldItalicFont =
2 new Font( "TimesRoman", [Link] + [Link], 14 );
3 [Link]( plainFont );
4
5 setSize( 300, 100 );
6 show();
7 }
8
9 public static void main( String args[] )
10 {
11 RadioButtonTest app = new RadioButtonTest();
12
13 [Link](
14 new WindowAdapter() {
15 public void windowClosing( WindowEvent e )
16 {
17 [Link]( 0 );
18 }
19 }
20 );
21 }
22
23 private class RadioButtonHandler implements ItemListener {
24 public void itemStateChanged( ItemEvent e )
25 {
26 if ( [Link]() == plain )
27 [Link]( plainFont );
28 else if ( [Link]() == bold )
29 [Link]( boldFont );
30 else if ( [Link]() == italic )
31 [Link]( italicFont );
32 else if ( [Link]() == boldItalic )
33 [Link]( boldItalicFont );
34
35 [Link]();
36 }
37 }
38 }
39
40
Java for GUI Programming P. 10