如何自定义一个带有圆角、没有标题、没有控制按钮的 JDialog

o.o*_*o.o 0 java swing

如何自定义JDialog圆角、没有标题、没有控制按钮的 a。就像下面这个一样。

所需的 JDialog

Mad*_*mer 7

与生活中的大多数事情一样,这是一种幻觉。

你其实问了三个问题...

  1. 如何去除窗户装饰品
  2. 如何让窗口透明
  3. 如何绘制“圆角”矩形

所有的事情都相对容易做,知道你需要它们,那是另一个问题。

所以,对于前两个...

JDialog dialog = new JDialog();
dialog.setUndecorated(true);
dialog.setBackground(new Color(0, 0, 0, 0));
dialog.setModal(true);
dialog.setSize(640, 480);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

您需要删除窗口装饰并将背景设置为透明颜色。如果你运行上面的代码,你会发现,实际上没什么,有点诡异。那是因为窗户没有装饰并且是透明的。

下一步将需要一个自定义组件。再次强调,这是一种幻觉。我们将使组件透明,但是,我们将手动填充它。

public class RoundedPane extends JPanel {

    private int radius = 20;
    
    public RoundedPane() {
        setOpaque(false);
        setBorder(new EmptyBorder(10, 10, 10, 10));
        setLayout(new BorderLayout());
    }

    public void setRadius(int radius) {
        this.radius = radius;
        setBorder(new EmptyBorder(radius / 2, radius / 2, radius / 2, radius / 2));
        revalidate();
        repaint();
    }

    public int getRadius() {
        return radius;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(getBackground());
        g2.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getRadius(), getRadius());
        g2.setColor(getForeground());
        g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getRadius(), getRadius());
        super.paintComponent(g);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,简单地说,这将创建一个JPanel,使其透明 ( setOpaque(false)),然后覆盖它,paintComponent以便我们可以绘制圆形效果。

现在,如果你把它放在一起,你最终会得到看起来像这样的东西......

如此圆润的

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JDialog dialog = new JDialog();
                dialog.setUndecorated(true);
                dialog.setBackground(new Color(0, 0, 0, 0));
                dialog.setModal(true);
                dialog.setContentPane(new RoundedPane());
                dialog.setSize(640, 480);
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }
        });
    }

    public class RoundedPane extends JPanel {

        private int radius = 20;

        public RoundedPane() {
            setOpaque(false);
            setBorder(new EmptyBorder(10, 10, 10, 10));
            setLayout(new BorderLayout());
        }

        public void setRadius(int radius) {
            this.radius = radius;
            setBorder(new EmptyBorder(radius / 2, radius / 2, radius / 2, radius / 2));
            revalidate();
            repaint();
        }

        public int getRadius() {
            return radius;
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(getBackground());
            g2.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getRadius(), getRadius());
            g2.setColor(getForeground());
            g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getRadius(), getRadius());
            super.paintComponent(g);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,关于上述示例的一些直接说明。

  • setSize特意使用了这个例子。应该RoundedPane推迟根据 及其Border内容计算所需的大小,因此我们不想修改它,但由于它会给出首选大小0x0,所以我自己设置对话框的大小。你应该依靠pack才能获得更好的效果。
  • 我已经设置RoundedPane为对话框的contentPane。这将允许您继续像平常一样直接向对话框添加内容。您可以直接向 中添加内容RoundedPane,但这也确保现有内容contentPane不会干扰我们的工作。

您还应该看看:

这样我发现这个未修饰的所有字体类型都发生了变化,我如何在整个对话框中设置字体类型。

字体由安装的外观管理。如果您确实遇到字体问题,那么您可能做错了什么,这是特定于平台的问题(即错误)

你所有的字体