简体   繁体   English

如何更改JFrame的背景颜色(我是大一学生)

[英]How to change the background color of my JFrame (I'm a first year university student)

Alright so my question here is how am I supposed to change the JFrame background color when my if statements check my booleans to give me either "Strong", "Fair" etc. frame.setBackground(Color.green) would be what I needed but I dont think it wants me to access that object. 好的,所以我的问题是,当我的if语句检查我的布尔值以给我“强”,“一般”等信息时,我应该如何更改JFrame背景颜色。frame.setBackground(Color.green)将是我所需要的,但是我认为它不希望我访问该对象。 (PS I know the code is kind of sloppy). (PS我知道代码有点草率)。

Thanks guys. 多谢你们。

public class PasswordJFrame extends JFrame implements ActionListener { 公共类PasswordJFrame扩展JFrame实现ActionListener {

private JLabel title;
private JTextField input;
private JButton rate;
private JLabel rating;
private JButton reset;
private String password;
private boolean upperlower = false;
private boolean symbol = false;
private boolean number = false;


public PasswordJFrame(){

    Container pane = this.getContentPane();
    pane.setLayout(new FlowLayout());
    title = new JLabel("Enter Your Password:");
    input = new JTextField(15);
    rate = new JButton("Rate my password");
    rating = new JLabel("");
    reset = new JButton("Reset");

    pane.add(title);
    pane.add(input);
    pane.add(rate);
    pane.add(rating);
    pane.add(reset);
    rate.addActionListener(this);
    reset.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();
    password = input.getText();

    if (source == rate){

        for(int i = 0; i<password.length() - 1; i++){

            switch(input.getText().charAt(i)){

                case '@':
                symbol = true;
                break;
                case '$':
                symbol = true;
                break;
                case '*':
                symbol = true;
                break;
                case '+':
                symbol = true;
                break;
                case '%':
                symbol = true;
                break;
                case '&':
                symbol = true;
                break;
                case '0':
                number = true;
                break;
                case '1':
                number = true;
                break;
                case '2':
                number = true;
                break;
                case '3':
                number = true;
                break;
                case '4':
                number = true;
                break;
                case '5':
                number = true;
                break;
                case '6':
                number = true;
                break;
                case '7':
                number = true;
                break;
                case '8':
                number = true;
                break;
                case '9':
                number = true;
                break;  
            }

            if(!password.equals(password.toLowerCase()) && !password.equals(password.toUpperCase())){       
                upperlower = true;
            }
        }

        if(upperlower && symbol && number){
            rating.setText("Strong");
            frame.setBackground(Color.green);
        }
        else if (upperlower && number){
            rating.setText("Fair");
            frame.setBackground(Color.yellow);
        }
        else if (upperlower){
            rating.setText("Weak");
            frame.setBackground(Color.red);
        }
        else{
            rating.setText("Awful");
            frame.setBackground(Color.black);
        }

    }
}

public static void main(String[] args){

    PasswordJFrame frame = new PasswordJFrame();
    frame.setSize(300,300);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

} }

The frame contains a content pane where you add all your components. 该框架包含一个内容窗格,您可以在其中添加所有组件。 So you need to set the background of the content pane: 因此,您需要设置内容窗格的背景:

frame.getContentPane().setBackground(...);

See the section from the Swing tutorial on Using Top Level Containers for more information and diagrams showing the content pane relationship to the frame. 有关更多信息和显示内容窗格与框架的关系的图,请参见Swing教程中有关使用顶层容器的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM