Java - 无法加载相同的URL并将其显示在编辑器窗格中两次?

时间:2011-08-23 08:41:57

标签: java swing jbutton jeditorpane

这是我用于在编辑器窗格中显示网址内容的代码

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    import java.net.*;


    public class header extends JFrame {

        private void initComponents() {

            jTextField2 = new JTextField();
            jTextField3 = new JTextField();
            jButton1 = new JButton();
            jScrollPane1 = new JScrollPane();
            jTextArea1 = new JEditorPane();

            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            jButton1.setText("Load");
            jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });

            jTextArea1.setEditable(false);
            jScrollPane1.setViewportView(jTextArea1);
            setSize(418,300);
            setVisible(true);
            GroupLayout layout = new GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>

    private void jButton1ActionPerformed(ActionEvent evt) {
        try {
            URL header = new URL(jTextField2.getText());
                jTextArea1.setPage(header);
            }catch(MalformedURLException e){} catch(IOException e){}
        }


        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            header gui = new header();
            gui.initComponents();
        }
        // Variables declaration - do not modify
        private JButton jButton1;
        private JScrollPane jScrollPane1;
        private JEditorPane jTextArea1;
        private JTextField jTextField2;
        private JTextField jTextField3;
        // End of variables declaration
    }

import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.io.*; import java.net.*; public class header extends JFrame { private void initComponents() { jTextField2 = new JTextField(); jTextField3 = new JTextField(); jButton1 = new JButton(); jScrollPane1 = new JScrollPane(); jTextArea1 = new JEditorPane(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Load"); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTextArea1.setEditable(false); jScrollPane1.setViewportView(jTextArea1); setSize(418,300); setVisible(true); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(ActionEvent evt) { try { URL header = new URL(jTextField2.getText()); jTextArea1.setPage(header); }catch(MalformedURLException e){} catch(IOException e){} } /** * @param args the command line arguments */ public static void main(String args[]) { header gui = new header(); gui.initComponents(); } // Variables declaration - do not modify private JButton jButton1; private JScrollPane jScrollPane1; private JEditorPane jTextArea1; private JTextField jTextField2; private JTextField jTextField3; // End of variables declaration }

我只是无法让它刷新并显示相同网址的内容两次。当我点击加载按钮时它会加载内容但是如果我再次点击加载按钮它不会加载网址或显示内容..我做错了什么?我是java的新手,很抱歉,如果这是一个简单的解决方案,如果你能指出我正确的方向来解决这个问题那就太棒了,谢谢!

1 个答案:

答案 0 :(得分:1)

我似乎记得你需要强制编辑器窗格重新加载页面。

猜猜,但你可以尝试:

setContentType("");

setDocument(null);