javax.swing:JEditorPane(html页面)没有出现在主框架上

时间:2011-03-02 10:29:31

标签: swing browser show paint

类浏览器的方法getBrowserWindow()返回JEditorPane(new URL("some URL")).

类MainForm调用new Browser()getBrowserWindow()并将其分配给另一个JEditorPane对象。

editor = new Browser().getBrowserWindow(); 
scrollpane = new JScrollPane(editor);

对象编辑器.hashCode()是正确的,所以毫无疑问我有正确的对象。

仍然,我无法在主框架中看到html页面

P.S。我试过创建而不是htmlpane简单的JEditorPane只填充文本, 而这个我也看不到。

P.P.S。

package faxutils;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;


public class MainForm {

    public MainForm() {

    }

    private static GeneralActions ga = new GeneralActions();
    private static Arguments _args_for_Update;
    private static String _Phone;
    private static JTextField[] emails;
    private static MyConstants c = new MyConstants();
    private static JFrame frame = new JFrame("frame title");
    private static JCheckBox c2 = new JCheckBox("SMS Notification");
    private static JCheckBox c1 = new JCheckBox("PDF");
    private static JCheckBox c3 = new JCheckBox("153");
    private static JTextField textBox1 = new JTextField(20);
    private static JTextField textBox2 = new JTextField(20);
    private static JTextField textBox3 = new JTextField(20);
    private static JTextField textBox4 = new JTextField(20);
    private static JTextArea richText = new JTextArea();
    private static JEditorPane log = new JEditorPane();
    private static JScrollPane scrollp = new JScrollPane(richText);
    private static JScrollPane scrollplog = new JScrollPane(log);
    private static JPanel p1 = new JPanel();
    private static JPanel p2 = new JPanel();
    private static JLabel label3 = new JLabel("Send to:");
    private static JLabel label2 = new JLabel("number");
    private static JLabel label = new JLabel("Fax:");
    private static JPanel p3 = new JPanel();
    private static JButton button1 = new JButton("Search");
    private static JButton button2 = new JButton("Save");
    private static JButton button3 = new JButton("Send");
    private static Component[] comp = new Component[]{c1, c2, c3, textBox2, textBox3, textBox3, textBox4, richText, log, button2, button3};

    private static void createAndShowGUI() {
        //Create and set up the window.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        Container contentPane = frame.getContentPane();
        SpringLayout layout1 = new SpringLayout();
        SpringLayout layout2 = new SpringLayout();
        SpringLayout layout3 = new SpringLayout();
        SpringLayout frmlayout = new SpringLayout();

        p1.setBorder(new TitledBorder("Search"));
        p2.setBorder(new TitledBorder("Customer details"));
        p3.setBorder(new TitledBorder("Send test fax"));

        p1.setLayout(layout1);
        p2.setLayout(layout2);
        p3.setLayout(layout3);

        frame.setLayout(frmlayout);

        //Create and add the components to p1.
        p1.add(label);
        p1.add(textBox1);
        p1.add(button1);
        //Create and add the components to p2
        p2.add(scrollp);
        p2.add(textBox2);
        p2.add(textBox3);
        p2.add(textBox4);
        p2.add(c1);
        p2.add(c2);
        p2.add(button2);
        //Create and add the components to p3
        p3.add(c3);
        p3.add(label3);
        p3.add(label2);
        p3.add(button3);
        p3.add(scrollplog);
        //Add panels to frame
        contentPane.add(p1);
        contentPane.add(p2);
        contentPane.add(p3);

        //Layout1
        //label
        layout1.putConstraint(SpringLayout.WEST, label,
                5,
                SpringLayout.WEST, p1);
        layout1.putConstraint(SpringLayout.NORTH, label,
                5,
                SpringLayout.NORTH, p1);
        //textbox1
        layout1.putConstraint(SpringLayout.WEST, textBox1,
                30,
                SpringLayout.WEST, label);
        layout1.putConstraint(SpringLayout.NORTH, textBox1,
                0,
                SpringLayout.NORTH, label);
        //button1
        layout1.putConstraint(SpringLayout.WEST, button1,
                230,
                SpringLayout.WEST, textBox1);
        layout1.putConstraint(SpringLayout.NORTH, button1,
                -5,
                SpringLayout.NORTH, textBox1);
        //Layout2
        //RICHTEXT
        layout2.putConstraint(SpringLayout.WEST, scrollp, 5, SpringLayout.WEST, p2);
        layout2.putConstraint(SpringLayout.NORTH, scrollp, 5, SpringLayout.NORTH, p2);
        layout2.putConstraint(SpringLayout.EAST, scrollp, -5, SpringLayout.EAST, p2);
        layout2.putConstraint(SpringLayout.SOUTH, scrollp, -150, SpringLayout.SOUTH, p2);
        //TEXTBOX1,2,3
        layout2.putConstraint(SpringLayout.WEST, textBox2, 140, SpringLayout.WEST, scrollp);
        layout2.putConstraint(SpringLayout.NORTH, textBox2, 130, SpringLayout.NORTH, scrollp);
        layout2.putConstraint(SpringLayout.WEST, textBox3, 0, SpringLayout.WEST, textBox2);
        layout2.putConstraint(SpringLayout.NORTH, textBox3, 25, SpringLayout.NORTH, textBox2);
        layout2.putConstraint(SpringLayout.WEST, textBox4, 0, SpringLayout.WEST, textBox3);
        layout2.putConstraint(SpringLayout.NORTH, textBox4, 25, SpringLayout.NORTH, textBox3);
        //CHECKBOXES
        layout2.putConstraint(SpringLayout.WEST, c1, 0, SpringLayout.WEST, scrollp);
        layout2.putConstraint(SpringLayout.NORTH, c1, 200, SpringLayout.NORTH, scrollp);
        layout2.putConstraint(SpringLayout.WEST, c2, 0, SpringLayout.WEST, c1);
        layout2.putConstraint(SpringLayout.NORTH, c2, 25, SpringLayout.NORTH, c1);
        //BUTTON
        layout2.putConstraint(SpringLayout.WEST, button2, 160, SpringLayout.WEST, textBox4);
        layout2.putConstraint(SpringLayout.NORTH, button2, 25, SpringLayout.NORTH, textBox4);
        //Layout3
        //CHECKBOX3
        layout3.putConstraint(SpringLayout.WEST, c3, 5, SpringLayout.WEST, p3);
        layout3.putConstraint(SpringLayout.NORTH, c3, 5, SpringLayout.NORTH, p3);
        //label3
        layout3.putConstraint(SpringLayout.WEST, label3, 50, SpringLayout.WEST, c3);
        layout3.putConstraint(SpringLayout.NORTH, label3, 4, SpringLayout.NORTH, c3);
        // label2
        layout3.putConstraint(SpringLayout.WEST, label2, 50, SpringLayout.WEST, label3);
        layout3.putConstraint(SpringLayout.NORTH, label2, 0, SpringLayout.NORTH, label3);
        //button3
        layout3.putConstraint(SpringLayout.WEST, button3, 305, SpringLayout.WEST, p3);
        layout3.putConstraint(SpringLayout.NORTH, button3, 0, SpringLayout.NORTH, p3);
        //Browser
        layout3.putConstraint(SpringLayout.WEST, scrollplog, 0, SpringLayout.WEST, c3);
        layout3.putConstraint(SpringLayout.NORTH, scrollplog, 25, SpringLayout.NORTH, c3);
        layout3.putConstraint(SpringLayout.EAST, scrollplog, 320, SpringLayout.EAST, c3);
        layout3.putConstraint(SpringLayout.SOUTH, scrollplog, 140, SpringLayout.SOUTH, c3);
        //FrameLayout
        //p1
        frmlayout.putConstraint(SpringLayout.WEST, p1,
                5,
                SpringLayout.WEST, frame);
        frmlayout.putConstraint(SpringLayout.NORTH, p1,
                5,
                SpringLayout.NORTH, frame);
        frmlayout.putConstraint(SpringLayout.EAST, p1,
                380,
                SpringLayout.EAST, frame);
        frmlayout.putConstraint(SpringLayout.SOUTH, p1,
                50,
                SpringLayout.SOUTH, frame);
        //p2
        frmlayout.putConstraint(SpringLayout.WEST, p2,
                5,
                SpringLayout.WEST, frame);
        frmlayout.putConstraint(SpringLayout.NORTH, p2,
                75,
                SpringLayout.NORTH, p1);
        frmlayout.putConstraint(SpringLayout.EAST, p2,
                380,
                SpringLayout.EAST, frame);
        frmlayout.putConstraint(SpringLayout.SOUTH, p2,
                285,
                SpringLayout.SOUTH, p1);
        //p3
        frmlayout.putConstraint(SpringLayout.WEST, p3,
                5,
                SpringLayout.WEST, frame);
        frmlayout.putConstraint(SpringLayout.NORTH, p3,
                285,
                SpringLayout.NORTH, p2);
        frmlayout.putConstraint(SpringLayout.EAST, p3,
                380,
                SpringLayout.EAST, frame);
        frmlayout.putConstraint(SpringLayout.SOUTH, p3,
                200,
                SpringLayout.SOUTH, p2);
        //Display the window.
        frame.setSize(400, 600);
        initialize();
        //  frame.pack();
        frame.setVisible(true);
    }


    private static void setPanelsEnabled(boolean b) {
        for (int i = 0; i < comp.length; i++)
            comp[i].setEnabled(b);
    }

    private static void initialize() {
        try {
            emails = new JTextField[]{textBox2, textBox3, textBox4};
            textBox1.requestFocus();
            setPanelsEnabled(false);
            richText.setEditable(false);
            log.setEditable(false);
            textBox1.setText("enter number here");
        } catch (Exception ex) {
            System.out.println(c.MSGBOX_EXCEPTION_TITLE + ": " + ex);
        }
    }

    private static void button1_Click() {
        try {
            label2.setText(textBox1.getText());
            _Phone = textBox1.getText();
            _args_for_Update = ga.query1(_Phone, c1, c2);

            if (_args_for_Update.getUserId() == 0) {
                c1.setSelected(false);
                c2.setSelected(false);
                System.out.println(c.MSGBOX_SEARCH_STATUS_TITLE + ": " + c.MSGBOX_NO_USER_WAS_FOUND);
            } else {
                richText.setText(_args_for_Update.getUserInfo());
                ga.query2(_args_for_Update); 
                _args_for_Update.setEmailsAmount(ga.fillEmails(_args_for_Update, emails)); 
            }
            boolean b = _args_for_Update.getUserId() > 0;
            setPanelsEnabled(b);
            c3.setSelected(false); 
        } catch (Exception ex) {
            System.out.println(c.MSGBOX_EXCEPTION_TITLE + ": " + ex);
        }
    }

    private static void button2_Click() {
        try {
            int saveOK = ga.SaveSettings(_args_for_Update, emails, c1, c2);
            button1.doClick(); 


            if (saveOK == c.USER_UPDATE_RESULT_0)
                System.out.println(c.MSGBOX_EMAIL_CHANGES_STATUS_TITLE + ": " + c.MSGBOX_CANNOT_DELETE_MAIN_EMAIL);
            else
                System.out.println(c.MSGBOX_USER_STATUS_TITLE + ": " + c.MSGBOX_USER_WAS_SUCCESSFULLY_UPDATED);
        } catch (Exception ex) {
            System.out.println(c.MSGBOX_EXCEPTION_TITLE + ": " + ex);
        }

    }

    private static void checkBox3_CheckedChanged() {
        if (c3.isSelected())
            label2.setText(c.FAX_PREFIX_153 + _Phone.substring(1)); 
        else
            label2.setText(_Phone);
    }

    private static void button3_Click() {
        try {
               // debugging
//            log = new Browser().getBrowserWindow(String.format(c.SEND_FAX, ".com")); /////////
            log = new JEditorPane(new URL(String.format(c.SEND_FAX, ".com")));
//            System.out.println(log.getText());
            p3.remove(scrollplog);
            scrollplog = new JScrollPane(log); //////////
            p3.add(scrollplog, layout3);
            scrollplog.repaint();


            System.out.println(c.MSGBOX_TEST_FAX_STATUS_TITLE + ": " + c.MSGBOX_FAX_SENDING_IN_PROGRESS);
        } catch (Exception ex) {
            System.out.println(c.MSGBOX_EXCEPTION_TITLE + ": " + ex);
        }
    }

    public static void main(String[] args) {
        try {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    button1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            button1_Click();
                        }
                    });
                    button2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            button2_Click();
                        }
                    });
                    c3.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            checkBox3_CheckedChanged();
                        }
                    });
                    button3.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            button3_Click();
                        }
                    });
                    createAndShowGUI();
                }
            });
        } catch (Exception e) {
            System.out.println(">:)" + e + ">:)");
        }

    }
}

1 个答案:

答案 0 :(得分:0)

Swing组件的实例化不足以显示它。它必须添加到某种容器中(JFrameJPanel ...)。同样适用于重新初始化。重新初始化组件后,必须从容器中删除旧版本并添加新版本。