为什么 GlassPane 不能与 JDIC 的 WebBrowser 一起使用?
我有以下程序来测试 GlassPane,但它不适用于 JDIC 的 WebBrowser。有谁知道我做错了什么以及如何让它发挥作用?
import org.jdesktop.jdic.browser.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class Test_Panel extends JPanel
{
static WebBrowser webBrowser=new WebBrowser();
static int W=802,H=702;
Test_Panel()
{
setPreferredSize(new Dimension(W,H));
setLayout(new BorderLayout());
webBrowser.setPreferredSize(new Dimension(W,H));
// add("Center",webBrowser);
try { webBrowser.setURL(new URL("http://www.yahoo.com")); }
catch (MalformedURLException e) { e.printStackTrace(); }
}
static void Create_And_Show_GUI()
{
JFrame frame=new JFrame("Test");
frame.add(new Test_Panel());
frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
new My_GlassPane(frame,W,H);
frame.pack();
frame.setBounds(0,0,W,H);
frame.setVisible(true);
}
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } }); }
}
class My_GlassPane extends JComponent
{
JFrame f;
int W,H,Edge,Size;
public My_GlassPane(JFrame f,int W,int H)
{
this.f=f;
this.W=W;
this.H=H;
Edge=W/100;
Size=W/5;
f.setGlassPane(this);
f.getGlassPane().setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(W/6,H*18/120,W*2/3,H*2/3);
g.setColor(Color.white);
g.setFont(new Font("Times New Roman",0,Size));
g.drawString("Test",W/3,H*68/120);
}
}
如果您取消注释 add("Center",webBrowser);
您就会明白我的意思 - GlassPane 将不会显示。为什么不呢?
您需要有“jdic.jar”和“IeEmbed.exe”才能使其工作。我拥有的版本是0.9.1.0,您可以获取它们 这里。
I have the following program to test GlassPane, but it doesn't work with JDIC's WebBrowser. Does anyone know what I did wrong and how to make it work?
import org.jdesktop.jdic.browser.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class Test_Panel extends JPanel
{
static WebBrowser webBrowser=new WebBrowser();
static int W=802,H=702;
Test_Panel()
{
setPreferredSize(new Dimension(W,H));
setLayout(new BorderLayout());
webBrowser.setPreferredSize(new Dimension(W,H));
// add("Center",webBrowser);
try { webBrowser.setURL(new URL("http://www.yahoo.com")); }
catch (MalformedURLException e) { e.printStackTrace(); }
}
static void Create_And_Show_GUI()
{
JFrame frame=new JFrame("Test");
frame.add(new Test_Panel());
frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
new My_GlassPane(frame,W,H);
frame.pack();
frame.setBounds(0,0,W,H);
frame.setVisible(true);
}
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Create_And_Show_GUI(); } }); }
}
class My_GlassPane extends JComponent
{
JFrame f;
int W,H,Edge,Size;
public My_GlassPane(JFrame f,int W,int H)
{
this.f=f;
this.W=W;
this.H=H;
Edge=W/100;
Size=W/5;
f.setGlassPane(this);
f.getGlassPane().setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(W/6,H*18/120,W*2/3,H*2/3);
g.setColor(Color.white);
g.setFont(new Font("Times New Roman",0,Size));
g.drawString("Test",W/3,H*68/120);
}
}
If you uncomment add("Center",webBrowser);
you will see what I mean-- the GlassPane won't show up. Why not?
You need to have "jdic.jar" and "IeEmbed.exe" to make it work. The version I have is 0.9.1.0 and you can get them here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我所读到的,WebBrowser 是一个 AWT 组件,而 GlassPane 是一个 Swing 组件。有一个常见问题混合重量级和轻量级组件。我认为您想要做的事情没有解决方法。
有关此主题的更多信息可以在此
As I've read, WebBrowser is an AWT component while GlassPane is a Swing component. There is a common problem mixing heavyweight and lightweight components. I don't think there is a workaround on what you're trying to do.
More information on this subject can be found in this discussion.