javaswingmavenuser-interfaceawtutilities

java - how to resolve AWTUtilities error in maven GUI project?


I writing a gui program in java, my project kind is maven. I want to use AWTUtilities library to create JFrame with rounded corners, but when i trying to run the project, i accept the following errors:

  1. Error:(3, 19) java: package com.sun.awt does not exist.
  2. Error:(35, 9) java: cannot find symbol symbol: variable AWTUtilities location: class Test.

The code snippet:

import com.sun.awt.AWTUtilities;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

public class Test extends JFrame{
    private static Robot robot;

    public static void main(String[] args) {
        try {
            robot = new Robot();
        } catch (AWTException e1) {
            e1.printStackTrace();
        }

        JFrame win = new JFrame();
        win.setSize(700, 100);
        JPanel panel = new JPanel();
        JButton button = new JButton("simulate");
        final JTextField textField = new JTextField();
        textField.setPreferredSize(new Dimension(600, 30));
        panel.add(textField);
        panel.add(button);
        win.add(panel);

        Shape shape = new Rectangle2D.Double(0, 0, win.getWidth(), win.getHeight());
        AWTUtilities.setWindowShape(win, shape);

        win.setVisible(true);
    }
}

How can i resolve it?


Solution

  • The code you wrote works only in Java 6 and under certain conditions. If you are using Java 8 and you want to create a Frame with rounded corners, try something like this:

    JFrame frame = new JFrame();
    frame.setUndecorated(true);
    frame.setBackground(new Color(0, 0, 0, 180));
    frame.setShape(new RoundRectangle2D.Double(0, 0, 500, 600, 80, 80));