📄 filenamepathbean.java
字号:
package readfileprocessbarbean;import java.awt.*;import javax.swing.*;import javax.swing.JLabel;import java.beans.*;/** * 这是一个返回文件名和文件路径的JavaBean */public class FileNamePathBean extends JLabel{ BorderLayout borderLayout1 = new BorderLayout(); private String fileNameAndPath; // 文件名和文件路径 /* 实例化一个PropertyChangeSupport的局部对象 */ private PropertyChangeSupport changes = new PropertyChangeSupport(this); public FileNamePathBean() { try { jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(borderLayout1); } public void setFileNameAndPath() { /* 设计器的方法,其中引用关联属性 */ String oldfileNameAndPath = this.fileNameAndPath; // 定义一个变量,用以存放原来的属性值 String newfileNameAndPath = null; // 定义一个变量,用以存放当前的属性值 /* 调用showFileDialogWindows()方法来设置新的文件路径和文件名 */ newfileNameAndPath = this.showFileDialogWindows(); this.fileNameAndPath = newfileNameAndPath; changes.firePropertyChange("fileNameAndPath",oldfileNameAndPath,newfileNameAndPath); } public String getFileNameAndPath() { /* 获取单值属性的值 */ return fileNameAndPath; } private String showFileDialogWindows() { /* 这是一个显示文件浏览器的方法 */ String getTabSource = null; String returnStr = null; JFileChooser jfc = new JFileChooser(); jfc.showOpenDialog(null); getTabSource = jfc.getSelectedFile().toString(); if(getTabSource.length() > 1 && getTabSource != null) { returnStr = getTabSource.trim(); } else { returnStr = "unknown"; } return returnStr; } public void addPropertyChangeListener(PropertyChangeListener l) { /* 注册属性监听方法 */ try {changes.addPropertyChangeListener(l);} catch(Exception e) {} } public void removePropertyChangeListener(PropertyChangeListener l) { /* 删除属性监听方法 */ try {changes.removePropertyChangeListener(l);} catch(Exception e) {} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -