如何增加JFileChooser的大小?

时间:2012-01-25 02:27:18

标签: java swing

我正在编写一个需要在屏幕分辨率非常高的设备上运行的Java应用程序。我需要显示的唯一UI组件是JFileChooser。

由于屏幕分辨率如此之高,FileChooser显得太小。是否有一个简单的命令我可以使它变大?理想情况下,我希望保持组件的比例相同,以便图标的增长与文本一样多。

此外,任何更改都只能修改我的应用程序。改变图形大小的全局方法,例如使用较低分辨率或更改系统范围的字体大小,对我来说不是一种选择。

有什么想法吗?

4 个答案:

答案 0 :(得分:2)

我知道答案。只需使用chooser.setPreferredSize(new Dimension(int width,int height));方法,其中选择器是您的JFileChooser。

示例:

public class MyFrame extends JFrame(){
  JFileChooser chooser = new JFileChooser();
  chooser.setPreferredSize(new Dimension(800,600));
  //Here show your dialog and do the rest
}

答案 1 :(得分:2)

这个类工作正常,可以调整JFileChooser窗口和字体的大小。

public class JFileChooserArqs  {

    private Font font = new Font("monospaced",Font.BOLD,16);

    private String fileName;

    public JFileChooserArqs(String title)
    {  
      fileName = null;
      JFileChooser  fc = new JFileChooser(".");
      fc.setPreferredSize(new Dimension(800,600));
      fc.setDialogTitle(title);
      setFileChooserFont(fc.getComponents());  
      int returnVal = fc.showOpenDialog(null);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
          fileName = fc.getSelectedFile().getAbsolutePath();
      }
    }  

    private 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  
      }  
    }  

    public String obtemNomeArquivo() {
        return fileName;
    }
}

答案 2 :(得分:1)

您需要选择合适的layouts来设计用户界面。看一下CodeRanch主题。

答案 3 :(得分:1)

我打算建议将JFileChooser添加到具有合适布局的容器中,如@AVD建议的那样。例如,ImageDisplay将选择器添加到BorderLayout.WEST,在此处可以自由地垂直增长,同时采用UI委托的首选宽度。在放弃这种方法之前,请确认您没有无意中击败该设计功能。

如果确实需要控制选择器子组件的显示特性,您可能需要查看FileBrowser的这些变体。

相关问题