java 点击按钮 自动写入文本

java 点击按钮 自动写入文本,第1张

import java.io.*

public class Test {

public static void main(String[] args){

String s = new String()

String s1 = new String()

try {

File f = new File("E:\\123.txt")

if(f.exists()){

System.out.print("文件存在")

}else{

System.out.print("文件不存在")

f.createNewFile()//不存在则创建

}

BufferedReader input = new BufferedReader(new FileReader(f))

while((s = input.readLine())!=null){

s1 += s+"\n"

}

System.out.println(s1)

input.close()

s1 += "添加的内容!"

BufferedWriter output = new BufferedWriter(new FileWriter(f))

output.write(s1)

output.close()

} catch (Exception e) {

e.printStackTrace()

}

}

}

在点击按执行上面参考的方法 当然不能在main 你写在类里面

①建立com.lowagie.text.Document对象的实例。

Document document = new Document()

②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。

PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"))

③打开文档。

document.open()

④向文档中添加内容。

document.add(new Paragraph("Hello World"))

⑤关闭文档。

document.close()

通过上面的5个步骤,就能产生一个Helloworld.PDF的文件,文件内容为"Hello World"。

import java.awt.*

import java.awt.event.*

import javax.swing.*

public class JTableTest extends JFrame implements ActionListener{

public JTableTest() {

JButton button = new JButton("添加文本框")

this.add(button)

button.addActionListener(this)

this.setVisible(true)

this.setDefaultCloseOperation(EXIT_ON_CLOSE)

}

public void actionPerformed(ActionEvent e){

JTextField text = new JTextField("")

this.add(text)

}

public static void main (String[] args) {

new JTableTest()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11799711.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存