绘制带有 JLabel 的 JPanel 渐变背景时出现问题
我有以下代码来绘制 JPanel 的背景:
@Override
public void paintComponent(Graphics g) {
UIDefaults uid = UIManager.getDefaults();
Graphics2D g2d = (Graphics2D)g;
Dimension d = this.getSize();
g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.light"), 0, d.height, uid.getColor("ToolBar.shadow"), true));
g2d.fillRect(0, 0, d.width , d.height);
g2d.setColor(uid.getColor("ToolBar.light"));
g2d.drawLine(0, d.height-2, d.width, d.height-2);
g2d.setColor(uid.getColor("ToolBar.highlight"));
g2d.drawLine(0, d.height-1, d.width, d.height-1);
}
我在这个 JPanel 中有一个 JLabel (不透明 = false),但 JLabel 的背景仍然是某种灰色。如何在 JPanel 背景上绘制渐变,并为 JLabel 提供透明背景?
完整的工作示例如下:
DemoFrame.java:
import javax.swing.UIManager;
public class DemoFrame extends javax.swing.JFrame {
public DemoFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
panelHeader = new Extras.JPanelHeader();
labelTitle = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
panelHeader.setOpaque(false);
panelHeader.setPreferredSize(new java.awt.Dimension(172, 29));
panelHeader.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
labelTitle.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
labelTitle.setText("Criar Novo Utilizador");
labelTitle.setOpaque(true);
panelHeader.add(labelTitle);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelHeader, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panelHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(83, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DemoFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel labelTitle;
private Extras.JPanelHeader panelHeader;
// End of variables declaration
}
JPanelHeader.java:
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
public class JPanelHeader extends JPanel {
@Override
public void paintComponent(Graphics g) {
UIDefaults uid = UIManager.getDefaults();
Graphics2D g2d = (Graphics2D)g;
Dimension d = this.getSize();
g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.shadow"), 0, d.height, uid.getColor("ToolBar.background"), false));
g2d.fillRect(0, 0, d.width , d.height);
g2d.setColor(uid.getColor("ToolBar.light"));
g2d.drawLine(0, d.height-2, d.width, d.height-2);
g2d.setColor(uid.getColor("ToolBar.highlight"));
g2d.drawLine(0, d.height-1, d.width, d.height-1);
}
}
I have the following code to paint a JPanel's background:
@Override
public void paintComponent(Graphics g) {
UIDefaults uid = UIManager.getDefaults();
Graphics2D g2d = (Graphics2D)g;
Dimension d = this.getSize();
g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.light"), 0, d.height, uid.getColor("ToolBar.shadow"), true));
g2d.fillRect(0, 0, d.width , d.height);
g2d.setColor(uid.getColor("ToolBar.light"));
g2d.drawLine(0, d.height-2, d.width, d.height-2);
g2d.setColor(uid.getColor("ToolBar.highlight"));
g2d.drawLine(0, d.height-1, d.width, d.height-1);
}
I have a JLabel inside this JPanel (with opaque = false) but the JLabel's background is still some kind of gray. How can I paint a gradient on this JPanel background with a transparent background for the JLabel?
Full working example below:
DemoFrame.java:
import javax.swing.UIManager;
public class DemoFrame extends javax.swing.JFrame {
public DemoFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
panelHeader = new Extras.JPanelHeader();
labelTitle = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
panelHeader.setOpaque(false);
panelHeader.setPreferredSize(new java.awt.Dimension(172, 29));
panelHeader.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
labelTitle.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
labelTitle.setText("Criar Novo Utilizador");
labelTitle.setOpaque(true);
panelHeader.add(labelTitle);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelHeader, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panelHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(83, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DemoFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel labelTitle;
private Extras.JPanelHeader panelHeader;
// End of variables declaration
}
JPanelHeader.java:
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
public class JPanelHeader extends JPanel {
@Override
public void paintComponent(Graphics g) {
UIDefaults uid = UIManager.getDefaults();
Graphics2D g2d = (Graphics2D)g;
Dimension d = this.getSize();
g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.shadow"), 0, d.height, uid.getColor("ToolBar.background"), false));
g2d.fillRect(0, 0, d.width , d.height);
g2d.setColor(uid.getColor("ToolBar.light"));
g2d.drawLine(0, d.height-2, d.width, d.height-2);
g2d.setColor(uid.getColor("ToolBar.highlight"));
g2d.drawLine(0, d.height-1, d.width, d.height-1);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你一定使用了某种奇怪的 LAF。如果是这种情况,那么应该将其发布在您的问题中。事实上,SSCCE 应该与每个问题一起发布,以便我们可以准确地看到您在做什么。
对背景使用 alpha 值可能会导致问题,如 背景透明度。
这是一个简单的 SSCCE,它表明您不需要执行任何特殊操作即可正确绘制标签:
You must be using some wierd LAF. If that is the case then that should be posted in your question. In fact a SSCCE should be posted with every question so we can see exactly what you are doing.
Using an alpha value for backgrounds can cause problems as is demonstrated by Backgrounds With Transparency.
Here is a simple SSCCE that shows you don't need to do anything special to have the label paint properly: