Swing MVC Example in java
Java Swing first programs
In this chapter, we will
program our first programs in Swing toolkit. The examples are going to be very simple. We will
cover some basic functionality.
Our first example
In our first example, we will
show a basic window.
import javax.swing.JFrame;
public class Simple extends JFrame {
public Simple() {
setSize(300, 200);
setTitle("Simple");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
Simple simple = new Simple();
simple.setVisible(true);
}
}
While this code is very small,
the application window can do quite a lot. It can be resized,
maximized, minimized. All the
complexity that comes with it has been hidden from the
application programmer.
import javax.swing.JFrame;
Here we import the JFrame
widget. It is a toplevel container, which is used for placing other
widgets.
setSize(300, 200);
setTitle("Simple");
This code will resize the window
to be 300px wide and 200px tall. It will set the title of the
window to Simple.
setDefaultCloseOperation(EXIT_ON_CLOSE);
This method will close the
window, if we click on the close button. By default nothing
Comments
Post a Comment