我使用 Netbeans 创建了一个小型 GUI。我遇到了 settext 和 gettext 的问题。如果您能说出问题出在哪里以及我必须做什么,或者您向我展示解决方案,我会非常高兴。
我想通过单击按钮创建一个word文件。这工作正常,但在 word 文件中应该有一些来自 JTextfiel 的文本,但这是行不通的。
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.HeadlessException;
import java.io.FileOutputStream;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.IOException;
private void createActionPerformed(java.awt.event.ActionEvent evt) {
try{
FileOutputStream outStream = new FileOutputStream("Bewerberinterview.docx");
XWPFDocument doc;
doc = new XWPFDocument();
XWPFParagraph paraTit=doc.createParagraph();
paraTit.setAlignment(ParagraphAlignment.CENTER);
XWPFRun paraTitRun=paraTit.createRun();
paraTitRun.setBold(true);
paraTitRun.setFontSize(20);
paraTitRun.setText(title.getText());
doc.createParagraph().createRun().addBreak();
doc.createParagraph().createRun().setText(name_content.getText());
doc.write(outStream);
doc.close();
System.out.println("createdocument.docx written successully");
}catch (HeadlessException | IOException e){
JOptionPane.showMessageDialog(null, e);
}
}
当我启动应用程序并在框中输入一些文本并单击“按钮 1 = 创建”时。该文件将正常创建,但其中没有文本。
最佳答案
以下代码,即Minimal, Reproducible Example对于您所描述的问题,按我的预期工作。
每次点击按钮write
,它都会写入包含两个文本字段内容的Bewerberinterview.docx
文件。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.apache.poi.xwpf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
public class TextDemo extends JPanel implements ActionListener {
protected JTextField title;
protected JTextField name_content;
protected JButton write;
public TextDemo() {
super();
title = new JTextField(20);
name_content = new JTextField(20);
write = new JButton("write");
write.addActionListener(this);
add(title);
add(name_content);
add(write);
}
public void actionPerformed(ActionEvent evt) {
String titleText = title.getText();
String name_contentText = name_content.getText();
System.out.println(titleText);
System.out.println(name_contentText);
try {
FileOutputStream outStream = new FileOutputStream("Bewerberinterview.docx");
XWPFDocument doc = new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
run.setBold(true);
run.setFontSize(20);
run.setText(titleText);
doc.createParagraph().createRun().addBreak();
doc.createParagraph().createRun().setText(name_contentText);
doc.write(outStream);
outStream.close();
doc.close();
System.out.println("File " + new File("Bewerberinterview.docx").getAbsolutePath() + " written successully.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new TextDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
所以你所描述的问题不存在。
确实如此?那么就显示一个Minimal, Reproducible Example这表明了问题。
关于java - 读取JTextfield放入word文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57453135/