覆盖JButton的paintComponent()方法不起作用 java

3

我想自己画一个JButton的版本,所以我重写了paintComponent()方法,并画了一个渐变圆角矩形。这个方法可以正常工作,但是之后,我想在上面绘制Button的字符串,在编译时没有错误信息。但是在运行时,我只看到了圆角矩形和渐变,就像我想要的那样(我也可以点击它),但是字符串是不可见的...

以下是我的代码:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class JIconButton extends JButton implements MouseListener
{
    private boolean mouseInside;
    public JIconButton(String file, String text)
    {
        super(text, new ImageIcon(file));
        setBorder(new LineBorder(Color.LIGHT_GRAY, 0, true));
        setContentAreaFilled(false);
        setFocusPainted(false);
        addMouseListener(this);
        setVisible(true);
    }

    public void mouseClicked(MouseEvent e)
    {

    }

    public void mouseEntered(MouseEvent e)
    {

    }

    public void mouseExited(MouseEvent e)
    {

    }

    public void mousePressed(MouseEvent e)
    {

    }

    public void mouseReleased(MouseEvent e) 
    {

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setPaint(Color.BLACK);
        g2.drawString(getText(), 0, 0);
        g2.setPaint(new GradientPaint(
                new Point(0, 0), 
                Color.WHITE, 
                new Point(0, getHeight()), 
                Color.PINK.darker()));
        g2.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30);
        g2.dispose();

        //super.paintComponent(g);
    }
}

我想要使用mouseListener来实现鼠标进入、鼠标按下等动画效果,但这已经实现了,现在我只想绘制那个字符串。 - 11684
2
对我来说有效。请参考下面的示例。请注意,您不能将0, 0作为文本的位置 - Hovercraft Full Of Eels
为什么不呢?@HovercraftFullOfEels - 11684
@HovercraftFullOfEels 你是对的,0,0 是一个在我的按钮上方的点。我现在已经修复了它,但你说另一个答案更好,但我并没有完全理解它... JButton 是否有 paintButtonPressed() 和 paintFocus 等等(是否继承),我能否重写它们来重新绘制我的按钮,如果它被按下、聚焦等等? - 11684
我会使用HovercraftFullOfEels的方法,带有if语句,或者类似的东西... - 11684
显示剩余8条评论
4个回答

6
根据我的评论,“it worked for me....”
例如:
   @Override
   protected void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setPaint(new GradientPaint(new Point(0, 0), Color.WHITE, new Point(0,
            getHeight()), Color.PINK.darker()));
      g2.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30);
      g2.setPaint(Color.BLACK);
      g2.drawString(getText(), 30, 12);
      g2.dispose();

      // super.paintComponent(g);
   }

1
比JLabel与MouseListener更好 :-) +1 - mKorbel
为什么注释掉了super.paintComponent? - nachokk

3

你需要做的是:

g2.drawString(getText(), 0, 10);

字符串坐标的 y 值必须大于 0,因为它是基线的起始点而不是一个框的左上角点。最终代码:

@Override
protected void paintComponent(Graphics g) {
  Graphics2D g2 = (Graphics2D) g.create();
  g2.setPaint(new GradientPaint(
  new Point(0, 0),
    Color.WHITE,
    new Point(0, getHeight()),
    color.PINK.darker()));
  g2.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30);
  // The drawString(string) must be put after the setPaint(gradient)
  g2.setPaint(Color.BLACK);
  g2.drawString(getText(), 0, 10);
  g2.dispose();
}

2

1
只需为您的JButton调用setContentAreaFilled(false),重写paint()方法,在paint()方法的最后调用super.paint()即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接