gpt4 book ai didi

java - jtree 高 DPI 不缩放

转载 作者:行者123 更新时间:2023-11-30 08:30:27 24 4
gpt4 key购买 nike

我有一个使用 JTree 的 java swing 应用程序。
我最近换到了 UHD 显示器,现在 JTree 不能很好地缩放。
我感觉它使用图标来缩放行高,但我自己不知道如何影响它。
请注意,如果 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 被遗漏,它在新显示器上小得难以阅读。
请查看下面的屏幕截图/简单示例代码。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTree;
import javax.swing.UIManager;

public class JTreeTest extends JFrame
{

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
JTreeTest frame = new JTreeTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public JTreeTest()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JTree tree = new JTree();
contentPane.add(tree, BorderLayout.CENTER);
}

}

普通分辨率 jtree 截图:

/image/n4fUe.png

UHD 分辨率 jtree 屏幕截图:

/image/hvXMf.png

谢谢。

最佳答案

也许你需要使用 JTree#setRowHeight(int)

public void setRowHeight(int rowHeight)

Sets the height of each cell, in pixels. If the specified value is less than or equal to zero the current cell renderer is queried for each row's height.

import java.awt.*;
import javax.swing.*;

public class JTreeTest2 extends JFrame
{
private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JTreeTest2();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public JTreeTest2()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JTree tree = new JTree();
tree.setFont(tree.getFont().deriveFont(32f));

JTree tree2 = new JTree();
tree2.setFont(tree2.getFont().deriveFont(32f));
tree2.setRowHeight(32);
//or: tree2.setRowHeight(0);

JPanel p = new JPanel(new GridLayout(1, 0));
p.add(new JScrollPane(tree));
p.add(new JScrollPane(tree2));

contentPane.add(p, BorderLayout.CENTER);
setBounds(100, 100, 450, 300);
}
}

关于java - jtree 高 DPI 不缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41200454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com