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 javax.swing.*;
8 import java.awt.event.*;
9
10 import java.awt.*;
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(Color.black);
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 // ButtonTest.java
4 // Creating JButtons.
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
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 c.setLayout( new FlowLayout() );
18
19 // create buttons
20 plainButton = new JButton( "Plain Button" );
21 c.add( plainButton );
22
23 Icon bug1 = new ImageIcon( "bug1.gif" );
24 Icon bug2 = new ImageIcon( "bug2.gif" );
25 fancyButton = new JButton( "Fancy Button", bug1 );
26 fancyButton.setRolloverIcon( bug2 );
27 c.add( fancyButton );
28
29 // create an instance of inner class ButtonHandler
30 // to use for button event handling
31 ButtonHandler handler = new ButtonHandler();
32 fancyButton.addActionListener( handler );
33 plainButton.addActionListener( 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 app.addWindowListener(
44 new WindowAdapter() {
45 public void windowClosing( WindowEvent e )
46 {
47 System.exit( 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 JOptionPane.showMessageDialog( null,
6 "You pressed: " + e.getActionCommand() );
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 // FlowLayout1.java
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.*;
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 this.setLayout(new FlowLayout(FlowLayout.LEFT));
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 f.setForeground(Color.black);
42 f.setBackground(Color.lightGray);
43 f.getContentPane().add(panel, "Center");
44
45 f.setSize(400,100);
46 f.setVisible(true);
47 f.addWindowListener(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 = e.getWindow();
7 win.setVisible(false);
8 win.dispose();
9 System.exit(0);
10 }//windowClosing
11 }//class WindowCloser
12
13
14
15
16
17
Java for GUI Programming P. 6
1 Using checkbox control
2
3 // CheckBoxTest.java
4 // Creating Checkbox buttons.
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
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 c.setLayout(new FlowLayout());
19
20 t = new JTextField( "Watch the font style change", 20 );
21 t.setFont( new Font( "TimesRoman", Font.PLAIN, 14 ) );
22 c.add( t );
23
24 // create checkbox objects
25 bold = new JCheckBox( "Bold" );
26 c.add( bold );
27
28 italic = new JCheckBox( "Italic" );
29 c.add( italic );
30
31 CheckBoxHandler handler = new CheckBoxHandler();
32 bold.addItemListener( handler );
33 italic.addItemListener( 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 app.addWindowListener(
44 new WindowAdapter() {
45 public void windowClosing( WindowEvent e )
46 {
47 System.exit( 0 );
48 }
49 }
50 );
51 }
52
Java for GUI Programming P. 7
1 private class CheckBoxHandler implements ItemListener {
2 private int valBold = Font.PLAIN;
3 private int valItalic = Font.PLAIN;
4
5 public void itemStateChanged( ItemEvent e )
6 {
7 if ( e.getSource() == bold )
8 if ( e.getStateChange() == ItemEvent.SELECTED )
9 valBold = Font.BOLD;
10 else
11 valBold = Font.PLAIN;
12
13 if ( e.getSource() == italic )
14 if ( e.getStateChange() == ItemEvent.SELECTED )
15 valItalic = Font.ITALIC;
16 else
17 valItalic = Font.PLAIN;
18
19 t.setFont(
20 new Font( "TimesRoman", valBold + valItalic, 14 ) );
21 t.repaint();
22 }
23 }
24 }
25
26
27
28
29
30
31
32
Java for GUI Programming P. 8
1 Using radio button control
2
3 // RadioButtonTest.java
4 // Creating radio buttons using ButtonGroup and JRadioButton.
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
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 c.setLayout( new FlowLayout() );
22
23 t = new JTextField( "Watch the font style change", 25 );
24 c.add( t );
25
26 // Create radio buttons
27 plain = new JRadioButton( "Plain", true );
28 c.add( plain );
29 bold = new JRadioButton( "Bold", false);
30 c.add( bold );
31 italic = new JRadioButton( "Italic", false );
32 c.add( italic );
33 boldItalic = new JRadioButton( "Bold/Italic", false );
34 c.add( boldItalic );
35
36 // register events
37 RadioButtonHandler handler = new RadioButtonHandler();
38 plain.addItemListener( handler );
39 bold.addItemListener( handler );
40 italic.addItemListener( handler );
41 boldItalic.addItemListener( handler );
42
43 // create logical relationship between JRadioButtons
44 radioGroup = new ButtonGroup();
45 radioGroup.add( plain );
46 radioGroup.add( bold );
47 radioGroup.add( italic );
48 radioGroup.add( boldItalic );
49
50 plainFont = new Font( "TimesRoman", Font.PLAIN, 14 );
51 boldFont = new Font( "TimesRoman", Font.BOLD, 14 );
52 italicFont = new Font( "TimesRoman", Font.ITALIC, 14 );
Java for GUI Programming P. 9
1 boldItalicFont =
2 new Font( "TimesRoman", Font.BOLD + Font.ITALIC, 14 );
3 t.setFont( 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 app.addWindowListener(
14 new WindowAdapter() {
15 public void windowClosing( WindowEvent e )
16 {
17 System.exit( 0 );
18 }
19 }
20 );
21 }
22
23 private class RadioButtonHandler implements ItemListener {
24 public void itemStateChanged( ItemEvent e )
25 {
26 if ( e.getSource() == plain )
27 t.setFont( plainFont );
28 else if ( e.getSource() == bold )
29 t.setFont( boldFont );
30 else if ( e.getSource() == italic )
31 t.setFont( italicFont );
32 else if ( e.getSource() == boldItalic )
33 t.setFont( boldItalicFont );
34
35 t.repaint();
36 }
37 }
38 }
39
40
Java for GUI Programming P. 10