Java GUI Programming_GUI Example
Java GUI Programming_GUI Example
3
4
5
30
31
32
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
11
12
13
14
15
16
17
18
19
20
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
13
14
15
16
17
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
25
26
27
28
29
30
31
32
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 );
39
40