java中怎样在图形界面下写入txt文件
3个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.List;
/**
* @author Hardneedl
*/
final class TextEditorDemo extends JFrame {
public String getTitle() {return "TextEditorDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
TextEditorDemo() throws HeadlessException {
init();
attachListeners();
doLay();
}
private void init(){
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
JTextArea textArea = new JTextArea();
container.add(new JScrollPane(textArea),BorderLayout.CENTER);
JPanel buttonsPane = new JPanel();
buttonsPane.add(new JButton(new OpenAction(textArea)));
buttonsPane.add(new JButton(new SaveAction(textArea)));
container.add(buttonsPane,BorderLayout.NORTH);
pack();
setVisible(true);
}
private class ReadWorker extends SwingWorker<String,String>{
private File f;
private JTextArea jTextArea;
private ReadWorker(File file,JTextArea textArea) {
f=file;
jTextArea =textArea;
}
protected String doInBackground() throws Exception {
jTextArea.setText(null);
BufferedReader bufReader = new BufferedReader(new FileReader(f));
String lineText;
while ( (lineText=bufReader.readLine())!=null)
publish(lineText);
return null;
}
protected void process(List<String> chunks) {
for(String s:chunks)jTextArea.append(s+"\r\n");
}
}
private class SaveWorker extends SwingWorker{
private String text;
private File file;
private SaveWorker(String text,File file) {this.text=text;this.file=file;}
protected Object doInBackground() throws Exception {
BufferedWriter bufWriter = new BufferedWriter(new FileWriter(file));
bufWriter.write(text);
bufWriter.flush();
bufWriter.close();
return null;
}
}
private class OpenAction extends AbstractAction{
private JFileChooser fileChooser;private JTextArea textArea;
private OpenAction(JTextArea textArea) {
super("Open ...");this.textArea=textArea;
fileChooser=new JFileChooser(".");
}
public void actionPerformed(ActionEvent e) {
if(fileChooser.showOpenDialog((Component) e.getSource())==JFileChooser.APPROVE_OPTION){
new ReadWorker( fileChooser.getSelectedFile(),textArea).execute();
}
}
}
private class SaveAction extends AbstractAction{
private JFileChooser fileChooser;private JTextArea textArea;
private SaveAction(JTextArea textArea) {
super("Save ...");this.textArea=textArea;
fileChooser=new JFileChooser(".");
}
public void actionPerformed(ActionEvent e) {
if(JFileChooser.APPROVE_OPTION==fileChooser.showSaveDialog((Component) e.getSource())){
new SaveWorker(textArea.getText(),fileChooser.getSelectedFile()).execute();
}
}
}
public static void main(String[] args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new TextEditorDemo();
}
}
);
}
}
展开全部
先new一个文本域,然后用IO流读出文件内容,通过文本域对象的对应的set方法往里面设置就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置文本域
更多追问追答
追问
具体点,好像比较麻烦,最好给个例子
追答
会拖控件不。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询