[Java的挥杆] JFileChooser的在Java中
JFileChooser的在Java是一个对象的显示框,您可以打开或保存文件. 是这样的:
现在,我们将继续为开放一个简单的例子和保存文件.
package nguyenvanquan7826.JFileChooser; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** * --------------------- @author nguyenvanquan7826 --------------------- * ------------------ website: cachhoc.net ------------------- * ---------- date: Jul 29, 2014 - filename: DemoJFileChooser.java ---------- */ public class DemoJFileChooser extends JFrame implements ActionListener { public static String iconOpen = "iconOpen.png"; public static String iconSave = "iconSave.png"; private JTextArea taLog; private JButton btnOpen, btnSave; private JFileChooser fc = new JFileChooser(); public DemoJFileChooser() { addContent(); setDisplay(); } /** * set display for JFrame */ private void setDisplay() { setTitle("Demo JFileChooser"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } /** * add content for JFrame, JFrame contain a ButtonPane (contain buttons) and * a JTextArea to show something you work */ private void addContent() { setLayout(new BorderLayout()); add(createButtonPanel(), BorderLayout.PAGE_START); add(new JScrollPane(taLog = createTextArea(15, 30)), BorderLayout.CENTER); } /** * create a JPanel contain 2 button (open and save) on TOP of JFrame */ private JPanel createButtonPanel() { JPanel panel = new JPanel(new FlowLayout()); panel.add(btnOpen = createButton("Open file", iconOpen)); panel.add(btnSave = createButton("Save file", iconSave)); return panel; } /** * create JTextArea */ private JTextArea createTextArea(int row, int col) { JTextArea ta = new JTextArea(row, col); ta.setWrapStyleWord(true); ta.setLineWrap(true); return ta; } /** * create a JButton with text and icon and add Action for it */ private JButton createButton(String text, String iconLink) { ImageIcon icon = createImageIcon(iconLink); JButton btn = new JButton(text, icon); btn.addActionListener(this); return btn; } /** * get a icon via link to icon */ private ImageIcon createImageIcon(String iconLink) { ImageIcon icon = new ImageIcon(getClass().getResource(iconLink)); return icon; } /** * Handle when you open a file */ private void openFile() { int select = fc.showOpenDialog(this); if (select == JFileChooser.APPROVE_OPTION) { taLog.append("You open " + fc.getSelectedFile().getName()); } else { taLog.append("You cancelled open!"); } taLog.append("\n"); } /** * Handle when you save a file */ private void saveFile() { int select = fc.showSaveDialog(this); if (select == JFileChooser.APPROVE_OPTION) { taLog.append("You save " + fc.getSelectedFile().getName()); } else { taLog.append("You cancelled save!"); } taLog.append("\n"); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btnOpen) { openFile(); return; } if (e.getSource() == btnSave) { saveFile(); return; } } public static void main(String[] args) { new DemoJFileChooser(); } }
在上面的代码中,你注意到了 2 显示,以打开和保存文件是不同的.
int select = fc.showOpenDialog(this);
int select = fc.showSaveDialog(this);
下一个参数 JFileChooser.APPROVE_OPTION 表明您已接受 “打开” 或 “保存” 文件.
图标 保存 和 开放 你把在同一个包如下:
上面的例子只是说明如何使用的JFileChooser, 工作 读写文本文件 或 阅读对象 你做离线.
阅读更多: JFileChooser的使用, JFileChooser的类, 的Java Swing
最新评论