我有以下代码来绘制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(opaque = 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);
}
}
答案 0 :(得分:2)
你必须使用一些奇怪的LAF。如果是这种情况,那么应该在您的问题中发布。事实上,每个问题都应该张贴SSCCE,这样我们就能确切地看到你在做什么。
对背景使用alpha值会导致问题,如Backgrounds With Transparency所示。
这是一个简单的SSCCE,它表明你不需要做任何特别的事情就可以正确地绘制标签:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FaceComponent2 extends JPanel
{
protected 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);
}
public static void main(String args[])
throws Exception
{
JComponent face = new FaceComponent2();
face.setLayout( new GridLayout(0, 1) );
face.setPreferredSize( new Dimension(250, 250) );
face.add( new JLabel("Line1") );
face.add( new JLabel("Line2") );
face.add( new JLabel("Line3") );
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add( face );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}