/* * [TestJPanel.java] * * Summary: demonstrate the use of javax.swing.JPanel. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2009-04-26 initial version */ package com.mindprod.example; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import static java.lang.System.*; /** * demonstrate the use of javax.swing.JPanel. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2009-04-26 initial version * @since 2009-04-26 */ @SuppressWarnings( { "UnusedDeclaration" } ) final class TestJPanel { /** * height of frame in pixels */ private static final int height = 100; /** * width of frame in pixels */ private static final int width = 300; private static final String RELEASE_DATE = "2009-02-26"; /** * title for frame */ private static final String TITLE_STRING = "JPanel Demo"; /** * program version */ private static final String VERSION_STRING = "1.0"; /** * Debugging harness for a JFrame * * @param args command line arguments are ignored. */ @SuppressWarnings( { "UnusedParameters" } ) public static void main( String args[] ) { // Invoke the run method on the Swing event dispatch thread // Sun now recommends you call ALL your GUI methods on the Swing // event thread, even the initial setup. // Could also use invokeAndWait and catch exceptions SwingUtilities.invokeLater( new Runnable() { /** * } fire up a JFrame on the Swing thread */ public void run() { try { UIManager.setLookAndFeel( new com.sun.java.swing.plaf.motif.MotifLookAndFeel() ); } catch ( Exception e ) { err.println(); e.printStackTrace( err ); err.println( "Problem setting look and feel" ); err.println(); } JFrame.setDefaultLookAndFeelDecorated( true ); final JFrame jframe = new JFrame( TITLE_STRING + " " + VERSION_STRING ); final Container contentPane = jframe.getContentPane(); jframe.setSize( width, height ); // Note that jframe.setBackground is almost useless. // You must set the background colour of the contentPane. contentPane.setBackground( Color.YELLOW ); contentPane.setForeground( Color.BLUE ); final JPanel jPanel = new JPanel(); jPanel.setBackground( Color.ORANGE ); jPanel.setForeground( Color.GREEN ); jPanel.setLayout( new FlowLayout() ); jPanel.add( new JLabel( "Test" ) ); contentPane.add( jPanel ); jframe.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Alternatively DISPOSE_ON_CLOSE, HIDE_ON_CLOSE (not recommended) // or DO_NOTHING_ON_CLOSE which requires a WindowClosing eventhandler // like a Frame closing // to decide what to do. // Note how JLabel inherits background but not foreground colour. jframe.validate(); jframe.setVisible( true ); // Nothing much to see, just an empty yellow frame, with word Test } } ); } }