Why doesn't GlassPane work with JDIC's WebBrowser?
I have the following program to test Gla开发者_如何转开发ssPane, 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.
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.
精彩评论