CIS-257 Home: http://www.c-jump.com/bcc/c257c/c257syllabus.html
NetBeans, New Project
Category: General Type: Java Application
Make sure to deselect the "Create Main Class" checkbox
For step-by-step instructions, see Setting up CelsiusConverter Project tutorial.
(This tutorial is part of Creating a GUI With JFC/Swing series of Oracle Java tutorials.)
Right-click the button in the design view and choose
Events -> Action -> ActionPerformed
javax.awt.JFrame javax.awt.JDialog javax.awt.JApplet
Each of the above classes start a single containment hierarchy.
AWT is the Abstract Window Toolkit, Java's original platform-independent windowing, graphics, and user-interface widget toolkit.
The AWT is part of the Java Foundation Classes (JFC) - the standard Java GUI API.
// create JFrame JFrame firstFrame = new JFrame( "Hello" ); // set size and title firstFrame.setSize( 200, 100 ); firstFrame.setTitle( "My frame" );
Method
frame.getContentPane() // returns a Container object
returns the default content pane of the JFrame
For example,
frame.getContentPane().add( myLabel, BorderLayout.CENTER );
The default content pane is a container that inherits from JComponent
The pane is using the BorderLayout as its layout manager
The default layout manager for JPanel is FlowLayout
package pckg; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.text.*; import java.awt.BorderLayout; public class SwingMain { public static void main(String args []) { JFrame frame = new JFrame("JFrame is me"); frame.setBounds(0, 0, 400, 300); frame.setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE ); // Create content JPanel JPanel contentPane = new JPanel( new BorderLayout() ); Border border = BorderFactory.createEtchedBorder(); border = BorderFactory.createTitledBorder( border, "My Title"); contentPane.setBorder( border ); frame.setContentPane( contentPane ); // add button to layout: JButton button = new JButton( "Hello" ); frame.getContentPane().add(button); frame.setVisible(true); } //main } //SwingMain
mbar = new JMenuBar(); // create menu bar frame.setJMenuBar( mbar ); // add to JFrame
|
|
import javax.swing.*;
java.lang.Object
java.awt.Component
javax.awt.JComponent
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
javax.awt.JFrame
Type of component that holds other components
Can treat group as single entity
Defined in Container class
Often takes form of window
Drag
Resize
Minimize
Restore
Close
Child of Container
Does not have title bars or borders
Rarely used
Instead, use JFrame subclasses
Swing GUI app:
creates JFrame
adds other GUI widgets within JFrame for display
JFrame constructors:
JFrame() JFrame( String title ) JFrame( GraphicsConfiguration gc ) JFrame( String title, GraphicsConfiguration gc )
Default JFrame behavior:
When the user clicks Close button 'X', JFrame window becomes hidden, but the application keeps running.
To allow window to close and exit the application,
JFrame frame = new JFrame( "JFrame is me" ); frame.setBounds( 0, 0, 400, 300 ); frame.setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE );
To change the window title.
frame.setTitle( "Celsius Converter App" );
To make window fixed-size (non-resizable)
frame.setResizable( false ); // default is true
Window decorations:
Icon and button Appearance
Look and feel
Default appearance and behavior of user interface
JFrame frame = new JFrame( "window caption goes here" ); frame.setDefaultLookAndFeelDecorated( true );
// Managing text getText() setText( String ) // Enable text editing setEditable( boolean ) // whether user can edit the text selectAll() // Enable user interaction with the control setEnabled( boolean ) // if enabled, it may respond to user input // Enable control to receive focus on the form requestFocusInWindow() // set focus setFocusable( boolean ) // enable to receive focus // Set horizontal alignment for the text // JTextField.LEFT, CENTER, RIGHT, LEADING, TRAILING setHorizontalAlignment( int ) getHorizontalAlignment()
JTextField is a component where user can type a single line of text data
Getting text from the control:
double dbl = Double.parseDouble( myTextField.getText() ); int value = Integer.parseInt( myTextField.getText() );
Setting the text in a control:
myTextField.setText( "HELLO" );
Moving focus to the control:
myTextField.requestFocusInWindow();
Two methods for displaying a form:
frame.setVisible( boolean ) // makes window visible (the default is invisible) setLocationRelativeTo( component ) // set the location of dialog in respect of parent dialog setLocationRelativeTo( null ) // centers the window on the screen
Holds visible text on the form
Methods:
add() remove() setText() getText()
Represents a clickable button
Methods:
setText( String caption ) getText() // get caption
JFrame.add() method adds JButton to the frame
Note: when clicked, no resulting action occurs -- the code has to be written to handle the click event.
Event-driven program expects various events in any order: user keystrokes, mouse clicks, minimizing windows, and so on
Source: a component where the event is generated
Listener: an object to be notified about the event and processing it
To accept event messages, create a class that implements ActionListener:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class MyButtonClickAction implements ActionListener { JButton mButton; public MyButtonClickAction( JButton button ) { mButton = button; } /** * Handles user clicks */ public void actionPerformed( ActionEvent event ) { mButton.setText( "Click!" ); } }//class MyButtonClickAction
Add action listener to the event source widget:
JButton button = new JButton( "Hello" ); button.addActionListener( new MyButtonClickAction( button ) );
ActionListener interface specifies the
actionPerformed( ActionEvent evt );
method. The implementation can execute any statements in response to the event.
The actionPerformed() is invoked when the user
clicks a button
chooses a menu item
Note: When more than one component is added to the JFrame, it is necessary to find out which component is the source of the event:
/** * Handles user clicks */ public void actionPerformed( ActionEvent event ) { Object obj = event.getSource(); if ( obj == mButton ) { mButton.setText( "Click!" ); } }
A tool tip helps to understand the purpose of component on the input form
Tool tips appear when a user hovers the mouse over the component
// Create frame // ... // Add button and set font: JButton button = new JButton( "Hello" ); button.setToolTipText( "tool tip text goes here" ); frame.getContentPane().add( button ); frame.setVisible( true );
Creates an object that holds typeface and size information
Font constructor arguments: typeface, style, and point size.
For example, changing a JButton's font requires Font object argument:
// Create frame // ... // Add button and set font: JButton button = new JButton( "Hello" ); Font headlineFont = new Font( "Arial", Font.BOLD, 36 ); button.setFont( headlineFont ); frame.getContentPane().add( button ); frame.setVisible( true );
Common practice: define a class that descends from JFrame class
Advantages: configure JFrame properties and controls in object's constructor
Note: remember to call parent class constructor using the super keyword
public class JMyFrame extends JFrame { static final int WINDOW_WIDTH = 210; static final int WINDOW_HEIGHT = 130; //constructor public JMyFrame() { super( "window title goes here" ); setSize( WINDOW_WIDTH, WINDOW_HEIGHT ); setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE ); setVisible( true ); } }//class JMyFrame