GUII - WordPress.com

skatechildrenΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

96 εμφανίσεις

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


SWING

JFrame use for window in swing components.

JPanel use for group of child component

Border use for applying swing component.


2

Make a
Container


For an application, your container is typically a
JFrame


JFrame frame = new JFrame();


JFrame frame = new JFrame("Text to put in title
bar");


You can create a
JFrame

in your “main class”


It’s often more convenient to have your “main
class”
extend

JFrame


For an applet, your “main class” must extend
JApplet


Once your application or applet is up and
running, it create and display various dialogs

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Border

This is type

Of border

4

Add a layout manager


The most important layout managers are:


BorderLayout


Provides five areas into which you can
put components


This is the default layout manager for
both
JFrame

and
JApplet


FlowLayout


Components are added left to right, top
to bottom


GridLayout


Components are put in a rectangular grid


All areas are the same size and shape


BoxLayout


Creates a horizontal row or a vertical
stack


This can be a little weird to use


GridBagLayout


Too complex and a danger to your sanity
-
-
avoid

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Layout Mananger

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example

7

Add components to
containers


The usual command is


container
.add(
component
);


For
FlowLayout
,
GridLayout
, and
BoxLayout
,
this adds the component to the next
available location


For
BorderLayout
, this puts the component
in the
CENTER

by default


For
BorderLayout
, it’s usually better
to use


container
.add(
component
,
BorderLayout.
position
);


position

is one of
NORTH
,
SOUTH
,
EAST
,
WEST
, or
CENTER


____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Control in swing

Here are control of swing

JLabel

JTextField

JTextArea

JButton

JCheckBox

JComboBox

JList

JMenu



9

Some types of components

JLabel

JButton

JButton

JCheckbox

JChoice

JList

JScrollbar

JTextField

JTextArea

JCheckboxGroup

JCheckbox

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JLabel


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LabelTest extends JFrame {


private JLabel label1, label2, label3;


public LabelTest()


{


super( "Testing JLabel" );


Container container = getContentPane();


container.setLayout( new FlowLayout() );


label1 = new JLabel( "Label with text" );


label1.setToolTipText( "This is label1" );container.add( label1 );


Icon bug = new ImageIcon( "flight.gif" );


label2 = new JLabel( "Label with text and icon", bug, SwingConstants.LEFT );


label2.setToolTipText( "This is label2" );container.add( label2 );


label3 = new JLabel();


label3.setText( "Label with icon and text at bottom" );


label3.setIcon( bug );


label3.setHorizontalTextPosition( SwingConstants.CENTER );


label3.setVerticalTextPosition( SwingConstants.BOTTOM );


label3.setToolTipText( "This is label3" );


container.add( label3 );setSize( 275, 170 );setVisible( true );


}


public static void main( String args[] )


{


LabelTest application = new LabelTest();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}


}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JTestField and JPasswordField

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TextFieldTest extends JFrame {


private JTextField textField1, textField2, textField3;


private JPasswordField passwordField;


public TextFieldTest()


{


super( "Testing JTextField and JPasswordField" );


Container container = getContentPane();


container.setLayout( new FlowLayout() );


textField1 = new JTextField( 10 );


container.add( textField1 );


textField2 = new JTextField( "Enter text here" );


container.add( textField2 );


textField3 = new JTextField( "Uneditable text field", 20 );


textField3.setEditable( false );


container.add( textField3 );


passwordField = new JPasswordField( "Hidden text" );


container.add( passwordField );


TextFieldHandler handler = new TextFieldHandler();


textField1.addActionListener( handler );


textField2.addActionListener( handler );


textField3.addActionListener( handler );


passwordField.addActionListener( handler );


setSize( 325, 100 );


setVisible( true );


}



____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example(cont)

public static void main( String args[] )


{


TextFieldTest application = new TextFieldTest();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

private class TextFieldHandler implements ActionListener {


public void actionPerformed( ActionEvent event )


{


String string = "";


if ( event.getSource() == textField1 )


string = "textField1: " + event.getActionCommand();


else if ( event.getSource() == textField2 )


string = "textField2: " + event.getActionCommand();


else if ( event.getSource() == textField3 )


string = "textField3: " + event.getActionCommand();



else if ( event.getSource() == passwordField ) {


string = "passwordField: " +


new String( passwordField.getPassword() );


}



JOptionPane.showMessageDialog( null, string );



}


}


}



____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JTextArea

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TextAreaDemo extends JFrame {


private JTextArea textArea1, textArea2;


private JButton copyButton;


public TextAreaDemo()


{


super( "TextArea Demo" );


Box box = Box.createHorizontalBox();


String string = "This is a demo string to
\
n" +


"illustrate copying text
\
nfrom one textarea to
\
n" +


"another textarea using an
\
nexternal event
\
n";


textArea1 = new JTextArea( string, 10, 15 );


box.add( new JScrollPane( textArea1 ) );


copyButton = new JButton( "Copy >>>" );


box.add( copyButton );


copyButton.addActionListener(


new ActionListener() {


public void actionPerformed( ActionEvent event )


{


textArea2.setText( textArea1.getSelectedText() );


}


} );

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JText(Cont)


textArea2 = new JTextArea( 10, 15 );


textArea2.setEditable( false );


box.add( new JScrollPane( textArea2 ) );


Container container = getContentPane();


container.add( box );


setSize( 425, 200 );


setVisible( true );


}


public static void main( String args[] )


{


TextAreaDemo application = new
TextAreaDemo();


application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );


}

}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JButton

Example:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ButtonTest extends JFrame {


private JButton plainButton, fancyButton;


public ButtonTest()


{


super( "Testing Buttons" );


Container container = getContentPane();


container.setLayout( new FlowLayout() );


plainButton = new JButton( "Plain Button" );


container.add( plainButton );


Icon bug1 = new ImageIcon( "ball.gif" );


Icon bug2 = new ImageIcon( "bug1.gif" ); fancyButton = new JButton( "Fancy Button", bug1 );


fancyButton.setRolloverIcon( bug2 ); container.add( fancyButton );


ButtonHandler handler = new ButtonHandler();


fancyButton.addActionListener( handler );


plainButton.addActionListener( handler );


setSize( 275, 100 );


setVisible( true );


}


public static void main( String args[] )


{


ButtonTest application = new ButtonTest();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

private class ButtonHandler implements ActionListener {


public void actionPerformed( ActionEvent event )


{


JOptionPane.showMessageDialog( ButtonTest.this,


"You pressed: " + event.getActionCommand() );


}

}

}


____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JCheckbox

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CheckBoxTest extends JFrame {


private JTextField field;


private JCheckBox bold, italic;


public CheckBoxTest()


{


super( "JCheckBox Test" );


Container container = getContentPane();


container.setLayout( new FlowLayout() );


field = new JTextField( "Watch the font style change", 20 );


field.setFont( new Font( "Serif", Font.PLAIN, 14 ) );


container.add( field );bold = new JCheckBox( "Bold" );


container.add( bold );


italic = new JCheckBox( "Italic" );container.add( italic );


CheckBoxHandler handler = new CheckBoxHandler();


bold.addItemListener( handler );


italic.addItemListener( handler );


setSize( 275, 100 );setVisible( true );


}

public static void main( String args[] )


{


CheckBoxTest application = new CheckBoxTest();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Checkbox(cont)

private class CheckBoxHandler implements ItemListener {


private int valBold = Font.PLAIN;


private int valItalic = Font.PLAIN;


public void itemStateChanged( ItemEvent event )


{


if ( event.getSource() == bold )


valBold = bold.isSelected() ? Font.BOLD : Font.PLAIN;


if ( event.getSource() == italic )


valItalic = italic.isSelected() ? Font.ITALIC : Font.PLAIN;


field.setFont( new Font( "Serif", valBold + valItalic, 14 ) );


}


}

}



____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012



JRadioButton

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class RadioButtonTest extends JFrame {


private JTextField field;


private Font plainFont, boldFont, italicFont, boldItalicFont;


private JRadioButton plainButton, boldButton, italicButton,


boldItalicButton;


private ButtonGroup radioGroup;


public RadioButtonTest()


{


super( "RadioButton Test" );


Container container = getContentPane();


container.setLayout( new FlowLayout() );


field = new JTextField( "Watch the font style change", 25 );


container.add( field );


plainButton = new JRadioButton( "Plain", true );


container.add( plainButton );


boldButton = new JRadioButton( "Bold", false );


container.add( boldButton );


italicButton = new JRadioButton( "Italic", false );


container.add( italicButton );


boldItalicButton = new JRadioButton( "Bold/Italic", false );


container.add( boldItalicButton );


radioGroup = new ButtonGroup(); radioGroup.add( plainButton );


radioGroup.add( boldButton );radioGroup.add( italicButton );


radioGroup.add( boldItalicButton );



____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example(cont)

plainFont = new Font( "Serif", Font.PLAIN, 14 );


boldFont = new Font( "Serif", Font.BOLD, 14 );


italicFont = new Font( "Serif", Font.ITALIC, 14 );


boldItalicFont = new Font( "Serif", Font.BOLD + Font.ITALIC, 14 );


field.setFont( plainFont );


plainButton.addItemListener( new RadioButtonHandler( plainFont ) );


boldButton.addItemListener( new RadioButtonHandler( boldFont ) );


italicButton.addItemListener(


new RadioButtonHandler( italicFont ) );


boldItalicButton.addItemListener(


new RadioButtonHandler( boldItalicFont ) );


setSize( 300, 100 );


setVisible( true );


}


public static void main( String args[] )


{


RadioButtonTest application = new RadioButtonTest();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}


private class RadioButtonHandler implements ItemListener {


private Font font;


public RadioButtonHandler( Font f ){


font = f;


}



public void itemStateChanged( ItemEvent event ){


field.setFont( font );


}


}

}


____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JComboBox


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ComboBoxTest extends JFrame {


private JComboBox imagesComboBox;


private JLabel label;


private String names[] =


{ "bug1.gif", "bug2.gif", "travelbug.gif", "buganim.gif" };


private Icon icons[] = { new ImageIcon( names[ 0 ] ),


new ImageIcon( names[ 1 ] ), new ImageIcon( names[ 2 ] ),


new ImageIcon( names[ 3 ] ) };


public ComboBoxTest()


{


super( "Testing JComboBox" );


Container container = getContentPane();


container.setLayout( new FlowLayout() );


imagesComboBox = new JComboBox( names );


imagesComboBox.setMaximumRowCount( 3 );


imagesComboBox.addItemListener(new ItemListener() {


public void itemStateChanged( ItemEvent event )


{


if ( event.getStateChange() == ItemEvent.SELECTED )


label.setIcon( icons[


imagesComboBox.getSelectedIndex() ] );


}


} );



____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


ComboBox(cont)

container.add( imagesComboBox );


label = new JLabel( icons[ 0 ] );


container.add( label );


setSize( 350, 100 );


setVisible( true );


}


public static void main( String args[] )


{


ComboBoxTest application = new ComboBoxTest();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}


}




____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JList


import java.awt.*;

import javax.swing.*;

import javax.swing.event.*;

public class ListTest extends JFrame {


private JList colorList;


private Container container;


private final String colorNames[] = { "Black", "Blue", "Cyan",


"Dark Gray", "Gray", "Green", "Light Gray", "Magenta",


"Orange", "Pink", "Red", "White", "Yellow" };


private final Color colors[] = { Color.BLACK, Color.BLUE, Color.CYAN,


Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,


Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE,


Color.YELLOW };


public ListTest()


{


super( "List Test" );


container = getContentPane();


container.setLayout( new FlowLayout() );


colorList = new JList( colorNames );


colorList.setVisibleRowCount( 5 );


colorList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );


container.add( new JScrollPane( colorList ) );


colorList.addListSelectionListener(new ListSelectionListener() {


public void valueChanged( ListSelectionEvent event )


{


container.setBackground(


colors[ colorList.getSelectedIndex() ] );


}


});


setSize( 350, 150 );


setVisible( true );


}


public static void main( String args[] )


{


ListTest application = new ListTest();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );



}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example(Multilist)

import javax.swing.*;

public class MultipleSelectionTest extends JFrame {


private JList colorList, copyList;


private JButton copyButton;


private final String colorNames[] = { "Black", "Blue", "Cyan",


"Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange",


"Pink", "Red", "White", "Yellow" };


public MultipleSelectionTest()


{


super( "Multiple Selection Lists" );


Container container = getContentPane();


container.setLayout( new FlowLayout() );


colorList = new JList( colorNames );


colorList.setVisibleRowCount( 5 );


colorList.setSelectionMode(


ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );


container.add( new JScrollPane( colorList ) );


copyButton = new JButton( "Copy >>>" );


copyButton.addActionListener(


new ActionListener() {


public void actionPerformed( ActionEvent event )


{


copyList.setListData( colorList.getSelectedValues() );


}


} );

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Multi(cont)

container.add( copyButton );


copyList = new JList( );


copyList.setVisibleRowCount( 5 );


copyList.setFixedCellWidth( 100 );


copyList.setFixedCellHeight( 15 );


copyList.setSelectionMode(


ListSelectionModel.SINGLE_INTERVAL_SELECTION );


container.add( new JScrollPane( copyList ) );


setSize( 300, 130 );


setVisible( true );


}


public static void main( String args[] )


{


MultipleSelectionTest application = new
MultipleSelectionTest();


application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );


}

}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


MouseEvent

MouseListener

void mousePressed(MouseEvent )

void mouseClicked(MouseEvent )

void mouseReleased(MouseEvent)

void mouseEntered(MouseEvent)

void mouseExited(MouseEvent)

MouseMotionListener

void mouseDragged(MouseEvent )

void mouseMoved(MouseEvent )



____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example(mouse)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class MouseTracker extends JFrame


implements MouseListener, MouseMotionListener {


private JLabel statusBar;


public MouseTracker()


{


super( "Demonstrating Mouse Events" );


statusBar = new JLabel();


getContentPane().add( statusBar, BorderLayout.SOUTH );


addMouseListener( this );


addMouseMotionListener( this );


setSize( 275, 100 );


setVisible( true );


}


public void mouseClicked( MouseEvent event )


{


statusBar.setText( "Clicked at [" + event.getX() +


", " + event.getY() + "]" );


}


public void mousePressed( MouseEvent event )


{


statusBar.setText( "Pressed at [" + event.getX() +


", " + event.getY() + "]" );


}


public void mouseReleased( MouseEvent event )


{


statusBar.setText( "Released at [" + event.getX() +


", " + event.getY() + "]" );


}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Mouse(cont)

public void mouseEntered( MouseEvent event )


{


statusBar.setText( "Mouse entered at [" + event.getX() +


", " + event.getY() + "]" );


getContentPane().setBackground( Color.GREEN );


}


public void mouseExited( MouseEvent event )


{


statusBar.setText( "Mouse outside window" );


getContentPane().setBackground( Color.WHITE );


}


public void mouseDragged( MouseEvent event )


{


statusBar.setText( "Dragged at [" + event.getX() +


", " + event.getY() + "]" );


}


public void mouseMoved( MouseEvent event )


{


statusBar.setText( "Moved at [" + event.getX() +


", " + event.getY() + "]" );


}



public static void main( String args[] )


{


MouseTracker application = new MouseTracker();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Mouse(Ex2)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Painter extends JFrame {


private int pointCount = 0;


private Point points[] = new Point[ 1000 ];


public Painter()


{


super( "A simple paint program" );


getContentPane().add( new JLabel( "Drag the mouse to draw" ),


BorderLayout.SOUTH );


addMouseMotionListener(


new MouseMotionAdapter() {


public void mouseDragged( MouseEvent event )


{


if ( pointCount < points.length ) {


points[ pointCount ] = event.getPoint();


++pointCount;


repaint();


}


});


setSize( 300, 150 ); setVisible( true );


}


public void paint( Graphics g )


{


super.paint( g );


for ( int i = 0; i < points.length && points[ i ] != null; i++ )


g.fillOval( points[ i ].x, points[ i ].y, 4, 4 );


}


public static void main( String args[] )


{


Painter application = new Painter();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


KeyEvent

void keyPressed(KeyEvent)

void keyReleased(KeyEvent)

void keyType(KeyEvent)

Example:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class KeyDemo extends JFrame implements KeyListener {


private String line1 = "", line2 = "", line3 = "";


private JTextArea textArea;


public KeyDemo()


{


super( "Demonstrating Keystroke Events" );


textArea = new JTextArea( 10, 15 );


textArea.setText( "Press any key on the keyboard..." );


textArea.setEnabled( false );


textArea.setDisabledTextColor( Color.BLACK );


getContentPane().add( textArea );


addKeyListener( this );


setSize( 350, 100 );


setVisible( true );



}





____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


KeyEvent(cont)

public void keyPressed( KeyEvent event )


{


line1 = "Key pressed: " + event.getKeyText( event.getKeyCode() );


setLines2and3( event );


}


public void keyReleased( KeyEvent event )


{


line1 = "Key released: " + event.getKeyText( event.getKeyCode() );


setLines2and3( event );


}


public void keyTyped( KeyEvent event )


{


line1 = "Key typed: " + event.getKeyChar();setLines2and3( event );


}


private void setLines2and3( KeyEvent event )


{


line2 = "This key is " + ( event.isActionKey() ? "" : "not " ) +


"an action key";


String temp = event.getKeyModifiersText( event.getModifiers() );


line3 = "Modifier keys pressed: " +


( temp.equals( "" ) ? "none" : temp );


textArea.setText( line1 + "
\
n" + line2 + "
\
n" + line3 + "
\
n" );


}


public static void main( String args[] )


{


KeyDemo application = new KeyDemo();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

}


____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JMenu

The class used to declare menu are

JMenuBar: Contain the methods necessary to manage
a menu bar, which is a container for menu.


JMenu: Contain the methods necessary to manage a
menu menu, which is a container for menu item.


JMenuItem:Component inside a menu.


JChenkBoxMenuItem: Contain the methods necessary
to manage a menu menu item.
JRadioButtonMenuItem: are maintained as part of
Button Group.



____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example(menu)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MenuTest extends JFrame {


private final Color colorValues[] =


{ Color.black, Color.blue, Color.red, Color.green };


private JRadioButtonMenuItem colorItems[], fonts[];


private JCheckBoxMenuItem styleItems[];


private JLabel displayLabel;


private ButtonGroup fontGroup, colorGroup;


private int style;


public MenuTest()


{


super( "Using JMenus" );


JMenu fileMenu = new JMenu( "File" );


fileMenu.setMnemonic( 'F' );


JMenuItem aboutItem = new JMenuItem( "About..." );


aboutItem.setMnemonic( 'A' );


fileMenu.add( aboutItem );


aboutItem.addActionListener(


new ActionListener() {


public void actionPerformed( ActionEvent event )


{


JOptionPane.showMessageDialog( MenuTest.this,


"This is an example
\
nof using menus",


"About", JOptionPane.PLAIN_MESSAGE );


}});

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example(cont)


JMenuBar bar = new JMenuBar();


setJMenuBar( bar );


bar.add( fileMenu );


JMenu formatMenu = new JMenu( "Format" );


formatMenu.setMnemonic( 'r' );


String colors[] = { "Black", "Blue", "Red", "Green" };


JMenu colorMenu = new JMenu( "Color" );


colorMenu.setMnemonic( 'C' );


colorItems = new JRadioButtonMenuItem[ colors.length ];


colorGroup = new ButtonGroup();


ItemHandler itemHandler = new ItemHandler();


for ( int count = 0; count < colors.length; count++ ) {


colorItems[ count ] =


new JRadioButtonMenuItem( colors[ count ] );


colorMenu.add( colorItems[ count ] );


colorGroup.add( colorItems[ count ] );


colorItems[ count ].addActionListener( itemHandler );


}


colorItems[ 0 ].setSelected( true );


formatMenu.add( colorMenu );


formatMenu.addSeparator();


String fontNames[] = { "Serif", "Monospaced", "SansSerif" };



JMenu fontMenu = new JMenu( "Font" );


fontMenu.setMnemonic( 'n' );


fonts = new JRadioButtonMenuItem[ fontNames.length ];



____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example(cont)

fontGroup = new ButtonGroup();


for ( int count = 0; count < fonts.length; count++ ) {


fonts[ count ] = new JRadioButtonMenuItem( fontNames[ count ] );


fontMenu.add( fonts[ count ] );


fontGroup.add( fonts[ count ] );


fonts[ count ].addActionListener( itemHandler );


}


fonts[ 0 ].setSelected( true );


fontMenu.addSeparator();


String styleNames[] = { "Bold", "Italic" };


styleItems = new JCheckBoxMenuItem[ styleNames.length ];


StyleHandler styleHandler = new StyleHandler();


for ( int count = 0; count < styleNames.length; count++ ) {


styleItems[ count ] =


new JCheckBoxMenuItem( styleNames[ count ] );


fontMenu.add( styleItems[ count ] );


styleItems[ count ].addItemListener( styleHandler );


}


formatMenu.add( fontMenu );


bar.add( formatMenu );


displayLabel = new JLabel( "Sample Text", SwingConstants.CENTER );


displayLabel.setForeground( colorValues[ 0 ] );


displayLabel.setFont( new Font( "Serif", Font.PLAIN, 72 ) );


getContentPane().setBackground( Color.CYAN );


getContentPane().add( displayLabel, BorderLayout.CENTER );


setSize( 500, 200 );


setVisible( true );


}


public static void main( String args[] )


{


MenuTest application = new MenuTest();


application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


Example(cont)

private class ItemHandler implements ActionListener {


public void actionPerformed( ActionEvent event )


{


for ( int count = 0; count < colorItems.length; count++ )



if ( colorItems[ count ].isSelected() ) {


displayLabel.setForeground( colorValues[ count ] );


break;


}


for ( int count = 0; count < fonts.length; count++ )



if ( event.getSource() == fonts[ count ] ) {


displayLabel.setFont(


new Font( fonts[ count ].getText(), style, 72 ) );


break;


}


repaint();


}


}


private class StyleHandler implements ItemListener {


public void itemStateChanged( ItemEvent e )


{


style = 0;


if ( styleItems[ 0 ].isSelected() )


style += Font.BOLD;


if ( styleItems[ 1 ].isSelected() )


style += Font.ITALIC;


displayLabel.setFont(


new Font( displayLabel.getFont().getName(), style, 72 ) );


repaint();


}


}


}

____________________________________________________________

Design by

Chim

Bunthoeurn

e
-
mail: cbunthoeurn@yahoo.com

2011
-
2012


JTabbedPane

import java.awt.*;

import javax.swing.*;

public class JTabbedPaneDemo extends JFrame {


public JTabbedPaneDemo()


{


super( "JTabbedPane Demo " );


JTabbedPane tabbedPane = new JTabbedPane();


JLabel label1 = new JLabel( "panel one", SwingConstants.CENTER );


JPanel panel1 = new JPanel();


panel1.add( label1 );


tabbedPane.addTab( "Tab One", null, panel1, "First Panel" );


JLabel label2 = new JLabel( "panel two", SwingConstants.CENTER );


JPanel panel2 = new JPanel();


panel2.setBackground( Color.YELLOW );panel2.add( label2 );


tabbedPane.addTab( "Tab Two", null, panel2, "Second Panel" );


JLabel label3 = new JLabel( "panel three" );


JPanel panel3 = new JPanel();


panel3.setLayout( new BorderLayout() );


panel3.add( new JButton( "North" ), BorderLayout.NORTH );


panel3.add( new JButton( "West" ), BorderLayout.WEST );


panel3.add( new JButton( "East" ), BorderLayout.EAST );


panel3.add( new JButton( "South" ), BorderLayout.SOUTH );


panel3.add( label3, BorderLayout.CENTER );


tabbedPane.addTab( "Tab Three", null, panel3, "Third Panel" );


getContentPane().add( tabbedPane );


setSize( 250, 200 );setVisible( true );


}


public static void main( String args[] )


{


JTabbedPaneDemo tabbedPaneDemo = new JTabbedPaneDemo();


tabbedPaneDemo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

}