I don't want to change color of JButton when pressed

。_饼干妹妹 提交于 2020-01-06 15:33:05

问题


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.

EDIT: Solution in one line is:

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

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

EDIT2: 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).


回答1:


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".

There are workarounds, however.

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.

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

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

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.




回答2:


If the OP is referring to the temporary change of background colour on a button with an icon at the moment the mouse is pressed, the following statement does the trick:

button.setContentAreaFilled(false);

"If you wish to have a transparent button, such as an icon only button, for example, then you should set this to false."

This took me a long time to figure out. It seems to be a little known technique, perhaps since its name gives little clue as to its effect.




回答3:


With only first lane we can still see that it is clicked. You need to combine those two:

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

and if you don't wanna in grey color you put another button under him.

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

panelname.add(button2,-5,-5); \(-5,-5 means it is 5 points under panel)



来源:https://stackoverflow.com/questions/20425752/i-dont-want-to-change-color-of-jbutton-when-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!