簡體   English   中英

如何更改Java面板上的背景顏色?

[英]How to change the background color on a Java panel?

現在,我得到的背景是灰色的。 我想把它變成黑色。 我嘗試過像setBackground(color.BLACK)這樣的東西; 但它沒有用。 有什么建議?

public test() 
{
    setTitle("Adjustment Form");
    setSize(670,450);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    setLayout(new GridLayout(4,6,2,2));
    setVisible(true);   
}

你可以打電話:


getContentPane().setBackground(Color.black);

或者將JPanel添加到您使用的JFrame中。 然后將您的組件添加到JPanel。 這將允許你打電話


setBackground(Color.black);

在JPanel上設置背景顏色。

我想他想說的是使用getContentPane().setBackground(Color.the_Color_you_want_here)

但是如果你想將顏色設置為JFrame中的任何其他顏色,則使用object.setBackground(Color.the_Color_you_want_here)

例如:

jPanel.setbackground(Color.BLUE)

setBackground()是正確的使用方法。 你改變它后重新塗漆了嗎? 如果在使面板(或其包含框架)可見之前更改它,它應該可以工作

我假設我們正在處理JFrame? 內容窗格中的可見部分 - 您必須使用jframe.getContentPane()。setBackground(...);

暫無
暫無

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

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