我扩展了 JButton 类来制作我自己的自定义版本,但现在我不知道如何让 JLabel 出现在它上面。在普通按钮中,JLabel 被绘制在其顶部,但我不知道如何复制该行为。知道怎么做吗?
这是我覆盖的paintComponent 方法:
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
boolean isSelected = getModel().isPressed();
Color topColor = !isSelected ? BUTTON_TOP_GRADIENT : BUTTON_TOP_GRADIENT.darker();
Color bottomColor = !isSelected ? BUTTON_BOTTOM_GRADIENT : BUTTON_BOTTOM_GRADIENT.darker();
Graphics2D g2 = (Graphics2D)g.create();
RenderingHints qualityHints =
new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(qualityHints);
g2.setPaint(new GradientPaint(
new Point(0, 0),
topColor,
new Point(0, getHeight()),
bottomColor));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), BUTTON_CORNER_RADIUS, BUTTON_CORNER_RADIUS);
g2.setColor(Color.BLACK);
g2.dispose();
// Where I need to draw the JLabel
}