/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tjframe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TJFrame extends JFrame {
Container container = null;
// 1. Create a Swing frame.
public TJFrame(String title) {
super(title); // set the title for the frame
// 2. Get the content pane of the frame and
// configure the frame using its content pane.
container = this.getContentPane();
container.setBackground(Color.white);
container.setForeground(Color.blue);
// 3. Create a label and add it to the frame.
JLabel label = new JLabel("This is a Swing frame",
JLabel.CENTER);
label.setFont(new Font("Sans", Font.PLAIN, 22));
container.add(label);
// 4. Add the window listener to close the frame
// and display it with the specified size.
addWindowListener(new WindowEventHandler());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(350, 200); // width=350, height=200
show(); // Display the frame
}
// 5. The main method.
public static void main(String[] args) {
TJFrame frame = new TJFrame("Swing Frame");
}
// 6. The listener class to handle closing of the frame.
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
}
Tidak ada komentar:
Posting Komentar