简体   繁体   English

JFrame背景颜色不会改变吗?

[英]JFrame background color won't change?

So, I'm trying to display a simple JFrame but I'm unable to change the background color? 所以,我试图显示一个简单的JFrame,但无法更改背景颜色? Did a few searches and they all suggest to use useContentPane which I have. 做了几次搜索,他们都建议使用我拥有的useContentPane。

 import java.awt.*;

import javax.swing.*; 


public class Login {

     public static void main(String[] args) {

         createWindow();

         } 

    private static void createWindow() {
        JFrame frame = new JFrame("Login System");
        frame.getContentPane().setBackground(Color.darkGray);
        frame.setSize(350, 350);    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextField username = new JTextField();
        frame.add(username);
        frame.setVisible(true);
    }


}

The JFrame size works but the background does not and the TextField does not, pretty new to this. JFrame的大小有效,但背景不起作用,而TextField不起作用,这是很新的。 Any ideas? 有任何想法吗? All help is appreciated. 感谢所有帮助。

The code works fine. 该代码工作正常。 The problem is that your text field takes up the entire frame. 问题是您的文本字段占用了整个框架。 Try: 尝试:

//frame.add(username);
frame.add(username, BorderLayout.NORTH);

The problem with your code is that your text field is covering the full jframe 您的代码的问题是您的文本字段覆盖了整个jframe

one of the way is to reduce the text field size in the design view and then in code add this line 一种方法是减小设计视图中的文本字段大小,然后在代码中添加此行

getContentPane().setBackground(Color.BLACK); 。的getContentPane()的setBackground(Color.BLACK);

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

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