3

我注意到TitledBorder具有原生外观的 Windows 上的 Java 8(及更早版本)和 Java 9(及更高版本)的外观有所不同。从 Java 9 开始,边框变暗并且没有圆角。尤其是嵌套TitledBorder的,这看起来令人不安。有没有办法使用 Java 9 并像 Java 8 一样绘制边框?

Java 8 与 Java 9

MWE:

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

public class TitledBorderWithJava9 {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                //...
            }
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(150, 100);
            JPanel panel = new JPanel();
            frame.add(panel);
            
            TitledBorder border = BorderFactory.createTitledBorder("Title");
            panel.setBorder(border);
            
            frame.setVisible(true); 
        });
    }
}
4

1 回答 1

0

根据对问题的评论,以下方法接近预期的结果(颜色和尺寸是正确的,但我没有设法像在 Java 8 中那样拥有圆角)。

Border baseBorderOuter = BorderFactory.createLineBorder(new Color(213, 223, 229), 1, true);
Border baseBorderInner = BorderFactory.createLineBorder(Color.WHITE, 1, true);
Border baseBorder = BorderFactory.createCompoundBorder(baseBorderOuter, baseBorderInner);

TitledBorder border = BorderFactory.createTitledBorder(baseBorder, "Title");
panel.setBorder(border);
于 2021-07-28T15:32:37.137 回答