Home
01 Getting Started
02 Concepts
03 Variables & Data Types
04 Control Logic
05 Modifiers
06 Graphical User Interface
a GUI
b Swing Application
c Swing Applet
07 Other Useful Classes
08 Handling Events
09 Handling Exceptions
10 Project 01 - Mortg Calc
11 More on Applets
12 Layout Manager
13 Project 02 - Calculator
14 Threads
15 Project 03 - Wake Up
16 File I/O
17 Project 04 - File I/O
Expanded Table of Contents
Debugging Hints
HTML Review
Download Samples
|
Previous Next
JFC / Swing - Application
In this example, we are going to create an application class called SimpleSwing. SimpleSwing will extend the JFrame class making the frame its top-level container. Into this container, we will add the 3 components, JButton, JTextField, and JLabel.
In the source code below you will note the following steps:
- Import both of the following which will contain our GUI classes.
import javax.swing.*;
import java.awt.*;
- Use the new operator to create (or instantiate) the 3 components. Also, initialize them by calling their constructors with some text to be displayed.
JButton btn = new JButton("Do Something");
JTextField txt = new JTextField("Enter your answer here.");
JLabel lbl = new JLabel("Protected Area");
- We use the constructor for our class to load the components onto our frame.
public SimpleSwing()
- In the constructor of SimpleSwing, use the super method to define a title for the frame. The super method must be the first in the current constructor. The super method refers to the constructor of the class being extended, JFrame in this case.
super("A Simple Swing Application");
- Set the width and height of the frame using the setSize method.
setSize(180, 150);
- Create a container using JPanel to hold the components. Placing the components directly onto JFrame is not likely to yield the desired results. We will place JPanel onto JFrame.
JPanel panl = new JPanel();
- Set the foreground color (used for the text) and the background color by calling the methods setForeground and setBackground.
lbl.setForeground(Color.blue);
txt.setForeground(Color.red);
btn.setForeground(Color.green);
btn.setBackground(Color.yellow);
panl.setBackground(Color.magenta);
- Using the container method, add, place each of our 3 components onto the JPanel.
panl.add(lbl);
panl.add(txt);
panl.add(btn);
- Add panl to the JFrame SimpleSwing. We can format this as SimpleSwing.add(panl), but it is not necessary since it defaults to the container in which it is found.
add(panl);
- The setDefaultCloseOperation method determines what will happen by default when the user closes this frame. We use the option EXIT_ON_CLOSE so that when the frame is closed, the application is also closed. Without this or some alternate coding, the application will continue running even after you close the frame.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- The components cannot be seen until the setVisible method is executed.
setVisible(true);
- Of course, the main method instantiates the SimpleSwing class with the new operator, causing it to be executed.
SimpleSwing SimpSwing = new SimpleSwing();
Following is the code just discussed for this application. Try compiling and executing it.
import javax.swing.*;
import java.awt.*;
public class SimpleSwing extends JFrame {
JButton btn = new JButton("Do Something");
JTextField txt = new JTextField("Enter your answer here.");
JLabel lbl = new JLabel("Protected Area");
public SimpleSwing() {
super("A Simple AWT Application");
setSize(180, 150);
JPanel panl = new JPanel();
lbl.setForeground(Color.blue);
txt.setForeground(Color.red);
btn.setForeground(Color.green);
btn.setBackground(Color.yellow);
panl.setBackground(Color.magenta);
panl.add(lbl);
panl.add(txt);
panl.add(btn);
add(panl);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[]) {
SimpleSwing SimpSwing = new SimpleSwing();
}
}
Previous Next
|
|