怎样把jdialog变透明?

解决方案 »

  1.   

    给你一份完整的代码 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; 
        } 
    }希望对你有帮助
      

  2.   

    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;import com.sun.awt.AWTUtilities;@SuppressWarnings("serial")
    public class Test extends JPanel {    public Test() {
            add(new JButton("Hi"));
            add(new JButton("Biao"));
            add(new JCheckBox("Check"));
        }    public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.getContentPane().add(new Test());
            AWTUtilities.setWindowOpacity(frame, 0.5f);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
      

  3.   

    AWTUtilities.setWindowOpacity(frame, 0.5f);这句话就够了!
      

  4.   

       重点就是这句话:AWTUtilities.setWindowOpacity(frame, 0.5f);在SWT插件里就可以看的出很明显的效果。
      

  5.   

    import com.sun.awt.AWTUtilities;
    这个AWTUtilities  咋弄的?
    这个包 是下载的吗??