0

我想使用java应用程序(file.doc)在word文档中保存一个http链接。我使用BufferedWriter类成功地写入了word文档的链接,但word文档中的链接只是简单的一行文本。如果您按文档字上的输入按钮,它会变成 http 链接,这就是我想要对我的 java 应用程序执行的操作,另存为 http 格式

这是我的代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.swing.JTextField;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Save_file extends JFrame {

    private JPanel contentPane;
    public static JTextField textField;
    JFileChooser chooser;
    File file;
    public static FileWriter fw;
    public static BufferedWriter output;
    String the_past;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Save_file frame = new Save_file();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Save_file() {
        setResizable(false);

        setIconImage(Toolkit.getDefaultToolkit().getImage(Save_file.class.getResource("/Buttons/LogoIcon.png")));
        setTitle("Saving file");
        this.setSize(435, 300);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //  setBounds(100, 100, 435, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("Select a File :");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblNewLabel.setBounds(12, 13, 152, 25);
        contentPane.add(lblNewLabel);

        JButton btnNewButton = new JButton("");
        btnNewButton.setIcon(new ImageIcon(Save_file.class.getResource("/Buttons/loaddisk (2).gif")));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                try{


                    output = new BufferedWriter(fw);
                    output.write(textField.getText()+"\n");
                    output.write(" ");
                    JOptionPane.showMessageDialog(null, "Saving succes", "Save", JOptionPane.INFORMATION_MESSAGE);
                    //textField.setText("");
                    output.write("\r");
                    output.flush();
                    output.close();


                }
                catch(Exception io){
                    JOptionPane.showMessageDialog(null, "Saving Failed", "Error", JOptionPane.ERROR_MESSAGE);
                }

            }
        });
        btnNewButton.setBounds(142, 211, 32, 32);
        contentPane.add(btnNewButton);

        JLabel lblLink = new JLabel("Link   ");
        lblLink.setToolTipText("Just click once to past");
        lblLink.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblLink.setBounds(12, 107, 97, 25);
        contentPane.add(lblLink);

        textField = new JTextField();
        textField.setToolTipText("Just click once to past");
        textField.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                the_past= textField.getText();
                if (the_past.isEmpty()){
                textField.paste();
                }
                //else
                    //textField.setText("");
            }
        });
        textField.setColumns(10);
        textField.setBounds(12, 154, 393, 33);
        contentPane.add(textField);

        JButton btnOpenFile = new JButton("Open File");
        btnOpenFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

               try{

                    chooser = new JFileChooser();
                    int retval = chooser.showOpenDialog(Save_file.this);


                    if (retval == JFileChooser.APPROVE_OPTION) {
                        //... The user selected a file, get it, use it.
                        File file = chooser.getSelectedFile();
                        System.out.println("File equals"+file);
                        fw = new FileWriter(file, true);


                    }


                }
                catch(Exception io){


                }
            }

        });
        btnOpenFile.setBounds(12, 53, 97, 25);
        contentPane.add(btnOpenFile);

        JButton button = new JButton("");
        button.setIcon(new ImageIcon(Save_file.class.getResource("/Buttons/cc.jpg.png")));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        button.setBounds(243, 211, 35, 35);
        contentPane.add(button);
    }
}

保存链接示例的结果 应该是的结果

4

2 回答 2

2

不,你没有那样做。

您正在使用您使用的字符串编写一个文本(ASCII 或 UTF)文件,并为该文件分配一个 .doc 扩展名。.doc 扩展名不会使您的文件成为 word 文档。MS Word 可以自动读取文本文件并显示它,但您不是在编写(我重复)Word 文件。

搜索允许操作/创建 Word 文件的 API(我认为 Apache 有其中之一)。

于 2013-08-26T15:14:30.487 回答
1

如前所述,您创建了一个不支持超链接的简单纯文本文档。您应该查看Apache POI库以创建富文本格式文档。

于 2013-08-26T15:19:58.097 回答