Below is the source code to build window in Swing.
SwingJWindow.java
[java]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingJWindow {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public SwingJWindow(){
prepareGUI();
}
public static void main(String[] args){
SwingJWindow swingContainerDemo = new SwingJWindow();
swingContainerDemo.showJWindowDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
msglabel = new JLabel("Welcome to SPlessons."
, JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showJWindowDemo(){
headerLabel.setText("Container in action: JWindow");
final MessageWindow window = new MessageWindow(mainFrame, "Welcome to SPlessons.");
JButton okButton = new JButton("Open a Window");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
window.setVisible(true);
statusLabel.setText("A Window shown to the user.");
}
});
controlPanel.add(okButton);
mainFrame.setVisible(true);
}
class MessageWindow extends JWindow{
private String message;
public MessageWindow(JFrame parent, String
message) {
super(parent);
this.message = message;
setSize(300, 300);
setLocationRelativeTo(parent);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(0,0,getSize().width - 1,getSize().height - 1);
g.drawString(message,50,150);
}
}
}[/java]
Create the main method and create the object to the class.
[java]public static void main(String[] args){
SwingJWindow swingContainerDemo = new SwingJWindow();
swingContainerDemo.showJWindowDemo();[/java]
Add grid layout to the main frame and WindowListener to the main frame.
[java]mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent)[/java]
Create message label, header label, status label and add this labels to main frame.
[java]headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
msglabel = new JLabel("Welcome to SPlessons.", JLabel.CENTER);
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true); [/java]
Create the open window button and add Listeners to the button, when user click on this button another message window will be opened.
[java]JButton okButton = new JButton("Open a Window");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
[/java]
Output:
Output will be as follows.It consists of header message and status message.
When click on "open a window" button then it displays the output as follows.