问题描述
我很困惑关于组件的一个JPanel显示。
假设,如果我创建0.8f的半透明定制的JPanel如下: -
的JPanel面板=新JPanel(){
@覆盖
公共无效漆(图形G)
{
super.paint(G);
IMG的BufferedImage =(BufferedImage的)的createImage(的getWidth()的getHeight());
Graphics2D的G2 =(Graphics2D的)g.create();
g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
g2.drawImage(IMG,0,0,NULL);
}
@覆盖
公共尺寸的get preferredSize()
{
返回新的Dimension(300,300);
}
};
现在我将其设置为框架的contentPane。
frame.setContentPane(面板);
现在我一些按钮添加到它。
frame.add(的新的JButton(点击这里));
frame.add(的新的JButton(点击这里));
frame.add(的新的JButton(点击这里));
frame.add(的新的JButton(点击这里));
1),然后在输出为什么我得到半透明按钮?由于JPanel的是单层和我第一次画的半透明图像时我overrided其油漆
方法,然后按键分别为补充说,该按钮不得半透明的,因为他们要过来了。
2)还掉那些4个按键只有2个是半透明的。为什么会出现这种偏袒?
3)如果我也加入这些4个按钮,然后一切就变得translucent.Why前添加一个表?
对象[] =名新的对象[] {
标题,艺术家,专辑
};
的String [] []数据=新的String [] [] {
{洛杉矶,Sugarcult,熄灯},
{独自做到这一点,Sugarcult,熄灯},
{犯了错误,Sugarcult,熄灯},
{吻你更好,马克西莫公园,某个触发因素},
{遍铺,马克西莫公园,某个触发因素},
{失踪,马克西莫公园,某个触发因素}
};
JTable的表=新的JTable(数据名称);
frame.add(表);
4)如果我使用的paintComponent(图形G)
的JPanel中再没有什么是半透明的,我是否添加表或不作为,或许多按钮都added.Why?
(我问什么时候应用是run.I知道立即输出,当鼠标滑过那些按钮,或者如果表中的任何行点击它时会变成不透明,这是由于Swing的绘画机制。)
Actually, you painted the translucent effect OVER the buttons. paint
calls
paintComponent
paintBorder
paintChildren
Then you painted your translucent effect OVER the top of what has already been painted (the children). It will make no difference when you add the components, Swing will paint the component under a number of circumstances, the first been when the component is first realised (made visible on the screen) and in response to changes to the dirty regions (and lots of others)...don't fool your self, you have no control over this...
Think of the paint process as a layering approach. First you paint the background, then you paint the mid ground and then finally, the fore ground, which then you went and splashed over...
public class TestTranslucentControls {
public static void main(String[] args) {
new TestTranslucentControls();
}
public TestTranslucentControls() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage background;
public TestPane() {
setLayout(new GridBagLayout());
try {
background = ImageIO.read(new File("C:/Users/shane/Dropbox/MegaTokyo/poster.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
add(new JButton("1"));
add(new JButton("2"));
add(new JButton("3"));
add(new JButton("4"));
add(new JButton("5"));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.SrcOver.derive(0.25f));
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2.drawImage(background, x, y, this);
g2.dispose();
}
}
@Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(300, 300) : new Dimension(background.getWidth(), background.getHeight());
}
}
}
I think you may find...
useful ;)
这篇关于如何内容一个JPanel是用半透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!