在awt中更改按钮backgroundcolor [英] change buttons backgroundcolor in awt

查看:140
本文介绍了在awt中更改按钮backgroundcolor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个名为Safe25的GUI程序。基本上,如果您按正确的顺序按下15032018按钮,程序将自行关闭。
如果输入正确的数字,可以说你在开始时按1,按钮应该将背景颜色改为绿色,如这个



如果按错按钮,按钮应将颜色更改为红色。



但我的代码的逻辑与我的问题无关。
正如我所说,我想改变链接图像中的按钮backgroundcolor。我的问题是它改变了框架的背景颜色,而不是像这个



重要的一行是75,我评论了这一行。

  import java.awt 。事件。*; 
import java.awt。*;
import javax.swing。*;

公共类Safe25 extends Frame实现ActionListener {
JButton []按钮;

Safe25(){// Konstruktor
setSize(250,300);
setLocation(300,300);
setTitle(Safe25);

buttons = new JButton [10];
for(int i = 0; i< 10; i ++){//10KnöpfeimArray
buttons [i] = new JButton(+ i);
buttons [i] .setFont(new Font(Courier,Font.BOLD,34));
buttons [i] .addActionListener(this); //
}

面板panel0 = new Panel();
panel0.setLayout(new GridLayout(1,1));
panel0.add(buttons [0]);
Panel panelRest = new Panel();
panelRest.setLayout(new GridLayout(3,3));
setLayout(new GridLayout(2,1));
for(int i = 1; i< 10; i ++){
panelRest.add(buttons [i]);
}
add(panel0); // Panel mit 0-Knopf
add(panelRest);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent wv){
System.exit(0);
}
});
setVisible(true);
}

int s = 0;

public void actionPerformed(ActionEvent evt){
// 0 1 2 3 4 5 6 78Zustände...
// 1-5-0-3-2 -0-1-8 ist richtige Kombination
switch(Integer.parseInt(evt.getActionCommand())){
case 0:
s =(s == 2 || s == 5 )? s + 1:0;
休息;
案例1:
s =(s == 0 || s == 6)? s + 1:1;
休息;
案例2:
s =(s == 4)? s + 1:0;
休息;
案例3:
s =(s == 3)? s + 1:0;
休息;
案例5:
s =(s == 1)? s + 1:s == 7? 2:0;
休息;
案例8:
s =(s == 7)? s + 1:0;
休息;
默认值:
s = 0;
}
颜色col;
if(s == 0){
col = Color.red;
} else {// richtiger Weg
col = Color.green;
}
if(s == 8){
System.exit(0);
}
for(Component c:getComponents())//第75行,我想要这个
c.setBackground(col); //更改按钮backgroundcolor
repaint(); //但是它改变了框架backgroundcolor而不是
}

public static void main(String [] args){
Safe25 we = new Safe25();
}
}


解决方案

有你为JButton的 javadoc 做了什么? / p>

编辑:



抱歉,我快速查看了您的代码。你现在正在做的是设置当前容器中每个组件的背景颜色。
虽然你的按钮数组是全局的,你可以再次循环通过该集合来获得正确的组件按钮并设置背景颜色如下:

  for(JButton b:buttons)//第75行,我想要这个
b.setBackground(col); //更改按钮backgroundcolor
repaint(); //但它改变了框架backgroundcolor而不是


So i have a GUI program called Safe25. Basically, if you press the buttons in the right order which is "15032018" the program closes itself. If you input a correct number, lets say you press 1 at the start, the buttons should change their backgroundcolor to green like this:

If you press a wrong button, the buttons should change their color to red.

But the logic of my code is irrelevant for my problem. As i said, i want to change the buttons backgroundcolor like in the linked image. My problem is that it changes the backgroundcolor of the frame instead like this

The important line is 75, i commented this one.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Safe25 extends Frame implements ActionListener {
    JButton[] buttons;

    Safe25() { // Konstruktor
        setSize(250, 300); 
        setLocation(300, 300);
        setTitle("Safe25"); 

        buttons = new JButton[10]; 
        for (int i = 0; i < 10; i++) { // 10 Knöpfe im Array
            buttons[i] = new JButton("" + i); 
            buttons[i].setFont(new Font("Courier", Font.BOLD, 34));
            buttons[i].addActionListener(this); // 
        }

        Panel panel0 = new Panel(); 
        panel0.setLayout(new GridLayout(1, 1)); 
        panel0.add(buttons[0]); 
        Panel panelRest = new Panel(); 
        panelRest.setLayout(new GridLayout(3, 3)); 
        setLayout(new GridLayout(2, 1)); 
        for (int i = 1; i < 10; i++) { 
            panelRest.add(buttons[i]); 
        }
        add(panel0); // Panel mit 0-Knopf
        add(panelRest); 
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent wv) {
                System.exit(0);
            }
        });
        setVisible(true);
    }

    int s = 0; 

    public void actionPerformed(ActionEvent evt) {
        // 0 1 2 3 4 5 6 7 8 Zustände ...
        // 1-5-0-3-2-0-1-8 ist richtige Kombination
        switch (Integer.parseInt(evt.getActionCommand())) {
        case 0:
            s = (s == 2 || s == 5) ? s + 1 : 0;
            break;
        case 1:
            s = (s == 0 || s == 6) ? s + 1 : 1;
            break;
        case 2:
            s = (s == 4) ? s + 1 : 0;
            break;
        case 3:
            s = (s == 3) ? s + 1 : 0;
            break;
        case 5:
            s = (s == 1) ? s + 1 : s == 7 ? 2 : 0;
            break;
        case 8:
            s = (s == 7) ? s + 1 : 0;
            break;
        default:
            s = 0;
        }
        Color col;
        if (s == 0) { 
            col = Color.red;
        } else { // richtiger Weg
            col = Color.green;
        }
        if (s == 8) { 
            System.exit(0);
        }
        for (Component c : getComponents()) // line 75, i want this one
            c.setBackground(col); // to change the buttons backgroundcolor
        repaint(); // but it changes the frames backgroundcolor instead
    }

    public static void main(String[] args) {
        Safe25 we = new Safe25();
    }
}

解决方案

have you red the javadoc for JButton?

edit:

Sorry i looked over your code to quickly. What your doing right now is setting the background color of every component in the current container. While your buttons array is global you could simply loop trough that collection again to get the correct components "the buttons" and setting the background color like so:

        for (JButton b : buttons) // line 75, i want this one
           b.setBackground(col); // to change the buttons backgroundcolor
        repaint(); // but it changes the frames backgroundcolor instead

这篇关于在awt中更改按钮backgroundcolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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