输出链接回java程序的HTML文件

时间:2013-05-24 20:48:37

标签: java html

在我的Java程序中,我必须输出一个HTML文件,其中包含发送或显示特定电子邮件等按钮,这些按钮保存在此java程序中(将在服务器上运行)。如何链接此HTML文件,以便在单击按钮时,它实际上调用java程序中的方法(例如发送电子邮件方法)并影响某些字段,例如存储电子邮件的列表?如果没有办法做到这一点,我怎样才能实现我想做的事情?谢谢。

这是我用于GUI的代码,但我不想要GUI。我想将所有这些代码转换为输出的HTML文件。基本上,代替这些代码,我需要输出一个与此GUI完全相同的HTML文件。我需要输出网页的原因是,当程序启动到服务器上时,GUI并不那么方便。我相信程序将始终运行,并且主要是我有一个timeTask,每天运行一次这个例程。

public void createEmailPanel(String name, final Email email) {
    final JPanel emailPanel = new JPanel();
    emailPanel.setLayout(null);
    emailPanel.setSize(900, 50);
    panel.add(emailPanel);

    String emails = "";
    for(String a : email.getList()) {
        emails = emails + a + " ";
    }

    JLabel labelName = new JLabel(emails);
    labelName.setLocation(0, 0);
    labelName.setSize(700, 30);
    emailPanel.add(labelName);

    JButton display = new JButton("Display Email");
    display.setLocation(0, 50);
    display.setSize(200, 30);
    display.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JEditorPane HtmlPane= new JEditorPane();
            boolean fail = false;
            try {
                File temp = File.createTempFile("temp", ".html");
                BufferedWriter out = new BufferedWriter(new FileWriter(temp));
                out.write(email.getBody());
                out.close();

                HtmlPane.setContentType("text/html");
                HtmlPane.setEditable(false);
                try {
                    HtmlPane.setPage(temp.toURI().toURL());
                } catch (MalformedURLException a) {
                    fail = true;
                    JPanel p = new JPanel();
                    p.setSize(500, 200);
                    p.setLayout(null);

                    JLabel labelName = new JLabel("Error displaying emails");
                    labelName.setLocation(0, 0);
                    labelName.setSize(700, 30);
                    p.add(labelName);

                    JFrame f = new JFrame();
                    f.setSize(500, 200);
                    f.add(p);
                    f.setVisible(true);
                }
                JScrollPane jsp=new JScrollPane(HtmlPane);
                JFrame f = new JFrame();
                f.setSize(800, 500);
                f.add(jsp);
                f.setVisible(true);
                temp.delete();
            } catch (IOException e1) {
                if(fail) {
                    JPanel p = new JPanel();
                    p.setSize(500, 200);
                    p.setLayout(null);

                    JLabel labelName = new JLabel("Error displaying emails");
                    labelName.setLocation(0, 0);
                    labelName.setSize(700, 30);
                    p.add(labelName);

                    JFrame f = new JFrame();
                    f.setSize(500, 200);
                    f.add(p);
                    f.setVisible(true);
                }
            }

        }
    });
    emailPanel.add(display);


    JButton sendRecipient = new JButton("Send Email to Recipients");
    sendRecipient.setLocation(440, 50);
    sendRecipient.setSize(200, 30);
    sendRecipient.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            email.sendEmailToRecipients();
            queue.remove(email);
            panel.remove(emailPanel);
            JPanel pane = new JPanel();
            pane.setSize(900, 50);
            panel.add(pane);
            JLabel labe = new JLabel("Email sent to recipients.");
            labe.setSize(900, 50);
            labe.setHorizontalAlignment(0);
            pane.add(labe);
            frame.validate();
            frame.repaint();
        }
    });
    emailPanel.add(sendRecipient);

email是一个Email对象,它有一个使用JavaMail的sendEmailRecipient方法。此外,电子邮件的正文也是HTML,可能与此无关。

0 个答案:

没有答案