【问题标题】:Java - How can i increase the font of the folder names in JFileChooser?Java - 如何增加 JFileChooser 中文件夹名称的字体?
【发布时间】:2018-01-29 04:43:37
【问题描述】:

在高分辨率 4k 屏幕上运行我的 Java 程序时,JFileChooser 中文件夹名称的字体看起来很小,如下所示:

我试图找到一种方法来增加仅在 JFileChooser 中的文件夹/文件名字体大小。我目前的想法是创建一个自定义 JFileChooser,循环其元素并尝试增加文件夹名称的字体。我以为我会增加 FilePane 的字体,但它不起作用。什么都没发生。这是我的代码:

public class JFileChooserCustom extends JFileChooser {
    public JFileChooserCustom() {
        setFileChooserFont( this.getComponents() );
    }

    public void setFileChooserFont( Component[] comp ) {
        for( int x = 0; x < comp.length; x++ ) {
          // System.out.println( comp[x].toString() );  // Trying to know the type of each element in the JFileChooser.
          if(comp[x] instanceof Container) setFileChooserFont(((Container)comp[x]).getComponents());

          try{
              if(comp[x] instanceof FilePane) comp[x].setFont( comp[x].getFont().deriveFont( comp[x].getFont().getSize() * 2f ) );
          }
          catch(Exception e){}//do nothing
        }
    }
}

我希望有人可以帮助我。

【问题讨论】:

  • 另请参阅 File Browser GUI - 如果您决定放弃 JFileChooser 并替换它。
  • @Andrew Thompson ...谢谢,但它似乎非常先进。我不需要这么复杂的选择器:)

标签: java swing font-size jfilechooser


【解决方案1】:

不要使用 FilePane,而是使用 JList 和 JTable(FilePane 使用这些组件来呈现文件列表)。

import java.awt.Component;
import java.awt.Container;

import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

/**
 * <code>IncreaseFileChooserFont</code>.
 */
public class IncreaseFileChooserFont {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFileChooser chooser = new JFileChooser();
                setFileChooserFont(chooser.getComponents());
                chooser.showOpenDialog(null);
                System.exit(0);
            }
        });
    }

    public static void setFileChooserFont(Component[] comp) {
        for (int x = 0; x < comp.length; x++) {
            // System.out.println( comp[x].toString() ); // Trying to know the type of each element in the JFileChooser.
            if (comp[x] instanceof Container)
                setFileChooserFont(((Container) comp[x]).getComponents());

            try {
                if (comp[x] instanceof JList || comp[x] instanceof JTable)
                    comp[x].setFont(comp[x].getFont().deriveFont(comp[x].getFont().getSize() * 2f));
            } catch (Exception e) {
            } // do nothing
        }
    }
}

【讨论】:

    【解决方案2】:

    您可以通过单独设置 JFilechooser 的每个组件的字体来做到这一点。

    // Inside method where you are preparing your JFilechooser:  
    JFileChooser fc = new JFileChooser(".");  
    setFileChooserFont(fc.getComponents());  
    
    public void setFileChooserFont(Component[] comp)
    {
        for(int x = 0; x < comp.length; x++)
        {
          if(comp[x] instanceof Container) setFileChooserFont(((Container)comp[x]).getComponents());
          try{comp[x].setFont(font);}
          catch(Exception e){}//do nothing
        }
    }
    

    希望这会有所帮助。 :-)

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      相关资源
      最近更新 更多