簡體   English   中英

為 JFrame 設置背景顏色

[英]Setting background color for a JFrame

您如何為 JFrame 設置背景顏色?

檢索框架的內容窗格並使用繼承自ComponentsetBackground()方法更改顏色。

例子:

myJFrame.getContentPane().setBackground( desiredColor );

為 JFrame 設置背景顏色:

getContentPane().setBackground(Color.YELLOW);  //Whatever color

使用:

setBackground(Color.red);

不能正常工作。

采用

Container c = JFrame.getContentPane();

c.setBackground(Color.red);

要么

myJFrame.getContentPane().setBackground( Color.red );

要為 JFrame 設置背景顏色,請嘗試以下操作:

this.getContentPane().setBackground(Color.white);

你好,我確實遇到了同樣的問題,經過多次嘗試,我發現問題是你需要一個圖形對象才能繪制,paint(setBackgroundColor)。

我的代碼通常是這樣的:

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


public class DrawGraphics extends JFrame{

    public DrawGraphics(String title) throws HeadlessException {
      super(title);
      InitialElements();
    }

    private void InitialElements(){
      setSize(300, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      // This one does not work
      // getContentPane().setBackground(new Color(70, 80, 70));

    }

    public void paint(Graphics draw){
      //Here you can perform any drawing like an oval...
      draw.fillOval(40, 40, 60, 50);

      getContentPane().setBackground(new Color(70,80,70));
    }
}

幾乎所有其他答案中缺少的部分是放置代碼的位置。 那么現在你知道它進入油漆(圖形G)

這是最簡單和正確的方法。 您所要做的就是在 initComponents(); 之后添加此代碼;

getContentPane().setBackground(new java.awt.Color(204, 166, 166));

這是一個示例 RGB 顏色,您可以將其替換為您想要的顏色。 如果您不知道 RGB 顏色的代碼,請在互聯網上搜索...有很多網站提供這樣的自定義顏色。

您可以像這樣使用容器:

Container c = JFrame.getContentPane();
c.setBackground(Color.red); 

您當然必須為紅色常量導入java.awt.Color

這是另一種方法:

private void RenkMouseClicked(java.awt.event.MouseEvent evt) {
    renk = JColorChooser.showDialog(null, "Select the background color",
            renk);
    Container a = this.getContentPane();
    a.setBackground(renk);
}

我正在使用 netbeans ide。 對我來說, JFrame.getContentPane()沒有運行。 我用JFrame.getContentPane()的類等效this.getContentPane

我在更改 JFrame 背景時也遇到了麻煩,上面的回復並沒有完全解決。 我正在使用 Eclipse。 添加布局解決了這個問題。

public class SampleProgram extends JFrame {
    public SampleProgram() {
        setSize(400,400);
        setTitle("Sample");
        getContentPane().setLayout(new FlowLayout());//specify a layout manager
        getContentPane().setBackground(Color.red);
        setVisible(true);
}

你可以覆蓋 JFrame 的paint方法,然后用你喜歡的顏色填充它,如下所示:

@Override
public void paint(Graphics g) {
    g.setColor(Color.red);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
}

您可以將此代碼塊用於 JFrame 背景顏色。

    JFrame frame = new JFrame("Frame BG color");
    frame.setLayout(null);
    
    frame.setSize(1000, 650);
    frame.getContentPane().setBackground(new Color(5, 65, 90));
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);

創建一個JLabel,調整其大小,使其覆蓋您的JFrame。 右鍵單擊JLabel,查找圖標,然后單擊(...)按鈕。 通過單擊導入到項目按鈕來選擇圖片,然后單擊完成。 在“導航器”窗格中,(默認情況下,左下角如果被禁用,請轉到Netbeans IDE的“ Windows”選項卡並啟用它。)

使用Jlable,您還可以設置背景顏色和圖像。

public nameOfTheClass()  {

final Container c = this.getContentPane();

  public void actionPerformed(ActionEvent e) {
    c.setBackground(Color.white); 
  }
}

從停滯中復活一個線程。

2018 年,此解決方案適用於 NetBeans 中的 Swing/JFrame(應該適用於任何 IDE :):

this.getContentPane().setBackground(Color.GREEN);

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

public class MySimpleLayout extends JFrame {

        private Container c;
        public MySimpleLayout(String str) {
            super(str);
            c=getContentPane();
            c.setLayout(null);
            c.setBackground(Color.WHITE);
        }
}
    frame.getContentPane().setBackground(Color.white);

可能最簡單的方法是這樣的:

super.setBackground(Color.CYAN);

在執行此操作之前,您必須在類中擴展 JFrame!

暫無
暫無

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

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