就像标题所说的那样-我有一个JPanel,如果不活动,我想让它的不透明度为90%,如果鼠标悬停,则为完全不透明度。
我已经有了单独的组件工作-鼠标在上面和外面是OK的,我可以改变不透明度从覆盖paintComponent
方法,但我想我有问题与MouseAdapter
和paintComponent
之间的连接。
(顺便说一句,抱歉把代码留在家里了,所以现在没有例子:(除非它不能快速解决,否则我明天会抛出一些代码)
如果您要覆盖paintComponent
方法,您应该能够向JPanel
扩展添加一个不透明度变量。在MouseAdapter
中修改这个(使用mutator)。然后在重写的paintComponent
方法中引用该变量来确定如何绘制。
如果你已经覆盖了paintComponent方法,我建议你让MouseAdapter成为Panel的一个匿名内部类,并让它操作一个私有布尔标志。
public class FadingPanel extends JPanel
{
private boolean active;
public FadingPanel()
{
createMouseAdapter();
}
private void createMouseAdapter()
{
this.addMouseListener(new MouseAdapter()
{
// your mouseadapter code here toggling the active flag
}
}
@Override
public boolean paintComponent(Graphics g)
{
if(active)
{
super.paintComponent(g);
}
else
{
// your semitransparent painting code here
}
}
}
哎呀,我没有Haase和Guy的书,我不记得他们推荐的半透明组件的编码方式,但我猜重写paint或paintComponent工作,除了重写paint将从一开始就显示对子组件的影响。例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OpaqueJPanels {
private static void createAndShowUI() {
JPanel mainPanel = new JPanel(new GridLayout(1, 0));
mainPanel.add(new OpaqueJPanel(false, "Override paintComponent"));
mainPanel.add(new OpaqueJPanel(true, "Override paint"));
JFrame frame = new JFrame("Opaque JPanels");
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class OpaqueJPanel extends JPanel {
private static final int OJP_WIDTH = 250;
private static final int OJP_HEIGHT = OJP_WIDTH;
private static final Composite TRANSLUSCENT_COMPOSITE = AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 0.4f);
private static final Composite NON_TRANSLUSCENT_COMPOSITE = AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 1.0f);
private boolean overridePaint;
private boolean transluscent = true;
public OpaqueJPanel(boolean overridePaint, String title) {
add(new JButton("Button"));
setBorder(BorderFactory.createTitledBorder(title));
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
transluscent = false;
getParent().repaint();
}
@Override
public void mouseExited(MouseEvent e) {
if (!OpaqueJPanel.this.contains(e.getPoint())) {
transluscent = true;
getParent().repaint();
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(OJP_WIDTH, OJP_HEIGHT);
}
@Override
public void paint(Graphics g) {
if (!overridePaint) {
super.paint(g);
return;
}
Graphics2D g2 = (Graphics2D) g;
Composite composite = transluscent ? TRANSLUSCENT_COMPOSITE
: NON_TRANSLUSCENT_COMPOSITE;
g2.setComposite(composite);
super.paint(g);
}
@Override
protected void paintComponent(Graphics g) {
if (overridePaint) {
super.paintComponent(g);
return;
}
Graphics2D g2 = (Graphics2D) g;
Composite composite = transluscent ? TRANSLUSCENT_COMPOSITE
: NON_TRANSLUSCENT_COMPOSITE;
g2.setComposite(composite);
super.paintComponent(g);
}
}
我还发现,如果我重新绘制JPanel的父组件而不是JPanel本身,效果会更好。