微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

背景颜色不会改变 JFrame

如何解决背景颜色不会改变 JFrame

我需要有关 GUI 的 Java Swing 的帮助。我在代码中包含了 frame.getcontentpane().setBackground(color.cyan);,但框架背景颜色没有改变。

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

public class Loginone {

    private static JLabel lblUsr;
    private static JButton btnNext;
    private static JTextField txtUsr;
    private static JFrame frame;
    private static JPanel panel;

    public static void main(String[] args) {
        frame = new JFrame("Home Page");
        frame.setSize(800,600);
        frame.getContentPane().setBackground(Color.cyan);
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        frame.add(panel);

        panel.setLayout(null);

        lblUsr = new JLabel("Username");            //Username Label
        lblUsr.setBounds(10,20,80,25);        
        panel.add(lblUsr);                    

        txtUsr = new JTextField(20);         //Username input field
        txtUsr.setBounds(80,25);       
        panel.add(txtUsr);                   

        btnNext = new JButton("Next");
        btnNext.setBounds(80,90,25);
        panel.add(btnNext);

        frame.setVisible(true);
    }
}

解决方法

改用 panel.setBackground(Color.CYAN);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。