【问题标题】:Override JButton paintComponent() doesn't work java覆盖 JButton paintComponent() 不起作用 java
【发布时间】:2012-02-27 07:56:07
【问题描述】:

我想绘制我自己的 JButton 版本,所以我重写了 paintComponent() 方法,并绘制了一个渐变圆形矩形。这可行,但在那之后,我想在它上面绘制按钮的字符串,并且在编译时,我没有收到任何错误消息。但是在运行时,我只看到了roundRect,渐变,就像我想要的那样(我也可以点击它),但是String是不可见的......

这是我的代码:

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);
    }
}

【问题讨论】:

  • 如果在绘制渐变之后绘制字符串会怎样?虽然更好的方法可能是简单地使用按钮的图像创建一个 BufferedImage,但从中制作一个 ImageIcon 并设置按钮的图标。虽然我认为你需要摆脱边距和边框才能成功。
  • 我想要mouseListener 来为mouseIn、mousePressed 等设置动画,但这很管用,现在我只想绘制那个字符串。
  • @HovercraftFullOfEels 我先试过了,但没用...
  • 它对我有用。请参阅下面发布的示例。请注意,您不能使用 0, 0 作为文本的位置
  • 为什么不呢? @HovercraftFullOfEels

标签: java swing graphics paint jbutton


【解决方案1】:

1) 最简单的方法是 JButton 的方法 JButton(String text, Icon icon) 示例 here

2) 你可以覆盖XxxButtonUI,或者更改GradientButton

【讨论】:

    【解决方案2】:

    根据我的评论,“它对我有用......”
    例如:

       @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);
       }
    

    【讨论】:

    • 比带有 MouseListener 的 JLabel 更好 :-) +1
    • 为什么注释super.paintComponent?
    【解决方案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();
    }
    

    【讨论】:

      【解决方案4】:

      只需为您的 JButton 调用 setContentAreaFilled(false),覆盖 paint() 方法,最后调用 paint() 方法 super.paint()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-09
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多