透明JDialog

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;
    }
}

你可能感兴趣的:(java,C++,c,swing,C#)