Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: changing Buttons in program help
|
Posted: Apr 12, 2003 6:30 PM
|
|
// Look And Feel Demonstration
// Changing the look and feel.
// Java core packages
import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
public class LookAndFeelDemo extends JFrame {
private String strings[] = { "Metal", "Motif", "Windows" };
private UIManager.LookAndFeelInfo looks[];
private JButton button[];
//private ButtonGroup group;
// private JButton button;
private JLabel label;
// private JComboBox comboBox;
// set up GUI
public LookAndFeelDemo()
{
super( "Look and Feel Demo" );
Container container = getContentPane();
// set up panel for NORTH of BorderLayout
JPanel northPanel = new JPanel();
northPanel.setLayout( new GridLayout( 3, 1, 0, 5 ) );
// set up label for NORTH panel
label = new JLabel( "This is a Metal look-and-feel",
SwingConstants.CENTER );
northPanel.add( label );
// set up button for NORTH panel
// button = new JButton( "JButton" );
// northPanel.add( button );
// set up combo box for NORTH panel
// comboBox = new JComboBox( strings );
// northPanel.add( comboBox );
// attach NORTH panel to content pane
container.add( northPanel, BorderLayout.NORTH );
// create array for buttons
button = new JButton[ strings.length ];
// set up panel for SOUTH of BorderLayout
JPanel southPanel = new JPanel();
southPanel.setLayout(
new GridLayout( 1, button.length ) );
// set up buttons for SOUTH panel
button = new JButton[ strings.length ];
//group = new ButtonGroup();
ButtonHandler handler = new ButtonHandler();
for ( int count = 0; count < button.length; count++ ) {
button[ count ] = new JButton( strings[ count ] );
button[ count ].addActionListener( handler );
//group.add( button[ count ] );
southPanel.add( button[ count ] );
}
// attach SOUTH panel to content pane
container.add( southPanel, BorderLayout.SOUTH );
// get installed look-and-feel information
looks = UIManager.getInstalledLookAndFeels();
setSize( 300, 200 );
setVisible( true );
button[ 0 ].setSelected( true );
}
// use UIManager to change look-and-feel of GUI
private void changeTheLookAndFeel( int value )
{
// change look and feel
try {
UIManager.setLookAndFeel(
looks[ value ].getClassName() );
SwingUtilities.updateComponentTreeUI( this );
}
// process problems changing look and feel
catch ( Exception exception ) {
exception.printStackTrace();
}
}
// execute application
public static void main( String args[] )
{
LookAndFeelDemo application = new LookAndFeelDemo();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}
// private inner class to handle button events
private class ButtonHandler implements ActionListener {
// process user's look-and-feel selection
public void actionPerformed( ActionEvent event )
{
//for ( int count = 0; count < button.length; count++ )
//if ( button[ count ].isSelected() ) {
//label.setText( "Look and Feel = " +
//strings[ count ] + " " );
// comboBox.setSelectedIndex( count );
//changeTheLookAndFeel( count );
//}
label.setText( "Look and Feel = " + event.getActionCommand());
for (int i = 0; i < strings.length; i++){
if (event.getActionCommand().equals(strings[i])) changeTheLookAndFeel(i);
}
}
} // end private inner class ItemHandler
} // end class LookAndFeel
|
|