高级UI组件之文件对话框
程序员文章站
2022-03-30 11:40:51
...
在对话框的基础模式上添加JFileChooser。读取文件,显示在文字编辑框中。并可以保存编辑框中的内容,选择指定文件位置。
package Night;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.GridLayout;
public class FileChooserDemo {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileChooserDemo window = new FileChooserDemo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public FileChooserDemo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 553, 371);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("\u8BF7\u9009\u62E9\u6587\u4EF6\uFF1A");
frame.getContentPane().add(label, BorderLayout.NORTH);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(0, 1, 0, 0));
JTextArea textArea = new JTextArea();
panel.add(textArea);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("打开");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc = new JFileChooser();
int value = jfc.showOpenDialog(frame);
if(value==JFileChooser.APPROVE_OPTION)//选择打开
{
File fileName = jfc.getSelectedFile();
try {
FileReader fread = new FileReader(fileName);
char[] ch = new char[1024];
int hasData=0;
try {
if((hasData=fread.read(ch))>=0) {
textArea.append(new String(ch));
}
fread.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
panel_1.add(btnNewButton);
JButton btnNewButton_1 = new JButton("保存");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc = new JFileChooser();
int value = jfc.showOpenDialog(frame);
if(value==JFileChooser.APPROVE_OPTION) {
File filename = jfc.getSelectedFile();
try {
FileWriter fw = new FileWriter(filename);
char[] ch = new char[1024];
int hasData=0;
fw.write(textArea.getText());
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
});
panel_1.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("清除");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText("");
}
});
panel_1.add(btnNewButton_2);
}
}
推荐阅读
-
vue.js学习之UI组件开发教程
-
element-ui upload组件多文件上传的示例代码
-
Element-UI踩坑之Pagination组件的使用
-
无组件上传图片之文件采用方案
-
AngularJs的UI组件ui-Bootstrap之Tooltip和Popover
-
详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
-
Android学习笔记之——UI组件/RelativeLayout(相对布局)
-
ASP编程入门进阶(十七):FSO组件之文件夹操作
-
ASP编程入门进阶(十八):FSO组件之文件操作(下)
-
ASP编程入门进阶(十八):FSO组件之文件操作(上)