greatghoul
Ask and Learn

Ask and Learn

Swing无边窗口的两种实现

Photo by Sigmund on Unsplash

Swing无边窗口的两种实现

greatghoul's photo
greatghoul
·Apr 13, 2021·

1 min read

在使用 swing 编写java桌面应用的时候,有时会需要使用无边窗口,比如 SplashWindow

这里介绍两种无边窗口的实现方法:

方法1:使用JFrame,并隐藏标题和边框

JFrame 在隐藏标题后可以模拟出无边的效果,这里会用到 JFramesetUndecorated() 方法。

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class UndecoratedFrame extends JFrame {
    public static void main(String[] args) {
        UndecoratedFrame test = new UndecoratedFrame();

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
        test.add(panel);

        test.setSize(600, 400);
        test.setDefaultCloseOperation(EXIT_ON_CLOSE);
        test.setLocationRelativeTo(null);
        test.setUndecorated(true); // 必须在窗口显示前调用
        test.setVisible(true);
    }
}

方法2:直接使用 JWindow 构建无边窗口

JWindow 本来就没有标题栏和边框,拿来直接用就好了。

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JWindow;
import javax.swing.JPanel;

public class UndecoratedWindow extends JWindow {
    public static void main(String[] args) {
        UndecoratedWindow test = new UndecoratedWindow();

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
        test.add(panel);

        test.setSize(600, 400);
        test.setLocationRelativeTo(null);
        test.setVisible(true);
    }
}
 
Share this