我不想在按下时更改JButton的颜色 [英] I don't want to change color of JButton when pressed

查看:113
本文介绍了我不想在按下时更改JButton的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Color newColor = new Color(197,222,90);
JButton newButton;
newButton = new JButton(icon);
newButton.setBacgroundColor(newColor);

按下时会更改颜色。我怎样才能防止它变色。我有多个按钮,所以如果在一排或两排中有解决方案,请帮我,并记住我是新手,编写一些巨大的类对我没有帮助,因为我有多个名称不同的按钮会受此影响。

When it is pressed it changes color. How can i keep it from changing color. I have multiple buttons, so if there is solution in one or two rows pls help me, and keep in mind that I'm beginner, writing some huge classes wont help me, because i have multiple buttons with different name to be affected with this.

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

Solution in one line is:

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

但是它会更改所有按钮的颜色,但是我需要另一个按钮来具有不同的颜色。

But it changes all button color but i need another to have different color.

编辑2:经过一番研究,我发现没有简单的解决方案(但应该是,仅此而已)。我如何看待,我有2个解决方案:1.打破按钮以分离类并为其设置UIManager,其次是制作自定义按钮。对于按钮来说,这太繁琐了(ffs只是一个按钮)。

After some research I figured out there isn't easy solution (but it should be, ffs it is just a button). How I see it i have 2 solution, 1. is to break buttons to separate classes and set UIManager for them, and second is to make custom buttons. It is just too much work for button (ffs it is only a button).

推荐答案

我发现没有什么可以改变的普通JButton上的特定行为。问题是,无论您在动作侦听器中为该按钮写什么,都会在您放开鼠标按钮而不是单击时之后发生。

I've found nothing that can change that particular behavior on a normal JButton. The problem being, that whatever you write in your actionlistener for the button, will occur AFTER you've let go of the mousebutton, and not "while clicking".

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

My preferred choice is, to remove all graphics from the button, and then add your own images to the button's regular and pressed states. You could take a screenshot of your GUI, cut out the button, and set that image to be both states.

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

很明显,有很多方法可以保留和加载图像,但是由于这不是问题,我

Obviously there are many ways to retain and load an image, but since that's not the issue here, I'll leave additional methods out of it.

不过,没有必要无数次地遍历它。编写自己的JButton类的自定义实现应该非常容易,在该实现中,自定义构造函数接受单个参数(即BufferedImage),然后构造函数相应地对其进行设置(更改图标)。然后,在创建新的JButton时,您要做的就是使用自己的类,并向其传递图像:

There's no need to go through it all countless times, though. It should be pretty easy to write your own custom implementation of the JButton class, in which a custom constructor takes a single parameter, being the BufferedImage, and then the constructor sets it up accordingly (changes the icons). Then all you have to do when you create a new JButton, is to use your own class, and pass it an image:

JButton btn = new MyCustomJButton(myImage);

您也可以很轻松地与很少的图像相处。您只需要一个HashMap,其中包含所有图像,并使用String作为键。想象一下,您需要4个OK按钮。您使按钮的单个图像上写有文本 OK。然后,将该图像放入HashMap中,如下所示:

You could also easily get along with very few images. All you need is a HashMap which holds all the images, with a String as a key. Imagine you need 4 OK-buttons. You make a single image of a button with the text "OK" written on it. Then you put that image into the HashMap, like so:

myMap.put("OK", myImage);

然后,您可以在创建按钮时执行此操作,如果需要,可以一遍又一遍:

Then you could do this when creating a button, over and over again if you'd like more:

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

或者:
实现此目的的另一种方法是另一篇文章的答案中介绍了相当精致但又可能被认为是正确方法的按钮UI。

Alternatively: Another way of achieving this, which is pretty elaborate, but probably considered "the right way", is to use ButtonUI, as presented in this answer to another post.

这篇关于我不想在按下时更改JButton的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆