在我的程序中,我想在JFrame上使用一个半透明的白色到透明的渐变来覆盖黄色背景。这个方案有效,由于程序设置原因,必须是从白色到透明。然而,当我把程序带到大学(从JRE7到我的JRE6),渐变会从白色到黑色再到透明……直到你开始增加白色颜色的不透明度才会变得更糟糕。有没有办法可以解决这个问题?
以下是我JFrame代码的相关部分。
以下是我JFrame代码的相关部分。
public class DictionaryGUI extends JFrame
{
protected JPanel pGradientPane;
//Interface gradient specification
private Color pInterfaceColour = new Color(255, 245, 62);
protected int iDegreeWhite = 180
protected int iDegreeBlack = 0
DictionaryGUI(int iWidth, int iHeight)
{
/*General definitions*/
super(String.format("French Verb Conjugator - Version %s", MainLauncher.version));
setSize(iWidth, iHeight);
new Menu(this);
this.iWidth = iWidth;
this.iHeight = iHeight;
getContentPane().setBackground(pInterfaceColour);
pGradientPane = new JPanel(new GridBagLayout())
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics pGraphics)
{
Graphics2D pGraphicsGradientRender = (Graphics2D) pGraphics;
pGraphicsGradientRender.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint pGradient = new GradientPaint(0, 0, new Color(255, 255, 255, iDegreeWhite), 0, getHeight(), new Color(0, 0, 0, iDegreeBlack));
pGraphicsGradientRender.setPaint(pGradient);
pGraphicsGradientRender.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(pGraphics);
}
};
pGradientPane.setOpaque(false);
pGradientPane.setPreferredSize(new Dimension(iWidth - 16, iHeight - 62));
/*components added to pGradientPane here!*/
add(pGradientPane);
}
以及主类也是:
public class MainLauncher
{
static int iHeight = 400;
static int iWidth = 730;
static String version = "0A3B6";
public static void main(String[] args)
{
try
{
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {e.printStackTrace();}
DictionaryGUI window = new DictionaryGUI(iWidth, iHeight);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationByPlatform(true);
window.setVisible(true);
}
这只是JRE6和JRE7之间的一些差异吗?我也应该把底部的颜色改成白色吗?(原本是黑色,以防有人想要使底部变暗。)
如果有人需要,我可以明天发布一些截图....
谢谢 Jamie
编辑: 我将渐变中的第二个(透明的)颜色改为了白色,并解决了问题。但是,我仍然困扰于为什么透明黑色的颜色会在中间显示出来?这一定与JRE7有关,因为它发生在那里...也许他们改变了渐变中透明度工作方式的某些内容。有人知道如何在保持黑色的情况下消除这个问题吗?