📄 jfilechooserframe.java~34~
字号:
package jfilechooserexample;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JFileChooser;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class JFileChooserFrame extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JPanel jPanel2 = new JPanel();
JTextArea jTextArea1 = new JTextArea();
BorderLayout borderLayout2 = new BorderLayout();
JFileChooser jFileChooser1 = new JFileChooser("d:\\");
JButton jButton2 = new JButton();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JFileChooser jFileChooser2 = new JFileChooser("d:\\");
public JFileChooserFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
this.setJMenuBar(jMenuBar1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jButton1.setText("打开");
jButton1.addActionListener(new JFileChooserFrame_jButton1_actionAdapter(this));
jPanel2.setLayout(borderLayout2);
jButton2.setText("保存");
jButton2.addActionListener(new JFileChooserFrame_jButton2_actionAdapter(this));
jMenu1.setText("文件");
jMenuItem1.setText("打开");
jMenuItem1.addActionListener(new
JFileChooserFrame_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("保存");
jPanel1.add(jButton1);
jPanel1.add(jButton2);
contentPane.add(jPanel2, java.awt.BorderLayout.CENTER);
contentPane.add(jPanel1, java.awt.BorderLayout.NORTH);
jPanel2.add(jTextArea1, java.awt.BorderLayout.CENTER);
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
}
public void jButton1_actionPerformed(ActionEvent e) {
jFileChooser1.setApproveButtonText("aaa"); //确定按钮
jFileChooser1.setDialogTitle("打开文件");
//设置过滤文件
//自定义类JAVAFileFiler
jFileChooser1.addChoosableFileFilter(new JAVAFileFilter("java"));
jFileChooser1.addChoosableFileFilter(new JAVAFileFilter("txt"));
//设置图片
jFileChooser1.setFileView(new FileIcon());
int result = jFileChooser1.showOpenDialog(this);
File file = null;
if (result == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(this, "没有选择文件!");
}
if (result == JFileChooser.APPROVE_OPTION) {
file = jFileChooser1.getSelectedFile();
JOptionPane.showMessageDialog(this, "你选择的文件名" + file.getName());
}
// FileInputStream fileInputStream=null;
FileReader fileInputStream = null;
BufferedReader bu = null;
if (file != null) {
try {
// fileInputStream = new FileInputStream(file);
fileInputStream = new FileReader(file);
bu = new BufferedReader(fileInputStream);
} catch (FileNotFoundException ex) {
}
int readbyte;
String readstr = null;
try {
// while ((readbyte = fileInputStream.read()) != -1) {
// jTextArea1.append(String.valueOf((char)readbyte));
// }
while ((readstr = bu.readLine()) != null) {
jTextArea1.append(readstr);
jTextArea1.append("\n");
}
} catch (IOException ex1) {
JOptionPane.showMessageDialog(this, "文件错误!");
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException ioe2) {
}
}
}
}
public void jButton2_actionPerformed(ActionEvent e) {
int result = jFileChooser1.showSaveDialog(this);
File file = null;
FileWriter fw = null;
BufferedWriter buw = null;
if (result == jFileChooser1.APPROVE_OPTION) {
file = jFileChooser1.getSelectedFile();
JOptionPane.showMessageDialog(this, "文件名:" + file, "有情提示",
JOptionPane.DEFAULT_OPTION);
}
if (result == jFileChooser1.CANCEL_OPTION) {
JOptionPane.showMessageDialog(this, "没有文件名");
}
if (file != null) {
try {
fw = new FileWriter(file);
buw = new BufferedWriter(fw);
} catch (IOException ex) {
}
String content=jTextArea1.getText();
System.out.println(content);
try {
buw.write(content);
buw.flush();
} catch (IOException ex1) {
}
finally
{
try {
if (fw != null) {
buw.close();
fw.close();
}
} catch (Exception ex2) {
}
}
}
}
public void jMenuItem1_actionPerformed(ActionEvent e) {
jFileChooser2.showOpenDialog(this);
}
}
class JFileChooserFrame_jMenuItem1_actionAdapter implements ActionListener {
private JFileChooserFrame adaptee;
JFileChooserFrame_jMenuItem1_actionAdapter(JFileChooserFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}
class JFileChooserFrame_jButton2_actionAdapter implements ActionListener {
private JFileChooserFrame adaptee;
JFileChooserFrame_jButton2_actionAdapter(JFileChooserFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class JFileChooserFrame_jButton1_actionAdapter implements ActionListener {
private JFileChooserFrame adaptee;
JFileChooserFrame_jButton1_actionAdapter(JFileChooserFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -