当然可以,你是为了将html代码显示给用户看是吗?

解决方案 »

  1.   


    可以用JEditPanel类来处理,不过如果你要稍做处理,否则它只显示一个URL。你可以用Document来做中间处理,下周来我给你代码吧。或是你详细看一下API文档,会有发现的。
      

  2.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class Test extends JFrame {
        private JEditorPane editorPane = new JEditorPane();    public Test() {
            Container contentPane = getContentPane();        String url = "file:" + System.getProperty("user.dir") +
                 System.getProperty("file.separator") +
                 "java.util.Hashtable.html";        editorPane.setEditable(false);        try { 
                editorPane.setPage(url);
            }
            catch(Exception ex) { ex.printStackTrace(); }        contentPane.add(new ControlPanel(), BorderLayout.NORTH);
            contentPane.add(new JScrollPane(editorPane), 
                            BorderLayout.CENTER);
        }
        class ControlPanel extends JPanel {
            private JCheckBox edit = new JCheckBox("Editable");        public ControlPanel() {
                add(edit);
                edit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        editorPane.setEditable(edit.isSelected());
                    }
                });
            }
        }
        public static void main(String args[]) {
            GJApp.launch(new Test(), 
                        "JEditorPane",300,300,650,450);
        }
    }
    class GJApp extends WindowAdapter {
        static private JPanel statusArea = new JPanel();
        static private JLabel status = new JLabel(" ");
        static private ResourceBundle resources;    public static void launch(final JFrame f, String title,
                                  final int x, final int y, 
                                  final int w, int h) {
            launch(f,title,x,y,w,h,null);    
        }
        public static void launch(final JFrame f, String title,
                                  final int x, final int y, 
                                  final int w, int h,
                                  String propertiesFilename) {
            f.setTitle(title);
            f.setBounds(x,y,w,h);
            f.setVisible(true);        statusArea.setBorder(BorderFactory.createEtchedBorder());
            statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            statusArea.add(status);
            status.setHorizontalAlignment(JLabel.LEFT);        f.setDefaultCloseOperation(
                                WindowConstants.DISPOSE_ON_CLOSE);        if(propertiesFilename != null) {
                resources = ResourceBundle.getBundle(
                            propertiesFilename, Locale.getDefault());
            }        f.addWindowListener(new WindowAdapter() {
                public void windowClosed(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
        static public JPanel getStatusArea() {
            return statusArea;
        }
        static public void showStatus(String s) {
            status.setText(s);
        }
        static Object getResource(String key) {
            if(resources != null) {
                return resources.getString(key);
            }
            return null;
        }
    }
      

  3.   

    这个好玩不好玩:JLabel theText = new JLabel(
         "<html>Hello! This is a multiline label with <b>bold</b> "
         + "and <i>italic</i> text. <P> "
         + "It can use paragraphs, horizontal lines, <hr> "
         + "<font color=red>colors</font> "
         + "and most of the other basic features of HTML 3.2</html>");
      

  4.   

    hexiaofeng(java爱好者) 答非所问。
    grantdyg(正确) 的回答很好,感谢。会得分的。请教别的swing控件中有没有能显示html的。
    热切等待 sharetop(天生不笨) 的代码。
      

  5.   

    to nielinjie
    JEditPane可以显示html,而且可以编辑
      

  6.   


    其实 grantdyg已经给出了很好的代码,我的与他差不多,那天只是要下班了不想写了。给你吧,差不多一样的,不用document:    jEditorPane1.setContentType("text/html");
        jEditorPane1.setText("<html>Hello! This is a multiline label with <b>bold</b> "
        + "and <i>italic</i> text. <P> "
        + "It can use paragraphs, horizontal lines, <hr> "
        + "<font color=red>colors</font> "
        + "and most of the other basic features of HTML 3.2</html>");
    这种方法有一个问题,如果用于聊天室的构造。你看出来的吧。
      

  7.   

    cannot auto refresh? i dont know.
    有请 sharetop(天生不笨) 提示一下,分就到手了。
      

  8.   

    jEditorPane1.repaint()试试!
    或者:
    jEditorPane1.validate()