IT Help Desk > Java Forum
Java Swing
kazi shahin:
Swing class is important part of Java language. Java Swing class is used for Graphics User Interface (GUI). It' a popular class in Java.
Swing Components
kazi shahin:
A simple JFRame Code
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 happens.
This code generate the below JFrame, titled Simple.
kazi shahin:
The above program code makes the window & set it by default at the left top. The below code will make a window & adjust it at the middle of a screen
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class CenterOnScreen extends JFrame {
public CenterOnScreen() {
setSize(300, 200);
setTitle("CenterOnScreen");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
setLocation(size.width/2 - getWidth()/2,
size.height/2 - getHeight()/2);
}
public static void main(String[] args) {
CenterOnScreen cos = new CenterOnScreen();
cos.setVisible(true);
}
}
To center the window on the screen, you must know the resolution of the monitor. For this, here it is useing the Toolkit class.
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
We get the toolkit and figure out the screen size.
setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
To place the window on the screen, here it need to call the setLocation() method
kazi shahin:
The below code is about J button.There are two buttons here. The first button will beep a sound and the second button will close the window.
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Buttons extends JFrame {
private Toolkit toolkit;
public Buttons() {
setTitle("Buttons");
setSize(300, 200);
toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
setLocation((size.width - getWidth())/2, (size.height - getHeight())/2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
JButton beep = new JButton("Beep");
beep.setBounds(150, 60, 80, 30);
beep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
toolkit.beep();
}
});
JButton close = new JButton("Close");
close.setBounds(50, 60, 80, 30);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
panel.add(beep);
panel.add(close);
}
public static void main(String[] args) {
Buttons buttons = new Buttons();
buttons.setVisible(true);
}
}
The above code showing you about Layout management and event handling also.
JPanel panel = new JPanel();
getContentPane().add(panel);
It creates a JPanel component. It is a generic lightweight container. Added the JPanel to the JFrame.
panel.setLayout(null);
By default, the JPanel has a FlowLayout manager. The layout manager is used to place widgets onto the containers. If we call setLayout(null) we can position our components absolutely. For this, just use the setBounds() method.
JButton beep = new JButton("Beep");
beep.setBounds(150, 60, 80, 30);
beep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
toolkit.beep();
}
});
Here the code create a button. Positioning it by calling the setBounds() method. Then it is necessary to add an action listener. The action listener will be called, when we perform an action on the button. In our case, if we click on the button. The beep button will play a simple beep sound.
System.exit(0);
The close button will exit the application. For this, we call the System.exit() method.
panel.add(beep);
panel.add(close);
In order to show the buttons, we must add them to the pane
The output is below
ananda:
It is really a good effort. Please keep it up. One opinion, please start from the very beginning. That is how the platform will be set up and what software required to install (NetBean , Eclipse etc. ). Actually, please ahead in such a way that one who is not totally new in Java also get interest to learn by this post.
Again I would like to thank you for this good initiatives.
Navigation
[0] Message Index
[#] Next page
Go to full version