8

我正在将我的 Swing 应用程序迁移到 Java 11 以利用 HiDPI 显示支持。我使用的是三星显示器,分辨率设置为 3840x2160,缩放比例为 125%,Windows 10。

尽管 java 9 及更高版本被宣传为可以正确处理 HiDPI 缩放,但在显示简单的 JTable 时,网格线会出现不同的粗细,如下所示:

JTable 网格线问题

这是此的代码:

import javax.swing.*;

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

    public TestTable() {
        JTable table = new JTable(12,6);

        JDialog dialog = new JDialog();
        JScrollPane sp = new JScrollPane(table);

        table.setShowGrid(true);
        table.setRowHeight(25);

        dialog.setContentPane(sp);
        dialog.setSize(300,300);
        dialog.setVisible(true);
        dialog.setLocationRelativeTo(null);
    }
}

但是,在设置 Nimbus L&F 时,问题就消失了:

JTable Nimbus

import javax.swing.*;

public class TestTable {
    public static void main(String[] args) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) { }

        new TestTable();
    }

    public TestTable() {
        JTable table = new JTable(12,6);

        JDialog dialog = new JDialog();
        JScrollPane sp = new JScrollPane(table);

        table.setShowGrid(true);
        table.setRowHeight(25);

        dialog.setContentPane(sp);
        dialog.setSize(300,300);
        dialog.setVisible(true);
        dialog.setLocationRelativeTo(null);
    }
}

如何使用默认的 Windows L&F 实现相同的效果?

(使用 java 9 和 10 观察到相同的行为)

4

1 回答 1

6

不同之处在于两者的外观和感觉如何渲染它们的网格线。

默认外观MetalLookAndFeel(和WindowsLookAndFeel)基于BasicLookAndFeel使用BasicTableUI类来呈现JTable. 在BasicTableUI.paintGrid()中,它调用诸如SwingUtilities2.drawHLine()之类的调用-实际上调用Graphics.fillRect()的是问题所在。

Nimbus 外观使用 SynthTableUI 类。在SynthTableUI.paintGrid()中,它最终确实调用Graphics.drawLine()了 ,这清楚地在缩放下绘制了一条更清晰的线。

正如您所说,这听起来像是 HiDPI 下的主要外观和感觉中的错误。


可以为此创建一个解决方法,尽管它不是特别优雅。

使用正在使用的自定义版本,如果宽度或高度为 1,则Graphics可以覆盖fillRect()以代替使用。可以在绘制表格时专门引入此自定义:drawLine()Graphics

    JTable table = new JTable(12, 6) {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(new GraphicsWorkaround(g));
        }
    };

(匿名子类只是为了简洁起见)。

然后将GraphicsWorkaround类编写为g传入的 true 的包装器。DebugGraphics这里的子类化只是一个技巧,可以省去在所有其他方法中编写委托调用Graphics

import java.awt.Graphics;
import javax.swing.DebugGraphics;

public class GraphicsWorkaround extends DebugGraphics {
    private final Graphics g;

    public GraphicsWorkaround(Graphics g) {
        super(g);
        this.g = g;
    }

    @Override
    public Graphics create() {
        return new GraphicsWorkaround(g.create());
    }

    @Override
    public void fillRect(int x, int y, int width, int height) {
        if (width == 1)
            g.drawLine(x, y, x, y + height - 1);
        else if (height == 1)
            g.drawLine(x, y, x + width - 1, y);
        else
            super.fillRect(x, y, width, height);
    }
}

(该create()方法用于处理在scratchGraphics中创建的内部克隆JComponent.paintComponent())。

drawLine()毕竟,这可以被调用,在 125% 缩放时看起来要好得多。

于 2018-12-07T05:59:15.440 回答