gpt4 book ai didi

java - 启动时将文件打开到 JTextArea

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:30 27 4
gpt4 key购买 nike

我有一个程序,正在尝试测试是否创建了文件 (info.txt),如果是,则在程序启动时将内容打开到 JTextArea 中。我该怎么做呢?我已经弄清楚如何搜索并查看文件是否存在,只是无法在启动时将其打开到文本区域

import javax.swing.*; // need it
import java.awt.*; //need it also
import java.awt.datatransfer.*;
import java.util.*;
import java.io.*;
import java.awt.event.*; //keeps track of events
import javax.swing.border.*; // not necessary / already imported ^^

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.border.*;

public class finalA extends JFrame implements ActionListener{
private final int WIDTH = 750;
private final int HEIGHT = 400;
static String theText;
private String text;

//creates components
private JTextArea textArea;
private JButton SaveB;
private JScrollPane scroll;


public finalA(){
setTitle("Super fancy text editor");
setSize(WIDTH,HEIGHT);

Container pane = getContentPane();

//Creates button
SaveB = new JButton("Save & Exit");
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

//adds event handler for exit button

SaveB.addActionListener(this);

//sets pane to null
pane.setLayout(null);

//location of button and TA (750,400)
textArea.setLocation(10,100);
SaveB.setLocation(150,320);

//set size of TA and Button
textArea.setSize(730,200);
SaveB.setSize(100,30);

//add items to pane
pane.add(textArea);
pane.add(SaveB);

scroll.setBounds(10,100,730,200);
scroll.setVisible(true);

textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//ends constructor

public void openFile()throws Exception{
File f = new File("info.txt");
if(f.exists()){
FileReader reader = null;
try{
reader = new FileReader("info.txt");
textArea.read(reader, null);
}
finally{
if (reader != null){
reader.close();
}
}
}
}//ends openFile

public void actionPerformed(ActionEvent e){
String text = textArea.getText();
if(e.getActionCommand().equals("Save & Exit")){

try{
BufferedWriter reader = new BufferedWriter(new FileWriter(new File("info.txt"),true));
reader.write(text);
reader.newLine();
reader.close();
}catch(IOException E){
System.out.println("the error is " + E);
}
}
System.exit(0);



}
}// ends finalA class

最佳答案

因此,根据您的可用代码,您只需在创建 JTextArea 之后,在构造函数末尾调用 openFile 即可。

public TestRead() {
//...
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
openFile();
} catch (IOException exp) {
exp.printStackTrace();
}
}//ends constructor

不要使用null布局,像素完美布局在现代GUI设计中是一种幻觉。您无法控制字体规范、DPI 或渲染管道等因素,所有这些因素都可能改变组件的单独要求。

Swing 被设计为使用布局管理器,并且更新界面的许多功能都与其相关。如果您放弃布局管理器,请准备好承受永无休止的工作和挫败感

关于java - 启动时将文件打开到 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23531893/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com