
Though such IDEs can greatly simplify GUI creation, they are each different in capability.
#JAVA FLOWLAYOUT LOCK VERTICAL CODE#
Many integrated development environments provide GUI design tools in which you can specify the exact size and location of a component in a visual manner by using the mouse, then the IDE will generate the GUI code for you. As you will learn at the end of this chapter and in Chapter 22, Java provides several layout managers that can help you position components.

This is known as specifying the layout of the GUI components. Also, you typically must decide where to position each GUI component. When building a GUI, each GUI component must be attached to a container, such as a window created with a JFrame. Line 19 invokes superclass JFrame's constructor with the argument "Testing JLabel".JFrame's constructor uses this String to specify the text in the window's title bar. Typically, the JFrame subclass's constructor builds the GUI that is displayed in the window when the application executes. Lines 1214 declare the three JLabel instance variables, each of which is instantiated in the LabelFrame constructor (lines 1741). The class extends JFrame to inherit the features of a window.

Lines 38 import the classes used in class LabelFrame. We will use an instance of class LabelFrame to display a window containing three JLabels. (This item is displayed on page 520 in the print version)ħ public static void main( String args )ĩ LabelFrame labelFrame = new LabelFrame() // create LabelFrameġ0 tDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ġ1 tSize( 275, 180 ) // set frame sizeġ2 tVisible( true ) // display frameĬlass LabelFrame (Fig. (This item is displayed on page 519 in the print version)ģ import // specifies how components are arrangedĤ import // provides basic window featuresĥ import // displays text and imagesĦ import // common constants used with Swingħ import // interface used to manipulate imagesĨ import // loads imagesġ0 public class LabelFrame extends JFrameġ2 private JLabel label1 // JLabel with just textġ3 private JLabel label2 // JLabel constructed with text and iconġ4 private JLabel label3 // JLabel with added text and iconġ6 // LabelFrame constructor adds JLabels to JFrameĢ0 setLayout( new FlowLayout() ) // set frame layoutĢ2 // JLabel constructor with a string argumentĢ3 label1 = new JLabel( "Label with text" ) Ģ4 tToolTipText( "This is label1" ) Ģ5 add( label1 ) // add label1 to JFrameĢ7 // JLabel constructor with string, Icon and alignment argumentsĢ8 Icon bug = new ImageIcon( getClass().getResource( "bug1.gif" ) ) Ģ9 label2 = new JLabel( "Label with text and icon", bug,ģ1 tToolTipText( "This is label2" ) ģ2 add( label2 ) // add label2 to JFrameģ4 label3 = new JLabel() // JLabel constructor no argumentsģ5 tText( "Label with icon and text at bottom" ) ģ6 tIcon( bug ) // add icon to JLabelģ7 tHorizontalTextPosition( SwingConstants.CENTER ) ģ8 tVerticalTextPosition( SwingConstants.BOTTOM ) ģ9 tToolTipText( "This is label3" ) Ĥ0 add( label3 ) // add label3 to JFrame

We did not highlight the code in this example since most of it is new. 11.7 demonstrates several JLabel features and presents the framework we use in most of our GUI examples. Text in a JLabel normally uses sentence-style capitalization. Applications rarely change a label's contents after creating it. A JLabel displays a single line of read-only text, an image, or both text and an image. Such text is known as a label and is created with class JLabela subclass of JComponent.

In a large GUI, it can be difficult to identify the purpose of every component unless the GUI designer provides text instructions or information stating the purpose of each component. Since an application's GUI is typically specific to the application, most of our examples will consist of two classesa subclass of JFrame that helps us demonstrate new GUI concepts and an application class in which main creates and displays the application's primary window.Ī typical GUI consists of many components. JFrame provides the basic attributes and behaviors of a windowa title bar at the top of the window, and buttons to minimize, maximize and close the window.
#JAVA FLOWLAYOUT LOCK VERTICAL WINDOWS#
Most windows you will create are an instance of class JFrame or a subclass of JFrame. This is our first example in which the application appears in its own window. This framework uses several concepts that you will see in many of our GUI applications. Our next example introduces a framework for building GUI applications.
