怎样把jdialog变透明? 怎样把jdialog变透明? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你一份完整的代码 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; } }希望对你有帮助 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); }} AWTUtilities.setWindowOpacity(frame, 0.5f);这句话就够了! 重点就是这句话:AWTUtilities.setWindowOpacity(frame, 0.5f);在SWT插件里就可以看的出很明显的效果。 import com.sun.awt.AWTUtilities;这个AWTUtilities 咋弄的?这个包 是下载的吗?? 引入外部js文件出错 对象ObjectInputStream的文件操作,报java.io.StreamCorruptedException异常。 在JAVA中怎样使用continue与break的? 一个初学者的问题!!! 各位老师啊,我不想输出null,怎么改呀 覆盖 谁有把一个对象从JTree拖放到JTable的例子? java编程思想练习 苦思冥想没答案 类型的转换问题?谢谢!! 忙毕业设计中,请教高手问题,谢谢帮忙! 临时变量的问题? 最近用java去解析一个未知的文件,希望大家讨论下,散分
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;
}
}希望对你有帮助
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);
}
}
这个AWTUtilities 咋弄的?
这个包 是下载的吗??