Java Swing Tutorials

Java 2D API
previous Home next

We can make the Programming more interactive with Java 2D API. we can add figures, images, animation to our GUI and even pass visual information with the help of Java 2D API. We can easily use 2D within Swing components such as drop shadows since Swing is built on 2D package.

Pluggable Look and Feel

The Java Swing supports the look and feel features. Which means the dramatically changes in the component of the java swing like JFrame, JWindow etc. for visit it in several types of window we can create our own look and feel using Synth package. There are many types of the existing look and feels which are available to Swing programs provided by GTK+ look and feel. Moreover, the look and feel of the platform can be specified by the program while running and also to use Java look and feel can be specified by it.

 

Example

package r4r;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class twoDAPItest extends JFrame{
JLabel msgLab;
public twoDAPItest(){
super("Swing");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
ActionListener actl = new ActionListener(){
public void actionPerformed(ActionEvent ae){
msgLab.setText(((JButton)ae.getSource()).getText());
}
};
JButton btn;
JPanel btnPanel = new JPanel();
btnPanel.setBorder(new TitledBorder("Click at button"));
for (int i = 0; i < 3; i++){
btn = new JButton("Button " + (i + 1));
btn.addActionListener(actl);
btnPanel.add(btn);
}
JPanel panel2 = new JPanel(new BorderLayout());
panel2.setBorder(new EmptyBorder(8, 8, 8, 8));
msgLab = new JLabel("No button pressed!");
panel2.add(msgLab, BorderLayout.NORTH);
panel2.add(btnPanel, BorderLayout.CENTER);
setContentPane(panel2);
pack();
setVisible(true);
}
public static void main(String[] args){
new twoDAPItest();
}
}
previous Home next