完整的源代码(3个java文件,1个html文件)在这里下载:
(靠,我上传了半天还没通过!,暂时把代码全部贴在下边。那个页面文件,请把任意一个HTML文件改名成Google.htm放到和这三个Java文件相同的目录下即可)
其中一个Java文件的内容如下。
为什么弹出的对话框的大小并不按我设置的尺寸显示?(JDK5.0)package ask.aboutdialog;import javax.swing.JFrame;public class ClientFrame extends JFrame {
public ClientFrame() {
this.setSize(350, 100);
this.setLocation(200, 100);
AskPanel askPanel = new AskPanel();
this.add(askPanel);
}
}package ask.aboutdialog;import javax.swing.JFrame;public class DialogQuestion { public static void main(String[] args) {
ClientFrame frame = new ClientFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}package ask.aboutdialog;import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;public class AskPanel extends JPanel {
JButton button = new JButton("Click me to show the pop-up window"); AskPanel() {
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
JEditorPane editorPane = new JEditorPane();
editorPane.setSize(new Dimension(100, 200));
editorPane.setEditable(false);
try {
editorPane.setPage(getClass().getResource("Google.htm"));
editorPane.addHyperlinkListener(new Hyperactive());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} JDialog dialog = new JDialog();
dialog.add(editorPane);
dialog.setModal(false);
dialog.setLocation(20, 20);
//不理解為什么在6.0下可以,而在5.0下此處的限制沒有效果?
dialog.setMinimumSize(new Dimension(100, 100));
dialog.setPreferredSize(dialog.getMinimumSize());
dialog.setVisible(true); } }); add(button);
} class Hyperactive implements HyperlinkListener { public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) e.getSource();
if (e instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
HTMLDocument doc = (HTMLDocument) pane.getDocument();
doc.processHTMLFrameHyperlinkEvent(evt);
} else {
try {
pane.setPage(e.getURL());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
}}

解决方案 »

  1.   

    新浪刚刚审核完上传的文件,请到这里下载代码调试:
    http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2886801
    谢谢你!网路审查害死人,靠!浪费我时间,下班回去了!
      

  2.   

    Java 5 里在显示前加一句 dialog.pack();dialog.setMinimumSize(new Dimension(100, 100));
    dialog.setPreferredSize(dialog.getMinimumSize());
    dialog.pack();
    dialog.setVisible(true);
      

  3.   

    chenweionline, thanks very much, all the scores are assigned to you!