问几个个低级的问题!送100分
1.看看先面这段代码:
JFileChooser fileChooser=new JFileChooser();
fileChooser.setSelectionMode(JFileChooser.FILES_ONLY);
int result=fileChooser.showOpenDialog();
if(result==JFileChooser.CANCEL_OPTION) return;
File fileName=fileChooser.getSelectedFile();
if(fileName==null||fileName.getName().equals(""))//??????????
JOptionPane.showMessageDialog(null,"Invalid file name",
"Invalid file name",JOptionPane.ERROR_MESSAGE);
我打问号的那句不理解?
选择文件的时候要不就什么文件也没有选中,要不文件应该有名字的.可是它来这句就不明白了,
fileName.getName().equals(""),它是应对哪种情况的呢?
2.经常看到在程序中有validate方法,但是不知道这个函数是干什么用的?
3.还有就是方法pack是干什么用的?
这些是我在看书的时候遇到的疑问,API看的不是很懂,因为我很菜,希望大家不要见笑.
问题点数:100、回复次数:11Top
1 楼raul_177(风之子)回复于 2005-09-03 19:52:49 得分 20
My answer to Q3:
//这是在java文档上找到的答案
public void pack()
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.
pack: 根据容器内的组件以最优的方式调整容器的大小.
Top
2 楼raul_177(风之子)回复于 2005-09-03 19:55:20 得分 20
My answer to Q2:
public void validate()
Validates this container and all of its subcomponents.
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
validate()方法用于布局的重新排列,使你的布局生效,注意与revalidate()方法区别:validate()只更新组件本身,而revalidate()则影响了它和它的子容器、子组件。
Top
3 楼raul_177(风之子)回复于 2005-09-03 20:04:10 得分 10
My answer to Q1:
fileName.getName.equals("")应该时应对你什么文件都不选的情况吧//Not sure
Top
4 楼nkat(弥迷谜)回复于 2005-09-03 21:54:10 得分 5
文件名为空呀,这有什么不能理解的Top
5 楼simon0512(虫虫)回复于 2005-09-03 23:09:54 得分 5
文件名为空Top
6 楼limitmx(limit....)回复于 2005-09-03 23:15:34 得分 5
文件名为空是个啥文件呢?Top
7 楼congliu(取次花丛都不顾,半缘修道半缘君。)回复于 2005-09-04 08:38:14 得分 5
文件名为空就创建不成功Top
8 楼jblz(jblz)回复于 2005-09-04 09:08:03 得分 5
upTop
9 楼chinatelly(atelly)回复于 2005-09-04 17:55:52 得分 25
if(fileName==null//这应该是文件不存在
||fileName.getName().equals(""))//这应该是文件名不存在
Top
10 楼wcj1981(宇宙之神)回复于 2005-09-04 18:25:22 得分 0
谢谢楼上的各位解答!
但是文件名为空,这样子的文件能存在吗?
当然第一个指的是没有选择任何的文件,那么这个时候返回的是空句柄.
但是第二个就不知道是怎么回事了,怎么操作会出现这种情况呢?
Top
11 楼wcj1981(宇宙之神)回复于 2005-09-05 21:09:41 得分 0
能够解释的详细些吗?
谢谢了!Top




