非常像标题所说 - 我有一个JPanel,如果不活动,我希望它具有90%的不透明度,如果被淘汰则完全不透明。
我有单独的组件工作 - 鼠标是否正常,我可以通过覆盖paintComponent
方法来改变不透明度,但我想我在{{{}之间的连接有问题1}}和MouseAdapter
。
似乎有一种非常简单的方法可以做到这一点 - 任何提示?
(顺便说一句,抱歉把代码留在家里,所以现在没有例子:(除非它不能快速解决,否则明天我会抛出一些代码。)
答案 0 :(得分:4)
如果要覆盖paintComponent
方法,则应该能够向JPanel
扩展名添加不透明度变量。在MouseAdapter
中修改它(使用mutators)。然后在重写的paintComponent
方法中引用此变量以确定如何绘制。
答案 1 :(得分:3)
如果你已经覆盖了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
}
}
}
答案 2 :(得分:3)
拍摄,我没有Haase and Guy的书,我不记得他们建议编写半透明组件的方式,但我想重写paint或paintComponent工作,除了覆盖油漆将显示从一开始就对子组件产生影响。例如:
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本身会更好。