当前位置: 技术问答>java相关
两个简单但并不是很多人能够知道答案的问题.
来源: 互联网 发布时间:2015-01-04
本文导语: (1).怎样把程序中所使用的JFrame,JDialog等 窗体左上角的咖啡图标换成自定义图标.(注,要作用于所有同类型的窗体); (2).JFileChooser中,我想选择文件夹,而不是文件,有什么办法吗? | 2 Answer You can ...
(1).怎样把程序中所使用的JFrame,JDialog等 窗体左上角的咖啡图标换成自定义图标.(注,要作用于所有同类型的窗体);
(2).JFileChooser中,我想选择文件夹,而不是文件,有什么办法吗?
(2).JFileChooser中,我想选择文件夹,而不是文件,有什么办法吗?
|
2
Answer
You can use the DIRECTORIES_ONLY option for the setFileSelectionModel method of JFileChooser. For example:
import javax.swing.JFileChooser;
public class ChooseDir {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser ();
jfc.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
System.out.println (jfc.showDialog (null, null));
System.out.println (jfc.getSelectedFile ());
}
}
Answer
You can use the DIRECTORIES_ONLY option for the setFileSelectionModel method of JFileChooser. For example:
import javax.swing.JFileChooser;
public class ChooseDir {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser ();
jfc.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
System.out.println (jfc.showDialog (null, null));
System.out.println (jfc.getSelectedFile ());
}
}
|
(1) JFrame.setIconImage(Image),从这个JFrame弹出来的Dialog有同样的Icon
|
Answer
You can't... at least not directly.
The dialog may not have a title bar in some windowing environments...atleast that has been the given rationale behind the lack of such a feature
Starting JDK 1.1.7 a resizable dialog will inherit the icon of the owner frame. Owner frame is the one passed in the Dialog's CTOR.
If you want to allow a different icon for your dialog from that of your main application frame then you could always create a dummy frame, set the icon on it using the frame.setIconImage(Image); and pass it in the Dialog's CTOR. The dummy frame is never shown.
This has implications in term of -
Modality of the Dialog
Automatic showing/hiding of Dialog when your main application frame is shown/hidden. You can always get around it by listeneing to ComponentEvents on the application frame and showing and hiding the dialog apropriately.