所以我在背景图像顶部有几个 JButton,它们被设置为透明的,因此可以看到背景。但是,当我单击按钮时,在单击鼠标按钮时按钮所在的位置会出现一个灰色方块。这看起来有点难看,我想知道是否有任何方法可以禁用该动画?谢谢
问问题
2690 次
1 回答
1
胡乱猜测...,您可以尝试一种JPanel#setOpaque(false)
方法:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PanelOpaqueTest {
public JComponent makeUI() {
JPanel p1 = createPanel();
p1.setBorder(BorderFactory.createTitledBorder("JPanel#setOpaque(false)"));
p1.setOpaque(false);
p1.add(createButton());
JPanel p2 = createPanel();
p2.setBorder(BorderFactory.createTitledBorder("JPanel#setOpaque(true)"));
p2.setOpaque(true);
p2.add(createButton());
JPanel p = new JPanel(new GridLayout(2,1));
p.add(p1);
p.add(p2);
return p;
}
protected JButton createButton() {
JButton b = new JButton("button");
b.setContentAreaFilled(false);
b.setFocusPainted(false);
//b.setBorderPainted(false);
//b.setOpaque(false);
return b;
}
protected JPanel createPanel() {
return new JPanel() {
@Override public void paintComponent(Graphics g) {
g.setColor(new Color(100,100,100,100));
g.fillRect(0,0,getWidth(),getHeight());
}
};
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new PanelOpaqueTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
于 2013-06-03T06:41:37.483 回答