我有一个使用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截图:

java - jtree高DPI无法缩放-LMLPHP

UHD分辨率jtree截图:

java - jtree高DPI无法缩放-LMLPHP

谢谢。

最佳答案

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


  公共无效setRowHeight(int rowHeight)
  
  设置每个单元格的高度,以像素为单位。如果指定的值小于或等于零,则查询当前单元格渲染器的每一行高度。


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/

10-12 05:57