public class EditorPane extends javax.swing.JPanel {

/**
 * Creates new form EditorPane
 */
public EditorPane() {
    initComponents();
}

private void launchHyperLink(HyperlinkEvent e) {
    try {
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {

            String cmdFileLocation = System.getenv("windir") + File.separator + "system32" + File.separator + "cmd.exe";
            Runtime.getRuntime().exec(new String[]{cmdFileLocation, "/c", "start", e.getDescription()});
        }
    } catch (IOException ex) {
        Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void initEditorPane(JEditorPane editorPane) {
    editorPane.setBorder(null);
    editorPane.setContentType("text/html");
    editorPane.setEditable(false);
    editorPane.setOpaque(false);
    editorPane.addHyperlinkListener(new HyperlinkListener() {

        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                launchHyperLink(e);
            }
        }
    });
}


我试图在多个不同的GUI中重用上述EditorPane,因为它们具有以相同方式起作用的各种超链接。

但是我不确定如何在调用它的GUI中调用initEditorPane方法。

这就是我的意思:

public class MainMenu extends javax.swing.JFrame {

    private AcademicDTO ac;

    public MainMenu(AcademicDTO academicDTO) {
        this.ac = academicDTO;
        initComponents();
        searchTable.init(GUIStaticDataHelper.ACADEMIC_SUMMARY_COLUMNS);
        myContactTable.init(GUIStaticDataHelper.CONTACT_SUMMARY_COLUMNS);
        initEditorPane(emailTxtComp);
        initEditorPane(pageTxtComp);
        initComponentsWithData();
        initListeners();
    }

    public void initComponentsWithData() {
        nameLabel.setText("" + ac.getTitle() + " " + ac.getForename() + " " + ac.getSurname());
        roleLabel.setText(ac.getRole());
        roomLabel.setText("Room:    " + ac.getRoom());
        pageLabel.setText("Page:");
        pageTxtComp.setText("<html>&nbsp;<a href='" + ac.getPage() + "'>" + ac.getPage() + "</a>&nbsp;</html>");
        hoursLabel.setText("Hours:   " + ac.getHours());
        phoneLabel.setText("Phone:   " + ac.getPhone());
        mobileLabel.setText("Mobile:  " + ac.getMobile());
        emailLabel.setText("Email:");
        myContactTable.setData(ac.getContacts());
        if (ac.getImage() != null) {
            imageLabel.setIcon(new ImageIcon(ac.getImage()));
        }
        emailTxtComp.setText("<html>&nbsp;<a href='mailto://" + ac.getEmail() + "'>" + ac.getEmail() + "</a>&nbsp;</html> ");
    }


emailTxtComppageTxtComp现在都键入EditorPane而不是JEditorPane。因此无法使用方法initEditorPane(JEditorPane editorPane)

还有线

initEditorPane(emailTxtComp);
            initEditorPane(pageTxtComp);


我将它们更改为什么?

最佳答案

困难的问题,因为我不知道您的系统是如何设计的。这里有两个建议:


从对象的构造函数中自动调用方法initEditorPane(如果所有这些窗格都应受支持)
在EditorPane中进行静态初始化。

公共静态无效的buildPane(EditorPane aPane){
  initEditorPane(aPane.editorPane);
}


而且,正如垃圾桶评论中所说的那样,由于Java 6,您可以使用Desktop类来调用浏览器。

protected void launchHyperLink(HyperlinkEvent e) {
  if (Desktop.isDesktopSupported()) {
    try {
      Desktop.getDesktop().browse(e.getURL().toURI());
    } catch (Exception ex) {
      Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

09-04 06:44