JTextField inputFile = new JTextField(20);
JButton chooseInputFile = new JButton("..."); // 添加按钮
chooseInputFile.addActionListener(this);
/**
* 事件监听
* 1.负责监听控件的事件响应,并调用相应处理方法
*/
public void actionPerformed(ActionEvent e)
{
String valueButton = e.getActionCommand(); //按钮值
if(valueButton == "...")
{
//打开窗口程序
jfc.setFileSelectionMode(0);// 设定只能选择到文件
int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
if(state == 1)
{
return;// 撤销则返回
}
else
{
File f = jfc.getSelectedFile();// f为选择到的文件
String fileName = f.getAbsolutePath();
inputFile.setText(fileName);
}
}
}
弹出信息框:JOptionPane.showMessageDialog(null, " 找不到指定文件,请重新选择。 ", "Error", JOptionPane.ERROR_MESSAGE);