package com.javaranch.common ; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** Contains a collection of static methods related to GUI's (AWT and Swing stuff).
- - - - - - - - - - - - - - - - -
Copyright (c) 1998-2000 Paul Wheaton
You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...
My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.
The original source can be found at JavaRanch
- - - - - - - - - - - - - - - - -
@author Paul Wheaton @author Nathan Paris */ public class GUI extends AWT { // make sure no one instantiates this class private GUI(){} /** A convenience method that makes a JMenuItem out of some text and adds that to a JMenu.
@param m A JMenu object to add the new JMenuItem to.
@param desc The text of the new menu item.
@return The newly created JMenuItem.
*/ public static JMenuItem addMenuItem( JComponent m , String desc ) { JMenuItem mi = new JMenuItem( desc ); m.add( mi ); return mi ; } /** A convenience method that makes a JMenuItem out of some text, adds an ActionListener and adds the result to a JMenu.
@param m A JMenu object to add the new JMenuItem to.
@param desc The text of the new menu item.
@param listener The action listener that will want to be notified when this item is selected.
@return The newly created JMenuItem.
*/ public static JMenuItem addMenuItem( JComponent m , String desc , ActionListener listener ) { JMenuItem mi = new JMenuItem( desc ); mi.addActionListener( listener ); m.add( mi ); return mi ; } /** A convenience function that adds an ActionListener to a JMenuItem and adds the item to a JMenu.
@param m A JMenu object to add the JMenuItem to.
@param mi The JMenuItem.
@param listener The action listener that will want to be notified when this item is selected.
@return The newly created JMenuItem.
*/ public static JMenuItem addMenuItem( JComponent m , JMenuItem mi , ActionListener listener ) { mi.addActionListener( listener ); m.add( mi ); return mi ; } public static JLabel label( String text , int x , int y , int width , int height ) { JLabel label = new JLabel( text ); label.setBounds( x , y , width , height ); return label ; } /** Creates a label with the correct size for the provided FontMetric.
*/ public static JLabel label( String text , int x , int y , Font font , FontMetrics metrics ) { int width = metrics.stringWidth( text ); JLabel label = new JLabel( text ); label.setFont( font ); label.setBounds( x , y , width , metrics.getHeight() + 5 ); return label ; } public static JTextField textField( int columns , int x , int y , int width , int height ) { JTextField field = new JTextField( columns ); field.setBounds( x , y , width , height ); return field ; } public static JButton button( String text , int x , int y , int width , int height ) { JButton button = new JButton( text ); button.setBounds( x , y , width , height ); return button ; } public static JButton button( String text , int x , int y , Font font , FontMetrics metrics ) { JButton button = new JButton( text ); button.setFont( font ); int width = metrics.stringWidth( text ) + 40 ; button.setBounds( x , y , width , metrics.getHeight() + 5 ); return button ; } public static JCheckBox checkBox( int x , int y , int width , int height ) { JCheckBox checkBox = new JCheckBox(); checkBox.setBounds( x , y , width , height ); return checkBox ; } public static void messageDialog( Frame f , String message ) { JOptionPane.showMessageDialog( f , message ); } public static void warningDialog( Frame f , String message ) { JOptionPane.showMessageDialog( f , message , "" , JOptionPane.WARNING_MESSAGE ); } public static void errorDialog( Frame f , String message ) { JOptionPane.showMessageDialog( f , message , "" , JOptionPane.ERROR_MESSAGE ); } /** Provided for testing purposes only.
*/ public static void unitTest( TestLog log ) { // xxx need tests } }