[英]How to increase the size of a JFileChooser?
我正在编写一个需要在屏幕分辨率非常高的设备上运行的Java应用程序。 我需要显示的唯一UI组件是JFileChooser。
由于屏幕分辨率如此之高,FileChooser显得太小。 是否有一个简单的命令我可以使它变大? 理想情况下,我希望保持组件的比例相同,以便图标的增长与文本一样多。
此外,任何更改都只能修改我的应用程序。 改变图形大小的全局方法,如使用较低分辨率或更改系统范围的字体大小,对我来说不是一个选择。
有任何想法吗?
我知道答案。 只需使用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
}
这个类工作正常,都可以调整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;
}
}
我打算建议将JFileChooser
添加到具有合适布局的容器中,正如@AVD建议的那样。 例如, ImageDisplay
将选择器添加到BorderLayout.WEST
,它可以在采用UI委托的首选宽度时自由垂直增长。 在放弃该方法之前,请确认您没有无意中击败该设计功能。
如果您确实需要控制选择器子组件的显示特性,您可能需要查看FileBrowser
这些变体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.