【问题标题】:I don't want to change color of JButton when pressed我不想在按下时改变 JButton 的颜色
【发布时间】:2022-05-02 11:30:11
【问题描述】:
Color newColor = new Color(197,222,90);
JButton newButton;
newButton = new JButton(icon);
newButton.setBacgroundColor(newColor);

当它被按下时,它会改变颜色。我怎样才能让它不变色?我有多个按钮,所以如果有一两行有解决方案,请帮助我,并记住我是初学者,写一些大类对我没有帮助,因为我有多个不同名称的按钮会受到影响有了这个。

编辑:一行的解决方案是:

 UIManager.put("Button.select", newColor);

但它会改变所有按钮的颜色,但我需要另一种颜色才能有不同的颜色。

EDIT2:经过一些研究,我发现没有一个简单的解决方案(但应该是)。我如何看待它我有 2 个解决方案,1. 将按钮分解为单独的类并为它们设置 UIManager,第二个是制作自定义按钮。按钮的工作量太大了。

【问题讨论】:

    标签: colors jbutton pressed


    【解决方案1】:

    我没有发现任何东西可以改变普通 JButton 上的特定行为。问题是,无论您在按钮的 actionlistener 中写什么,都会在您松开鼠标按钮后发生,而不是“在单击时”。

    不过,有一些解决方法。

    我的首选是从按钮中删除所有图形,然后将您自己的图像添加到按钮的常规和按下状态。您可以截取 GUI 的屏幕截图,剪下按钮,然后将该图像设置为两种状态。

    JButton myButton = new JButton();
    
    // Sets button x, y, width, height. Make the size match the image.
    myButton.setBounds(5, 30, 100, 30);
    
    // Remove border-graphics.
    myButton.setBorder(null);
    
    // Remove default graphics from the button
    myButton.setContentAreaFilled(false);
    
    // Remove the focus-indicating dotted square when focused (optional)
    myButton.setFocusPainted(false);
    
    // Here, myImage is a simple BufferedImage object.
    // You can set one like this, provided you have an "images" package,
    // next to your main class (ex: com.somecompany.someprogram.images),
    // that contains an image:
    
    BufferedImage myImage = ImageIO.read(getClass().getResource("images/myImage.png"));
    
    // Then we simply apply our image to both states for the button, and we're done.
    myButton.setIcon(new ImageIcon(myImage));
    
    myButton.setPressedIcon(new ImageIcon(myImage));
    

    显然有很多方法可以保留和加载图像,但由于这不是这里的问题,所以我将不再使用其他方法。

    不过,没有必要经历无数次。编写自己的 JButton 类的自定义实现应该很容易,其中自定义构造函数采用单个参数,即 BufferedImage,然后构造函数相应地对其进行设置(更改图标)。那么当你创建一个新的 JButton 时,你所要做的就是使用你自己的类,并传递一个图像:

    JButton btn = new MyCustomJButton(myImage);
    

    您也可以轻松地与很少的图像相处。您所需要的只是一个包含所有图像的 HashMap,并以字符串为键。想象一下,您需要 4 个确定按钮。您制作一个按钮的单个图像,上面写有“OK”文本。然后将该图像放入 HashMap 中,如下所示:

    myMap.put("OK", myImage);
    

    然后你可以在创建按钮时这样做,如果你想要更多的话,一遍又一遍:

    JButton btn = new MyCustomJButton(myMap.get("OK"));
    

    或者: 另一种实现这一点的方法非常精细,但可能被认为是“正确的方法”,即使用 ButtonUI,如 this answer to another post.

    中所述

    【讨论】:

    • 这是我一直在寻找的简单而好的解决方案。
    【解决方案2】:

    如果 OP 指的是按下鼠标时带有图标的按钮上的背景颜色的临时更改,则以下语句可以解决问题:

    button.setContentAreaFilled(false);

    “如果您希望有一个透明按钮,例如仅图标按钮,那么您应该将其设置为 false。”

    我花了很长时间才弄清楚。它似乎是一种鲜为人知的技术,也许是因为它的名字几乎没有说明它的作用。

    【讨论】:

      【解决方案3】:

      只有第一条车道我们仍然可以看到它被点击了。您需要将这两者结合起来:

      button1.setContentAreaFilled(false);
      button1.setEnabled(false);
      

      如果你不想用灰色,你可以在他下面放另一个按钮。

      panelname.add(button1,+5,+5); \\(first not clicable, not visible button, notice +5) 
      

      面板名称.add(button2,-5,-5); \(-5,-5 表示在面板下 5 个点)

      【讨论】:

        猜你喜欢
        • 2018-09-28
        • 1970-01-01
        • 2014-09-24
        • 1970-01-01
        • 2016-05-08
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        相关资源
        最近更新 更多