Java Swing Example
Page 1 of 1
Java Swing Example
Problem: I want to create a frame window with a specific size and display it on the screen at a specific location.
Solution: You can use the JFrame.setBounds() method define the frame location and size. Then use the setVisible() method to make the frame visible on the screen. The following sample code, JFrameTest.java, shows you how to do this.
Solution: You can use the JFrame.setBounds() method define the frame location and size. Then use the setVisible() method to make the frame visible on the screen. The following sample code, JFrameTest.java, shows you how to do this.
- Code:
import javax.swing.*;
public class JFrameTest {
public static void main(String[] a) {
JFrame f = new JFrame("Frame Title");
f.setBounds(50,50,150,150);
f.setVisible(true);
}
}
Note 1: The size specified in the setBounds() method includes both the content area and the title area of the frame.
Note 2: After executing the f.setVisible(true) statement, the main() method reaches the end of execution. But the execution of the entire program is not terminated. This is due the fact that the f.setVisible(true) statement actually launches new execution threads, AWT threads. Those threads are still running even after the main thread reaches the end.
Note 3: When you click the close icon, the frame will be removed from the screen. But the execution of the entire program is not terminated. Removing all frames from the screen does not force all AWT threads to end.
Problem: I have a frame window displayed on the screen and I want to close the frame and terminate the application, when user invokes the close command from the window's system menu, or clicks on the close icon from the window's system icon list.
Note 2: After executing the f.setVisible(true) statement, the main() method reaches the end of execution. But the execution of the entire program is not terminated. This is due the fact that the f.setVisible(true) statement actually launches new execution threads, AWT threads. Those threads are still running even after the main thread reaches the end.
Note 3: When you click the close icon, the frame will be removed from the screen. But the execution of the entire program is not terminated. Removing all frames from the screen does not force all AWT threads to end.
Problem: I have a frame window displayed on the screen and I want to close the frame and terminate the application, when user invokes the close command from the window's system menu, or clicks on the close icon from the window's system icon list.
Solution 1: You can use the JFrame.setDefaultCloseOperation() method to change the default behavior option of JFrame responding to the window closing event. Select the EXIT_ON_CLOSE option will terminate the application immediately. Of course, when the application is terminated, all frames will be closed automatically. The following sample code, JFrameClose1.java, shows you how to do this.
- Code:
import javax.swing.*;
public class JFrameClose1 {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setTitle("Closing Frame with Default Close Operation");
f.setBounds(100,50,500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
- Code:
import java.awt.event.*;
import javax.swing.*;
public class JFrameClose2 {
public static void main(String[] a) {
MyJFrame f = new MyJFrame();
f.setTitle("Closing Frame with Process Window Event");
f.setBounds(100,50,500,300);
f.setVisible(true);
}
static class MyJFrame extends JFrame {
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
}
Solution 3: You can create a new window event listener class and add an object of this listener class to the JFrame object. In the new window event listener, you can implement your owner version of windowClosing() handler method. Of course, the quickest way to create a new window event listener class is to extend the WindowAdapter class. The following sample code, JFrameClose3.java, shows you how to do this.
- Code:
import java.awt.event.*;
import javax.swing.*;
public class JFrameClose3 {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setTitle("Closing Frame with Window Listener");
f.setBounds(100,50,500,300);
f.addWindowListener(new MyWindowListener());
f.setVisible(true);
}
static class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
For more about it CLICK here.
Similar topics
» Java swing Layout presentation
» Java Tutorial: Painting in swing components, part 1
» Java Swing: adding confirmation dialogue for closing Window in JFrame
» Java Tutorial: Painting in swing components, part 2 (Interactive painting)
» Java Tutorial: Using Two Java Reference Keywords, this and super
» Java Tutorial: Painting in swing components, part 1
» Java Swing: adding confirmation dialogue for closing Window in JFrame
» Java Tutorial: Painting in swing components, part 2 (Interactive painting)
» Java Tutorial: Using Two Java Reference Keywords, this and super
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford