【问题标题】:How to make Round JButtons in Java如何在 Java 中制作圆形 JButton
【发布时间】:2014-11-20 01:46:58
【问题描述】:

首先,我是一名 Web 开发人员和一名 Java 程序员新手。 我的老板要我在应用程序中制作这个按钮:

我的自定义按钮类必须扩展 JButtonBasicButtonUI 以便可以重复使用。

我对 Stack Overflow 做了一些研究,但我不明白答案,尤其是在我老板的时间限制下。

【问题讨论】:

  • First, I am Web Developer - 之前的研究很差,不知道谁赞成这个问题,不需要任何图形,只需要边框并覆盖来自 ButtonModel 的事件
  • 什么...谁赞成这个? :D 你确定有nothing here for you?? 请记住,StackOverflow 不是为你完成工作的地方。请概述您迄今为止尝试过的内容以及您遇到的问题。还要尝试提供为什么要扩展 JButton 并且不要急于求成的原因,这里的事情不会匆忙完成
  • make a button round的可能重复

标签: java swing jbutton custom-component rounded-corners


【解决方案1】:

您应该为此创建自己的组件。

覆盖 JPanel 上的 paintComponent 方法,并在 paintComponent 方法内部绘制(即填充)灰色的圆角矩形 2D:

RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(x, y, w, h, 10, 10);
g.fill(roundedRectangle);

(最后两个值决定曲率。玩弄直到你得到你想要的)

现在移动 x,y 并减小 widthheight 以便在绘制下一个矩形时,它位于灰色矩形内.将图形颜色设置为蓝色,然后执行以下操作:

RoundRectangle2D roundedRectangle2 = new RoundRectangle2D.Float(x + 5, y + 5, w - 10, h - 10, 10, 10);
g.fill(roundedRectangle2);

您还需要添加文本。添加文本需要 x 和 y 位置。精确的 x 和 y 位置可能难以计算,因此您可能需要使用FontMetrics 来获取有关字符串矩形形状的更多信息。 Fontmetrics 有像 stringWidth() 和 getHeight() 这样的方法,可以帮助你确定你的 x 和 y 应该是什么。

g.drawString("Click Me", x, y);

最后,您需要在面板上安装鼠标移动监听器。监听器需要找到鼠标在按钮上的时间,然后重绘组件。

您的矩形可以转换为shape 对象,并且可以计算鼠标是否在该形状中。例如:

shape.contains(x,y)

如果包含,更改颜色,然后在面板上调用 repaint()updateUI()

注意:您的颜色对象应作为类中的类级别字段保存,以便可以通过鼠标悬停进行更改。

希望这会有所帮助!

【讨论】:

  • 好吧,让我试一试
  • 我还可以添加更多内容。但是,如果我的回答还不够,请告诉我。
  • 请让我知道还有什么其他的事情,这样我可能会做得更好。谢谢
  • 你坚持哪一部分?您是否尝试过在面板上覆盖 drawComponent(..)?
  • 还要确保抗锯齿已打开,否则看起来很糟糕:g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
【解决方案2】:

如果您不想自己使用图形 API 绘制图像,或者您不能因为图像来自图形设计师,那么您可以将它们用作 ImageIcon 对象并使用 setRolloverIcon() 和 @ 987654323@.

在这种情况下,我会这样做

class ButtonRollover {

    private String normalImagePath;
    private String rolloverImagePath;

    public ButtonRollover(String normalImagePath, String rolloverImagePath) {
        this.normalImagePath = normalImagePath;
        this.rolloverImagePath = rolloverImagePath;
    }

    public void apply(AbstractButton abstractButton) {
        abstractButton.setBorderPainted(false);
        abstractButton.setBackground(new Color(0, 0, 0, 0));
        abstractButton.setRolloverIcon(createImageIcon(rolloverImagePath));
        abstractButton.setIcon(createImageIcon(normalImagePath));
    }

    private ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

然后使用它。例如

public class Main extends JFrame {

    public static void main(String[] args) {
        Main main = new Main();
        main.setBackground(Color.WHITE);
        main.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        main.setSize(640, 480);

        Container contentPane = main.getContentPane();

        ButtonRollover buttonRollover = new ButtonRollover("/bt_normal.png",
                "/bt_hover.png");

        JButton btn = new JButton();
        buttonRollover.apply(btn);

        contentPane.add(btn);
        main.setVisible(true);
    }
}

只需将图像文件放在类路径中即可。

【讨论】:

    【解决方案3】:

    有办法做到这一点。

    1) JButton 具有内置 API setIcon。您可以在此处设置 ImageIcon。

    2)您可以添加鼠标监听器(鼠标进入,鼠标退出)将图标更改为所需的。

    3) Make a button round - 参考创建弯曲按钮。

    【讨论】:

      【解决方案4】:
      public class Main extends JFrame {
      
          public static void main(String[] args) {
              Main main = new Main();
              main.setBackground(Color.WHITE);
              main.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              main.setSize(640, 480);
      
              Container contentPane = main.getContentPane();
      
              ButtonRollover buttonRollover = new ButtonRollover("/bt_normal.png",
                      "/bt_hover.png");
      
              JButton btn = new JButton();
              buttonRollover.apply(btn);
      
              contentPane.add(btn);
              main.setVisible(true);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-07-04
        • 2015-10-04
        • 1970-01-01
        • 1970-01-01
        • 2022-12-09
        • 1970-01-01
        • 1970-01-01
        • 2012-02-04
        • 1970-01-01
        相关资源
        最近更新 更多