记事本
记事本实例中使用了以下知识
1、ui界面
2、io流
3、事件监听
4、图形
5、JFileChooser 类,用于打开文件,另存为文件。弹出文件选择器对话框
学习重点:JFileChooser,有很多方法,参考下面的方法对照表
//记事本 //记事本实例中使用了以下知识 //1、ui界面 //2、io流 //3、事件监听 //4、图形 //5、JFileChooser 类,用于打开文件,另存为文件。弹出文件选择器对话框 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; public class Index extends JFrame implements ActionListener{ //设置菜单面板 JMenuBar cdmb; //设置一级菜单按钮 JMenu cd1,cd2; //设置二级菜单按钮 JMenuItem cd1_1,cd1_2,cd1_3,cd1_4,cd1_5; //设置文本域 JTextArea wby; //设置滚动条 JScrollPane gdt; //设置欢迎面板 Huanying huanying; public static void main(String[] args) throws Exception{ //实例化当前类 Index index = new Index(); } //自动执行 public Index(){ //设置菜单面板 cdmb = new JMenuBar(); //设置一级菜单按钮名称 cd1 = new JMenu("文件"); cd2 = new JMenu("编辑"); //设置二级菜单按钮名称 cd1_1 = new JMenuItem("新建"); cd1_2 = new JMenuItem("打开"); cd1_3 = new JMenuItem("保存"); cd1_4 = new JMenuItem("另存为"); cd1_5 = new JMenuItem("关闭"); //添加监听事件 cd1_1.addActionListener(this); cd1_1.setActionCommand("xinjian"); cd1_2.addActionListener(this); cd1_2.setActionCommand("dakai"); cd1_3.addActionListener(this); cd1_3.setActionCommand("baocun"); cd1_4.addActionListener(this); cd1_4.setActionCommand("lingcunwei"); //设置文本域 wby = new JTextArea(); //设置滚动条 gdt = new JScrollPane(wby); //设置欢迎面板 huanying = new Huanying(); huanying.setSize(500, 100); //添加二级菜单到一级菜单 cd1.add(cd1_1); cd1.add(cd1_2); cd1.addSeparator(); //添加横线 cd1.add(cd1_3); cd1.add(cd1_4); cd1.addSeparator(); //添加横线 cd1.add(cd1_5); //添加一级菜单到菜单面板 cdmb.add(cd1); cdmb.add(cd2); //添加面板到窗口 this.setJMenuBar(cdmb); this.add(gdt); this.add(huanying,BorderLayout.SOUTH); //设置窗口标题 this.setTitle("记事本"); //设置窗口的宽高 this.setSize(500,350); //添加标题栏图片 this.setIconImage((new ImageIcon("image/1.jpg")).getImage()); //设置窗口出现对于屏幕的位置 this.setLocation(100,100); //禁止拉大拉小 //this.setResizable(false); //关闭窗口后释放资源 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //输出窗口 this.setVisible(true); } public void actionPerformed(ActionEvent e){ /** * 新建文件 */ if(e.getActionCommand().equals("xinjian")){ System.out.println("新建文件"); } /** * 打开文件 */ if(e.getActionCommand().equals("dakai")){ System.out.println("打开文件"); //打开文件的窗口 JFileChooser wjxz = new JFileChooser(); //创建文件窗口的对象 wjxz.setDialogTitle("打开文件"); //设置文件窗口标题 wjxz.showOpenDialog(null); //打开文件窗口风格默认 *******,跟另存为只是这里有区别 wjxz.setVisible(true); //显示文件窗口 //得到用户选择的文件全路径 String wjlj = wjxz.getSelectedFile().getAbsolutePath(); FileReader wjl = null; //文件流 BufferedReader hcl = null; //缓冲流 try{ wjl = new FileReader(wjlj); //文件流方式打开文件 hcl = new BufferedReader(wjl); //缓冲流方式缓冲文件 String s = hcl.readLine(); //读取一行 String content = ""; while(s!=null){ content += (s+"\n"); s = hcl.readLine(); //继续读取一行 } wby.setText(content); } catch(Exception aa){ } finally{ try{ //关闭流,文件 wjl.close(); hcl.close(); } catch(Exception bb){ } } } /** * 另存为文件 */ if(e.getActionCommand().equals("lingcunwei")){ System.out.println("另存为文件"); //另存为文件的窗口 JFileChooser wjxz = new JFileChooser(); //创建文件窗口的对象 wjxz.setDialogTitle("另存为文件"); //设置文件窗口标题 wjxz.showSaveDialog(null); //另存为文件窗口风格默认 wjxz.setVisible(true); //显示文件窗口 //得到用户选择的文件全路径 String bclj = wjxz.getSelectedFile().getAbsolutePath(); try{ //保存文件 PrintStream pl = new PrintStream(bclj); System.setOut(pl); System.out.println(this.wby.getText()); pl.close(); } catch(Exception aa){ } } } } class Huanying extends JPanel{ public void paint(Graphics g){ //设置颜色 g.setColor(Color.red); //文字 g.setFont(new Font("宋体",Font.BOLD,10)); g.drawString("您好,欢迎使用记事本软件", 10, 10); } }
JFileChooser方法对照表(从手册上复制过来的)
方法摘要 | |
---|---|
boolean | accept(File f) 如果应该显示该文件,则返回 true。 |
void | addActionListener(ActionListener l) 向文件选择器添加一个 ActionListener 。 |
void | addChoosableFileFilter(FileFilter filter) 向用户可选择的文件过滤器列表添加一个过滤器。 |
void | approveSelection() 用户单击 Approve 按钮(默认情况下标有 "Open" 或 "Save")时由 UI 调用此方法。 |
void | cancelSelection() 用户选择 Cancel 按钮时由 UI 调用此方法。 |
void | changeToParentDirectory() 将要设置的目录更改为当前目录的父级。 |
protected JDialog | createDialog(Component parent) 创建并返回包含 this 的新 JDialog ,在 parent 窗体中的 parent 上居中。 |
void | ensureFileIsVisible(File f) 确保指定的文件是可见的,不是隐藏的。 |
protected void | fireActionPerformed(String command) 通知对此事件类型感兴趣的所有侦听器。 |
FileFilter | getAcceptAllFileFilter() 返回 AcceptAll 文件过滤器。 |
AccessibleContext | getAccessibleContext() 获取与此 JFileChooser 关联的 AccessibleContext。 |
JComponent | getAccessory() 返回 accessory 组件。 |
ActionListener[] | getActionListeners() 返回在此文件选择器上注册的所有操作侦听器的数组。 |
int | getApproveButtonMnemonic() 返回确认按钮的助记符。 |
String | getApproveButtonText() 返回 ApproveButton 中的 FileChooserUI 内使用的文本。 |
String | getApproveButtonToolTipText() 返回 ApproveButton 中使用的工具提示文本。 |
FileFilter[] | getChoosableFileFilters() 获得用户可选择的文件过滤器列表。 |
boolean | getControlButtonsAreShown() 返回 controlButtonsAreShown 属性的值。 |
File | getCurrentDirectory() 返回当前目录。 |
String | getDescription(File f) 返回文件描述。 |
String | getDialogTitle() 获得 JFileChooser 的标题栏中所显示的字符串。 |
int | getDialogType() 返回此对话框的类型。 |
boolean | getDragEnabled() 获得 dragEnabled 属性的值。 |
FileFilter | getFileFilter() 返回当前选择的文件过滤器。 |
int | getFileSelectionMode() 返回当前的文件选择模式。 |
FileSystemView | getFileSystemView() 返回文件系统视图。 |
FileView | getFileView() 返回当前的文件视图。 |
Icon | getIcon(File f) 返回此文件或文件类型的图标,这取决于系统。 |
String | getName(File f) 返回文件名。 |
File | getSelectedFile() 返回选中的文件。 |
File[] | getSelectedFiles() 如果将文件选择器设置为允许选择多个文件,则返回选中文件的列表。 |
String | getTypeDescription(File f) 返回文件类型。 |
FileChooserUI | getUI() 获得实现此组件 L&F 的 UI 对象。 |
String | getUIClassID() 返回一个指示 L&F 类名的字符串,该类负责呈现此组件。 |
boolean | isAcceptAllFileFilterUsed() 返回是否使用 AcceptAll FileFilter 。 |
boolean | isDirectorySelectionEnabled() 方便的调用,可根据当前的文件选择模式确定目录是否为可选择的。 |
boolean | isFileHidingEnabled() 如果在文件选择器中不显示隐藏文件,则返回 true;否则返回 false。 |
boolean | isFileSelectionEnabled() 方便的调用,可根据当前的文件选择模式确定文件是否为可选择的。 |
boolean | isMultiSelectionEnabled() 如果可以选择多个文件,则返回 true。 |
boolean | isTraversable(File f) 如果可以返回该文件(目录),则返回 true。 |
protected String | paramString() 返回此 JFileChooser 的字符串表示形式。 |
void | removeActionListener(ActionListener l) 从文件选择器中移除一个 ActionListener 。 |
boolean | removeChoosableFileFilter(FileFilter f) 从用户可选择的文件过滤器列表中移除一个过滤器。 |
void | rescanCurrentDirectory() 通知 UI 重新扫描当前目录的文件列表。 |
void | resetChoosableFileFilters() 将可选择文件过滤器列表重置为其开始状态。 |
void | setAcceptAllFileFilterUsed(boolean b) 确定是否将 AcceptAll FileFilter 用作可选择过滤器列表中一个可用选项。 |
void | setAccessory(JComponent newAccessory) 设置 accessory 组件。 |
void | setApproveButtonMnemonic(char mnemonic) 使用字符设置确认按钮的助记符。 |
void | setApproveButtonMnemonic(int mnemonic) 使用数值键代码设置确认按钮的助记符。 |
void | setApproveButtonText(String approveButtonText) 设置 FileChooserUI 中的 ApproveButton 内使用的文本。 |
void | setApproveButtonToolTipText(String toolTipText) 设置 ApproveButton 中使用的工具提示文本。 |
void | setControlButtonsAreShown(boolean b) 设置属性,指示在文件选择器中是否显示 approve 和 cancel 按钮。 |
void | setCurrentDirectory(File dir) 设置当前目录。 |
void | setDialogTitle(String dialogTitle) 设置显示在 JFileChooser 窗口标题栏的字符串。 |
void | setDialogType(int dialogType) 设置此对话框的类型。 |
void | setDragEnabled(boolean b) 设置 dragEnabled 属性,要在此组件上启用自动拖动处理(drag 和 drop 的第一部分),此属性必须为 true 。 |
void | setFileFilter(FileFilter filter) 设置当前文件过滤器。 |
void | setFileHidingEnabled(boolean b) 设置是否实现文件隐藏。 |
void | setFileSelectionMode(int mode) 设置 JFileChooser ,以允许用户只选择文件、只选择目录,或者可选择文件和目录。 |
void | setFileSystemView(FileSystemView fsv) 设置为访问和创建文件系统资源(如查找软驱和获得根驱动器列表), JFileChooser 所使用的文件系统视图。 |
void | setFileView(FileView fileView) 设置用于检索 UI 信息的文件视图,如表示文件的图标或文件的类型描述。 |
void | setMultiSelectionEnabled(boolean b) 设置文件选择器,以允许选择多个文件。 |
void | setSelectedFile(File file) 设置选中的文件。 |
void | setSelectedFiles(File[] selectedFiles) 如果将文件选择器设置为允许选择多个文件,则设置选中文件的列表。 |
protected void | setup(FileSystemView view) 执行公共的构造方法初始化和设置。 |
int | showDialog(Component parent, String approveButtonText) 弹出具有自定义 approve 按钮的自定义文件选择器对话框。 |
int | showOpenDialog(Component parent) 弹出一个 "Open File" 文件选择器对话框。 |
int | showSaveDialog(Component parent) 弹出一个 "Save File" 文件选择器对话框。 |
void | updateUI() 将 UI 属性重置为当前的外观值。 |