![]() ![]() The following example loads an icon from the graphics file subscribe.gif and creates a JButton with the icon as its label: ImageIcon subscribe = new ImageIcon( " subscribe.gif ") You can create an ImageIcon object by specifying the filename of a graphic as the only argument to the constructor. Examples include a garbage can or recycling bin icon for deleting files and folder icons for opening and storing files. An icon is a small graphic that can be placed on a button, a label, or another user interface element to identify it. Swing supports the use of graphical ImageIcon objects on buttons and other components in which a label can be provided. Be sure to set up a component fully with all required configuration before placing it in a panel or any other container. When working with Swing components, a common source of mistakes is to set up aspects of a component after it has been added to a container. To find out a component’s size, call its getSize() method, which returns a Dimension object with the dimensions in height and width instance variables.Īs you will see, similar Swing components have other methods in common inherited from superclasses, such as setText() and getText() for text components and setValue() and getValue() for components that store numeric values. For most components, you don’t need to set a size the default is usually acceptable. The setSize( int, int ) method resizes the component to the width and height specified as arguments, and setSize( Dimension ) uses a Dimension object to accomplish the same thing. ![]() There also is a boolean isVisible() method. Use true to display a component and false to hide it. ![]() The setVisible( boolean ) method works for all components the way it does for containers. If you want to check whether a component is enabled, you can call the isEnabled() method, which returns a boolean value. For instance, a disabled JButton has light gray borders and gray text. Many components change in appearance to indicate whether they are presently usable. The setEnabled( boolean ) method determines whether a component can receive user input (the argument true) or is inactive and cannot receive input ( false). You will work with many of these components throughout the rest of this lesson and further in Lesson 10, “Building an Interface.”Īll Swing components inherit several useful methods from their common superclass, JComponent. Swing offers more than two dozen user interface components in addition to the buttons and containers you have used so far. Sams Teach Yourself Java in 21 Days (Covers Java 11/12), 8th Edition ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |