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看的不是很懂,因为我很菜,希望大家不要见笑.
JOptionPane.showMessageDialog(null,"Invalid file name",
"Invalid file name",JOptionPane.ERROR_MESSAGE);我打问号的那句不理解?选择文件的时候要不就什么文件也没有选中,要不文件应该有名字的.可是它来这句就不明白了,fileName.getName().equals(""),它是应对哪种情况的呢?2.经常看到在程序中有validate方法,但是不知道这个函数是干什么用的?3.还有就是方法pack是干什么用的?
这些是我在看书的时候遇到的疑问,API看的不是很懂,因为我很菜,希望大家不要见笑.
//这是在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: 根据容器内的组件以最优的方式调整容器的大小.
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()则影响了它和它的子容器、子组件。
fileName.getName.equals("")应该时应对你什么文件都不选的情况吧//Not sure
||fileName.getName().equals(""))//这应该是文件名不存在