簡體   English   中英

Jframe 不設置背景色

[英]Jframe Not Setting Background Color

我嘗試了很多不同的方法來讓我的項目顯示背景顏色。 它仍然不會改變顏色。 我知道這只是需要修復的小問題,但我找不到它。 任何幫助表示贊賞。 (我確實在頂部有導入,不確定 Stack 是否顯示)-Liam

import java.awt.Color; //importing things that the code needs 
import java.awt.Graphics; //importing things that the code needs 
import javax.swing.JFrame; //importing things that the code needs 





public class bigJavaClass extends JFrame{  //Jframe is the viewer so we can see what we are drawing 
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final int R = 200;
    public static final int G = 200;
    public static final int B = 200;

public static void main(String[] args) {
    bigJavaClass test = new bigJavaClass();
    test.setSize(WIDTH, HEIGHT);
    test.setResizable(false);
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.setVisible(true);
    test.setBackground(new Color (248, 177, 149)); //background color R, G, B
}

    public void paint(Graphics g){
        boolean a = false; //for the variable a 
        boolean b = false; //for the variable b
        boolean c = false; //for the variable c 
        boolean d = false; //for the variable d 
  1. class 名稱不應以小寫字符開頭。

  2. 不要覆蓋 JFrame 上的 paint()。您已經失去了默認的繪制行為,即繪制添加到框架的背景和組件。

  3. “內容窗格”繪制在框架的頂部。 因此,您需要更改內容窗格的背景。 有關框架結構的更多信息,請參閱: 使用頂級容器 從教程中下載工作示例並進行自定義以了解如何更好地構建代碼。

在使框架可見之前設置背景。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM