10

I have a login JFrame for my application and i would like to make the corners of the frame rounded by a few pixles.

Id like to do this without using transparency on the JFrame and having to use a background image inside a JPanel - is this possible?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Alosyius
  • 8,771
  • 26
  • 76
  • 120

2 Answers2

18

It's possible with undecorated frame, consider the following example:

JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setShape(new RoundRectangle2D.Double(10, 10, 100, 100, 50, 50));
frame.setSize(300, 200);
frame.setVisible(true);

This code works on Java 7. For Java 6 (since update 10) you can do the same with AWTUtilities.setWindowShape:

JFrame frame = new JFrame();
frame.setUndecorated(true);
AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(10, 10, 100, 100, 50, 50));
frame.setSize(300, 200);
frame.setVisible(true);
Jk1
  • 11,233
  • 9
  • 54
  • 64
10

Try this. It works :)

yourframe.setUndecorated(true);
yourframe.setBackground(new Color(0, 0, 0, 180));
yourframe.addComponentListener(new ComponentAdapter() {
               @Override
                public void componentResized(ComponentEvent e) {
                    setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 80, 80));
                }
            });
Harsha Basnayake
  • 4,565
  • 3
  • 17
  • 23