【问题标题】:Read JTextfield put into word file读取 JTextfield 放入 word 文件
【发布时间】:2019-08-30 21:41:43
【问题描述】:

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

【问题讨论】:

  • 你得到了什么?你想要/期望得到什么?请注意,操作系统和正在使用的软件的确切版本(最新?)可能是相关的。
  • doc.write(outStream); 之后执行outStream.close();。如果那没有帮助进行调试。什么返回title.getText()?在该代码的上下文中返回 name_content.getText() 是什么?
  • 感谢您的回复。它的netbeans 11.1。和 apache-poi-bin 4.1.0。我做 outStream.close();
  • 我做 outStream.close();在 doc.write(outStream) 之后。它没有任何变化。 tiitle.gettext() 和 name_content.gettext() 是 JTextFields,您可以输入各种文本。
  • “我在 doc.write(outStream) 之后做 outStream.close();”:真的吗?你的代码没有显示。 “tititle.gettext() 和 name_content.gettext() 是 JTextFields,您可以输入各种文本”:是的,但是 title.getText()name_content.getText() 在您的代码上下文中返回什么?也许两者都返回空字符串?正如我所说,做一些调试。

标签: java netbeans apache-poi


【解决方案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 来显示问题。

【讨论】:

  • 谢谢你的代码。我运行了该文件,但出现以下故障:“AWT-EventQueue-0”java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
  • 您需要类路径中的所有apache poi jar。需要的罐子见Component Map。错过的org/apache/xmlbeans/XmlExceptionxmlbeans-3.1.0.jar
  • 谢谢你完美的工作!你解决了我的问题。我遇到的其他问题是:如何在 JPanel 中打开 youtube?我应该打开一个新线程还是你知道答案?
  • @Marius Schöttli:请就这个新问题提出一个新问题。但先做一些自己的努力。我从来没有这样做过,但是使用关键字jpanel embed browser 进行互联网搜索显示了有希望的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 2013-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多