JDK7半透明+ jframe + jcombobox

时间:2012-03-24 08:21:21

标签: java swing jcombobox java-7 translucency

当我在jFrame中使用半透明的jComboBox时似乎有问题。当我单击下拉列表时,白色背景似乎覆盖其他组件,但下拉列表中的项目未显示。每个项目只有在我的鼠标悬停在它上面时才会出现。边界似乎也不存在。

对此有何解决方法?

谢谢!

1 个答案:

答案 0 :(得分:2)

我也面临同样的问题。

快速修复:

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class WindowOpaqueTest2{
  public JComponent makeUI() {
    JPanel p = new JPanel();
    p.setBackground(new Color(.5f,.5f,.5f,.5f));
    p.add(new JComboBox<String>(new String[] {"aaa", "bb", "c"}));
    JComboBox c = new JComboBox<String>(new String[] {"aaa", "bb", "c"});
    c.addPopupMenuListener(new PopupMenuListener() {
      @Override public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() {
            JComboBox c = (JComboBox)e.getSource();
            Object o = c.getAccessibleContext().getAccessibleChild(0);
            if(o instanceof JComponent) { //BasicComboPopup
              ((JComponent)o).repaint();
            }
          }
        });
      }
      @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
      @Override public void popupMenuCanceled(PopupMenuEvent e) {}
    });
    p.add(c);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() { createAndShowGUI(); }
    });
  }
  public static void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame f = new JFrame();
    //com.sun.awt.AWTUtilities.setWindowOpaque(f, false);
    f.setBackground(new Color(0,true)); //1.7.0
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new WindowOpaqueTest2().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}