Как изменить фон окна java

Just how do you set the background color for a JFrame?

Just how do you set the background color for a JFrame?

Laser Infinite's user avatar

asked Jul 4, 2009 at 4:26

Retrieve the content pane for the frame and use the setBackground() method inherited from Component to change the color.

Example:

myJFrame.getContentPane().setBackground( desiredColor );

answered Jul 4, 2009 at 4:30

Brandon E Taylor's user avatar

Brandon E TaylorBrandon E Taylor

24.6k6 gold badges46 silver badges70 bronze badges

To set the background color for JFrame:

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

answered Jul 4, 2009 at 4:33

iwanttoprogram's user avatar

using:

setBackground(Color.red);

doesn’t work properly.

use

Container c = JFrame.getContentPane();

c.setBackground(Color.red);

or

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

Reins's user avatar

Reins

1,1091 gold badge17 silver badges35 bronze badges

answered Sep 12, 2015 at 8:07

darx's user avatar

darxdarx

1491 silver badge2 bronze badges

2

This is the simplest and the correct method. All you have to do is to add this code after initComponents();

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

That is an example RGB color, you can replace that with your desired color. If you dont know the codes of RGB colors, please search on internet… there are a lot of sites that provide custom colors like this.

answered Jul 21, 2017 at 22:49

Cristian Babarusi's user avatar

To set the background color for the JFrame try this:

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

luk2302's user avatar

luk2302

53.3k23 gold badges96 silver badges136 bronze badges

answered Jan 11, 2017 at 10:24

Mr. J's user avatar

Mr. JMr. J

711 silver badge1 bronze badge

2

Hello There I did have the same problem and after many attempts I found that the problem is that you need a Graphics Object to be able to draw, paint(setBackgroundColor).

My code usually goes like this:

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));
    }
}

The missing part on almost all other answers is where to place the code.
Then now you know it goes in paint(Graphics G)

answered Aug 15, 2015 at 2:20

T04435's user avatar

T04435T04435

11.5k4 gold badges52 silver badges53 bronze badges

You can use a container like so:

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

You must of course import java.awt.Color for the red color constant.

Ankur's user avatar

Ankur

2,17123 silver badges29 bronze badges

answered Jul 4, 2009 at 4:32

John T's user avatar

John TJohn T

23.5k11 gold badges56 silver badges82 bronze badges

Here’s another method:

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

I’m using netbeans ide. For me, JFrame.getContentPane() didn’t run. I used JFrame.getContentPane()‘s class equivalent this.getContentPane.

demongolem's user avatar

demongolem

9,33036 gold badges89 silver badges104 bronze badges

answered Nov 25, 2009 at 2:11

Abdullah's user avatar

0

you can override the paint method of JFrame and then fill that by your favorite color like this:

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

answered Mar 8, 2015 at 5:48

Mahdi Hasanpour's user avatar

I had trouble with changing the JFrame background as well and the above responses did not solve it entirely. I am using Eclipse. Adding a layout fixed the issue.

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);
}

answered Jul 28, 2020 at 19:42

Debra's user avatar

DebraDebra

191 bronze badge

You can use this code block for JFrame background color.

    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);

answered Jan 9, 2021 at 5:36

mihmandar's user avatar

public nameOfTheClass()  {

final Container c = this.getContentPane();

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

answered Dec 11, 2017 at 18:35

Learning's user avatar

3

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);
        }
}

Stephan's user avatar

Stephan

41.1k62 gold badges237 silver badges324 bronze badges

answered Nov 29, 2012 at 13:13

Pratik K's user avatar

1

Probably the SIMPLEST method is this:

super.setBackground(Color.CYAN);

You must extend JFrame in the class before doing this!

Hunter Zolomon's user avatar

answered Mar 30, 2013 at 4:11

Red_Hat's user avatar

Red_HatRed_Hat

751 silver badge7 bronze badges

2

I am trying to make a planner and want to change the background of my JFrame.
I have already tried frame.getContentPane().setBackground(Color.); but that doesn’t seem to work.

Here is the code for the frame portion

`public Planner(){
    frame = new JFrame();
    main = new JPanel();
    menu = new Menu(this);
    frame.setPreferredSize(preferredSize);
    frame.add(main);
    frame.setJMenuBar(menu);
    frame.pack();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Myva");

    JLabel loading = new JLabel();
    JOptionPane pane = new JOptionPane();
    pane.showMessageDialog( null, "Hi. ");
    name = pane.showInputDialog("What is your name:");

}

Thanks in advance

asked Aug 23, 2015 at 2:16

fariswheel's user avatar

0

If I understand your question, then you could call JFrame.setBackground(Color) like

frame.setBackground(Color.BLUE);

If you want to change the color in a more visible manner, you can make it on a JPanel. Like,

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.setBackground(Color.BLUE);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(640, 480);
    frame.setVisible(true);
}

which will give you a very BLUE window.

answered Aug 23, 2015 at 2:19

Elliott Frisch's user avatar

Elliott FrischElliott Frisch

195k20 gold badges157 silver badges246 bronze badges

2

To change the color of your JFrame, use the following code:

frame.setBackground(Color.BLUE);

(Color doesn’t have to be blue, I just used it as an example)

answered Aug 23, 2015 at 2:19

Wyatt Lowery's user avatar

Wyatt LoweryWyatt Lowery

5131 gold badge7 silver badges21 bronze badges

I am trying to make a planner and want to change the background of my JFrame.
I have already tried frame.getContentPane().setBackground(Color.); but that doesn’t seem to work.

Here is the code for the frame portion

`public Planner(){
    frame = new JFrame();
    main = new JPanel();
    menu = new Menu(this);
    frame.setPreferredSize(preferredSize);
    frame.add(main);
    frame.setJMenuBar(menu);
    frame.pack();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Myva");

    JLabel loading = new JLabel();
    JOptionPane pane = new JOptionPane();
    pane.showMessageDialog( null, "Hi. ");
    name = pane.showInputDialog("What is your name:");

}

Thanks in advance

asked Aug 23, 2015 at 2:16

fariswheel's user avatar

0

If I understand your question, then you could call JFrame.setBackground(Color) like

frame.setBackground(Color.BLUE);

If you want to change the color in a more visible manner, you can make it on a JPanel. Like,

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.setBackground(Color.BLUE);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(640, 480);
    frame.setVisible(true);
}

which will give you a very BLUE window.

answered Aug 23, 2015 at 2:19

Elliott Frisch's user avatar

Elliott FrischElliott Frisch

195k20 gold badges157 silver badges246 bronze badges

2

To change the color of your JFrame, use the following code:

frame.setBackground(Color.BLUE);

(Color doesn’t have to be blue, I just used it as an example)

answered Aug 23, 2015 at 2:19

Wyatt Lowery's user avatar

Wyatt LoweryWyatt Lowery

5131 gold badge7 silver badges21 bronze badges

VaneKKrasav4ik

0 / 0 / 0

Регистрация: 15.05.2014

Сообщений: 14

1

Цвет фона окна

18.05.2015, 20:55. Показов 17968. Ответов 1

Метки нет (Все метки)


Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package окно.входа.в.систему;
import java.awt.Color;
import static javafx.scene.paint.Color.color;
import javax.swing.Box;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
 
/**
 *
 * @author Parfenov 
 */
public class MyWindowApp extends JFrame { //Наследуя от JFrame мы получаем всю функциональность окна
 
  public MyWindowApp(){
    super("Вход в систему "); //Заголовок окна
    
    setBounds(100, 100, 400, 200); //РАЗМЕР нашего окошечка
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box box1 = Box.createHorizontalBox();
JLabel loginLabel = new JLabel("Логин:");
      JTextField loginField = new JTextField(15);
box1.add(loginLabel);
box1.add(Box.createHorizontalStrut(7));
box1.add(loginField);
 
Box box2 = Box.createHorizontalBox();
JLabel passwordLabel = new JLabel("Пароль:");
      JPasswordField passwordField = new JPasswordField(3);
box2.add(passwordLabel);
box2.add(Box.createHorizontalStrut(6));
box2.add(passwordField);
 
 
loginLabel.setPreferredSize(passwordLabel.getPreferredSize());
 
Box mainBox = Box.createVerticalBox();
mainBox.setBorder(new EmptyBorder(12,12,12,12));
mainBox.add(box1);
mainBox.add(Box.createVerticalStrut(12));
mainBox.add(box2);
mainBox.add(Box.createVerticalStrut(17));
 
setContentPane(mainBox);
pack();
setResizable(false);
 
  } // Тут эелементы
  
 
  public static void main(String[] args) { 
    MyWindowApp app = new MyWindowApp(); //Создаем экземпляр нашего приложения
    app.setVisible(true); //С этого момента приложение запущено!
  }
}

Как задать цвет окну ?

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



Pablito

2882 / 2294 / 769

Регистрация: 12.05.2014

Сообщений: 7,978

18.05.2015, 21:48

2

Java
1
2
setResizable(false);
setBackground(Color.CYAN);



0



Понравилась статья? Поделить с друзьями:
  • Как изменить фон навигатора
  • Как изменить фон набора номера на андроид
  • Как изменить фон на яндекс почте на телефоне
  • Как изменить фон на яндекс вкладке
  • Как изменить фон на эппл вотч