设置 Jbutton 的背景
我有 5 个 JButton:b1、b2、b3、b4、b5。 默认情况下,它们的颜色为灰色。 当我单击任何按钮时,该按钮的背景变为白色。 当我单击另一个按钮时,我希望先前单击的按钮将其背景更改为灰色,而新单击的按钮将其背景更改为白色。这是我编写的代码:
int liveButton = 0; //holds the value of the button that is last clicked.
//0 indicates no button clicked (in the beginning)
private void ChangeInUsersList(int clickedButton) {
switch(liveButton) {
case 1 : b1.setBackground(Color.GRAY);
break;
case 2 : b2.setBackground(Color.GRAY);
break;
case 3 : b3.setBackground(Color.GRAY);
break;
case 4 : b4.setBackground(Color.GRAY);
break;
case 5 : b5.setBackground(Color.GRAY);
break;
default: System.out.println("No button to change");
}
liveButton = clickedButton;// store the clicked button to change its
//background later
}
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(1);
b1.setBackground(new java.awt.Color(255,255,255));
}
private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(2);
b2.setBackground(new java.awt.Color(255,255,255));
}
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(3);
b3.setBackground(new java.awt.Color(255,255,255));
}
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(4);
b4.setBackground(new java.awt.Color(255,255,255));
}
private void b5ButtonActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(5);
b5.setBackground(new java.awt.Color(255,255,255));
}
但是,它没有按预期工作。当我点击一个按钮时,它的背景会变成白色。但是,如果我之后单击其他某个按钮,前一个按钮的背景不会变为灰色。我尝试用 new java.awt.Color(236,233,216) - 灰色的 rgb 替换 Color.GREY 但它仍然不起作用。
I have 5 JButtons: b1, b2, b3, b4, b5.
By default, their color is gray.
When I click on any button, the background of that button changes to white.
When I click another button, I want that previous clicked button to change its background to gray, and this newly clicked button to change its background to white. Here is the code that I wrote:
int liveButton = 0; //holds the value of the button that is last clicked.
//0 indicates no button clicked (in the beginning)
private void ChangeInUsersList(int clickedButton) {
switch(liveButton) {
case 1 : b1.setBackground(Color.GRAY);
break;
case 2 : b2.setBackground(Color.GRAY);
break;
case 3 : b3.setBackground(Color.GRAY);
break;
case 4 : b4.setBackground(Color.GRAY);
break;
case 5 : b5.setBackground(Color.GRAY);
break;
default: System.out.println("No button to change");
}
liveButton = clickedButton;// store the clicked button to change its
//background later
}
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(1);
b1.setBackground(new java.awt.Color(255,255,255));
}
private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(2);
b2.setBackground(new java.awt.Color(255,255,255));
}
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(3);
b3.setBackground(new java.awt.Color(255,255,255));
}
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(4);
b4.setBackground(new java.awt.Color(255,255,255));
}
private void b5ButtonActionPerformed(java.awt.event.ActionEvent evt) {
ChangeInUsersList(5);
b5.setBackground(new java.awt.Color(255,255,255));
}
However, its not working as expected. When i click on a button, its background does change to white. However, if i click on some other button after that, the former button's background doesnt change to grey. I tried replacing Color.GREY with new java.awt.Color(236,233,216) - the rgb for grey but it still doesnt work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您确实需要为按钮着色,然后将颜色设置回其原始默认状态(系统灰色),请使用
这将删除任何以前的颜色设置。
(我有一个应用程序,我需要单击几个按钮,跟踪我单击的按钮,然后当我执行一项功能时,我“取消单击”它们。我可以使用切换按钮,但是这一行更改为添加此功能比更改整个组件数组更容易。另外,UI“感觉”是正确的。)
If you ever do need to color a button and then set the color back to its original default state (system gray), use
This will remove any previous color setting.
(I have an application where I need to click a few buttons, keep track of which ones I clicked, then when I've performed a function, I "unclick" them. I could have used toggle buttons, but this one line change to add this feature was easier than changing my entire component array. Plus, the UI "feel" is right.)
请解释一下您到底想做什么。
根据您所写的内容,我推测您试图一次仅选择一个按钮。如果是这样,请将 JButton 替换为 JToggleButton,并将它们放入一个 ButtonGroup.
例如(伪代码):
否则,如果您确实想更改按钮的背景颜色:
Please explain what exactly it is you want to do.
From what you've written I gather you're trying to have only one Button selected at a time. If so replace your JButtons with JToggleButtons and put them in one ButtonGroup.
E.g. (pseudo-code):
Else if you really wanted to change the buttons' background colors:
我通过在声明“liveButton”变量后添加以下行来修复此问题:
后来,在 ChangeInUsersList 函数内,我用 buttonColor 替换了“Color.GRAY”。
它起作用了:)
I fixed it by adding the following line after declaring "liveButton" variable:
Later, inside ChangeInUsersList function, I replaced "Color.GRAY" with buttonColor.
And it worked :)
您的按钮上需要
setBackground()
、setContentAreaFilled(false)
、setOpaque(true)
。You need both
setBackground()
,setContentAreaFilled(false)
,setOpaque(true)
on your button.