设置 Jbutton 的背景

发布于 2024-08-28 15:14:08 字数 1808 浏览 8 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韵柒 2024-09-04 15:14:08

如果您确实需要为按钮着色,然后将颜色设置回其原始默认状态(系统灰色),请使用

button.setBackground(null);

这将删除任何以前的颜色设置。

(我有一个应用程序,我需要单击几个按钮,跟踪我单击的按钮,然后当我执行一项功能时,我“取消单击”它们。我可以使用切换按钮,但是这一行更改为添加此功能比更改整个组件数组更容易。另外,UI“感觉”是正确的。)

If you ever do need to color a button and then set the color back to its original default state (system gray), use

button.setBackground(null);

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

妖妓 2024-09-04 15:14:08

请解释一下您到底想做什么。

根据您所写的内容,我推测您试图一次仅选择一个按钮。如果是这样,请将 JButton 替换为 JToggleButton,并将它们放入一个 ButtonGroup.
例如(伪代码):

//[...]
JToggleButton button2 = new JToggleButton(...)
//[...]
ButtonGroup group = new ButtonGroup();
//[...]
group.add(button2);
//[...]

否则,如果您确实想更改按钮的背景颜色:

private List<JButton> buttons;
private JButton b1, b2, b3, b4, b5;
private void initButtons()
{
   buttons = new ArrayList<JButton>(5); // new List to "save" Buttons in
   buttons.add(b1 = new JButton());
   // etc etc ...
   buttons.add(b5 = new JButton());
}

public void setActiveButton(JButton button)
{
   for(JButton b : buttons)
   {
      b.setBackgroundColor(Color.GREY);
   } 
   button.setBackgroundColor(Color.WHITE);
}

private void b1ActionPerformed(java.awt.event.ActionEvent evt) 
{
   setActiveButton(b1);
   // or to be more "generic"
   // setActiveButton((JButton) evt.getSource());
}

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

//[...]
JToggleButton button2 = new JToggleButton(...)
//[...]
ButtonGroup group = new ButtonGroup();
//[...]
group.add(button2);
//[...]

Else if you really wanted to change the buttons' background colors:

private List<JButton> buttons;
private JButton b1, b2, b3, b4, b5;
private void initButtons()
{
   buttons = new ArrayList<JButton>(5); // new List to "save" Buttons in
   buttons.add(b1 = new JButton());
   // etc etc ...
   buttons.add(b5 = new JButton());
}

public void setActiveButton(JButton button)
{
   for(JButton b : buttons)
   {
      b.setBackgroundColor(Color.GREY);
   } 
   button.setBackgroundColor(Color.WHITE);
}

private void b1ActionPerformed(java.awt.event.ActionEvent evt) 
{
   setActiveButton(b1);
   // or to be more "generic"
   // setActiveButton((JButton) evt.getSource());
}
山人契 2024-09-04 15:14:08

我通过在声明“liveButton”变量后添加以下行来修复此问题:

Color buttonColor = b1.getBackground();

后来,在 ChangeInUsersList 函数内,我用 buttonColor 替换了“Color.GRAY”。
它起作用了:)

I fixed it by adding the following line after declaring "liveButton" variable:

Color buttonColor = b1.getBackground();

Later, inside ChangeInUsersList function, I replaced "Color.GRAY" with buttonColor.
And it worked :)

墨小墨 2024-09-04 15:14:08

您的按钮上需要 setBackground()setContentAreaFilled(false)setOpaque(true)

You need both setBackground(), setContentAreaFilled(false), setOpaque(true) on your button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文