A JFrame is equivalent to a window and these features will make your JFrame look as you like, make sure you consider all these parameters
before developing main window of any java swing application
1. Shape of JFrame
To change the shape of Jframe use this code
JFrame testFrame=new JFrame();
testFrame.setUndecorated(true);
// Frame is required to set
undecorated for changing shape and transparency
Dimension arcSize = new Dimension(250,250);
RoundRectangle2D rectShape = new RoundRectangle2D.Double(0d, 0d,
testFrame.getWidth(),
testFrame.getHeight(),
arcSize.width, arcSize.height);
testFrame.setShape(rectShape);
|
2. Opacity of JFrame
Add line in bold to set opacity of JFrame
JFrame testFrame=new JFrame ();
testFrame.setUndecorated(true);
// Frame is required to set undecorated for
changing shape and transparency
testFrame.setOpacity(0.3f);
|
This image
describes how jframe will look after setting opacity
3. Title of JFrame which will alsp appear in taskbar
JFrame testFrame=new JFrame ();
testFrame.setTitle("JFrame Example");
|
4. Icon for taskbar and Header icon
JFrame testFrame=new JFrame ();
testFrame.setTitle("JFrame Example");
testFrame.setBounds(600, 200, 300,
300);
ImageIcon titleIcon=new ImageIcon("E:/titleIcon.png");
testFrame.setIconImage(titleIcon.getImage());
|
5. Default close operation
We must include Default close operation for windows , If
this JFrame is main window for any application then program must exit properly after
its close operation.
It can done in two ways.
1.
By using function setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2. By implementing WindowListener interface
and override windowClosing method
Ex. We can write Sytem.exit(0);
public void
windowClosing(WindowEvent arg0) {
System.out.println("Window closing");
// Here we can write closure of all
pending task or save data if required
System.exit(0); // this must be last line
}
|
6. Complete Code with all parameters
import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.Dimension;
import
java.awt.geom.RoundRectangle2D;
import
javax.swing.ImageIcon;
import
javax.swing.JFrame;
import
javax.swing.JPanel;
public class
JFrameExample extends JFrame
{
private static final long serialVersionUID = 1L;
public
JFrameExample() {
this.setTitle("JFrame Example");
this.setBounds(600,
200, 300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon
titleIcon=new ImageIcon("E:/titleIcon.png");
this.setIconImage(titleIcon.getImage());
JPanel
pnlOuter=new JPanel();
pnlOuter.setBackground(Color.GRAY);
this.setLayout(new
BorderLayout());
this.add(pnlOuter,BorderLayout.CENTER);
}
public static void
main(String[] args) {
JFrameExample
testFrame=new JFrameExample();
// Comment Block 1 for Example C,D,E,F,G
// Block 1 Start
testFrame.setUndecorated(true); //
Frame is required to set undecorated for changing shape and transparency
Dimension
arcSize = new Dimension(250,250);
RoundRectangle2D
rectShape = new RoundRectangle2D.Double(0d, 0d,
testFrame.getWidth(),
testFrame.getHeight(),
arcSize.width,
arcSize.height);
testFrame.setShape(rectShape);
testFrame.setOpacity(0.3f);
// Block 1 End
testFrame.setVisible(true);
}
}
|
In this code
JPanel is also added, we can continue adding components to this panel for further
development of application.
In next blogs
I will continue with other components of java swing.


Thanks , useful information
ReplyDelete