首页 > 解决方案 > 读取 JTextfield 放入 word 文件

问题描述

我用 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 = 创建”时。该文件将创建良好,但其中没有文本。

标签: javanetbeansapache-poi

解决方案


以下代码是您所描述问题的最小可重现示例,它可以按我预期的方式工作。

对于每次单击按钮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();
            }
        });
    }
}

所以你描述的问题不存在。

有吗?然后展示一个最小的、可重现的例子来说明问题。


推荐阅读