简体   繁体   中英

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. (PS I know the code is kind of sloppy).

Thanks guys.

public class PasswordJFrame extends JFrame implements 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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