透明JDialog

[color=red]import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class Ebook extends JDialog{
private MyShow myshows;
private JTextPane textpane;
public Ebook(final MyShow myshows){
super((Dialog)null, true);
this.myshows = myshows;
this.setUndecorated(true);
extraInitComponents(true);
initGUI();

}
private void initGUI(){
setBak();
this.getContentPane().setLayout(new BorderLayout());
textpane = createHTMLPane();
JScrollPane jsc = new JScrollPane(textpane);
URL url = null;
StringBuffer page = new StringBuffer();
StringBuffer str = new StringBuffer();
str.append("C:\\zh_CN");
page.append("file:/");
page.append(str);
page.append(File.separator);
page.append("ebook.htm");
try {
url = new URL(page.toString());
textpane.setPage(url);
} catch (Exception ex) {
}
JPanel centerPanel = new JPanel(new BorderLayout());

centerPanel.setOpaque(false);
centerPanel.add(jsc,BorderLayout.CENTER);
centerPanel.setPreferredSize(new Dimension(50, 50));
centerPanel.setMinimumSize(new Dimension(50, 50));
centerPanel.setMaximumSize(new Dimension(50, 50));
this.getContentPane().add(centerPanel,BorderLayout.CENTER);
JPanel button = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton close = new JButton("close");
close.setPreferredSize(new Dimension(75, 25));
close.setMinimumSize(new Dimension(75, 25));
close.setMaximumSize(new Dimension(75, 25));
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});

button.add(close);
button.add(Box.createHorizontalStrut(75));
button.setOpaque(false);


this.getContentPane().add(button,BorderLayout.SOUTH);
this.getContentPane().add(Box.createVerticalStrut(80),BorderLayout.NORTH);
this.getContentPane().add(Box.createHorizontalStrut(80),BorderLayout.WEST);
this.getContentPane().add(Box.createHorizontalStrut(80),BorderLayout.EAST);
((JPanel) this.getContentPane()).setOpaque(false);
}

public void ShowUI(){
setVisible(true);
}

private void setBak() {
((JPanel) this.getContentPane()).setOpaque(false);
StringBuffer imagestr = new StringBuffer();
imagestr.append("C:\\back.PNG");
final ImageIcon img = new ImageIcon(imagestr.toString());
JLabel background = new JLabel(img){
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img.getImage(), 0,0,img.getIconWidth(),img.getIconHeight(),null);
g.dispose();
}
};
getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
setSize(400, 300);
}

public void extraInitComponents(boolean ifFirstInit) {
setLocationRelativeTo(myshows);
setAlwaysOnTop(true);
com.sun.awt.AWTUtilities.setWindowOpaque(this, false);
}

public static JTextPane createHTMLPane() {
JTextPane textPane;
textPane = new JTextPane();
HTMLEditorKit kit = new HTMLEditorKit();//kit s ur HTMLEditorKit
HTMLDocument doc = (HTMLDocument) (kit.createDefaultDocument());
textPane.setEditorKit(kit);//'this' is ur JTextPane object
textPane.setDocument(doc);
textPane.setEditable(false);
return textPane;
}
}[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值