我的背景颜色未应用于我的Java应用程序。我已经尝试查看了一些答案,他们都建议使用getContentPane(),这就是我一直在使用的。
import java.util.Random;
import javax.swing.*;
import java.awt.*;
public class higherLower extends JFrame{
private static final long serialVersionUID = 1L;
public static int WIDTH = 300;
public static int HEIGHT = WIDTH / 2;
public static final String TITLE = "Higher/Lower";
JTextField input = new JTextField();
public higherLower(){
this.setSize(WIDTH, HEIGHT);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.CYAN);
this.setLocationRelativeTo(null);
//this.add(input);
this.setVisible(true);
}
public void paint(Graphics g){
}
public static void main(String[] args){
new higherLower();
}
}
所以我不完全确定为什么这不起作用。
答案 0 :(得分:1)
删除短路画面功能的paint
方法
答案 1 :(得分:1)
为什么要覆盖paint(Graphics)什么都不做?
只需删除您的覆盖,看看会发生什么。