【问题标题】:Setting background color for a JFrame为 JFrame 设置背景颜色
【发布时间】:2010-11-08 01:33:06
【问题描述】:

您如何设置 JFrame 的背景颜色?

【问题讨论】:

    标签: java swing graphics awt


    【解决方案1】:

    检索框架的内容窗格并使用从Component 继承的setBackground() 方法更改颜色。

    例子:

    myJFrame.getContentPane().setBackground( desiredColor );
    

    【讨论】:

      【解决方案2】:

      为 JFrame 设置背景颜色:

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

      【讨论】:

        【解决方案3】:

        使用:

        setBackground(Color.red);
        

        不能正常工作。

        使用

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

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

        【讨论】:

        • 如果已经有其他用户相同的答案,为什么还要回答?
        • 他的第一句话是有道理的,即 setBackGround 不能正常工作。
        【解决方案4】:

        要设置 JFrame 的背景颜色,试试这个:

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

        【讨论】:

        • 我觉得这是一个解决方案的建议,可能是也可能不是答案。如果 OP 尝试此操作并发现它正在工作,他们可以要求您添加作为答案以将其标记为如此。这就是我通常的工作方式。
        • 感谢这个伟大的解决方案。颜色中的“C”似乎是区分大小写的。
        【解决方案5】:

        您好,我确实遇到了同样的问题,经过多次尝试,我发现问题是您需要一个 Graphics Object 才能进行绘制,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));
            }
        }
        

        几乎所有其他答案中缺少的部分是放置代码的位置。 那么现在你知道它在 paint(Graphics G)

        【讨论】:

          【解决方案6】:

          这是最简单也是正确的方法。您所要做的就是在 initComponents(); 之后添加此代码;

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

          这是一个示例 RGB 颜色,您可以将其替换为您想要的颜色。如果您不知道RGB颜色的代码,请在网上搜索......有很多网站提供这样的自定义颜色。

          【讨论】:

            【解决方案7】:

            你可以像这样使用容器:

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

            您当然必须为 red 颜色常量导入 java.awt.Color

            【讨论】:

              【解决方案8】:

              这是另一种方法:

              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

              【讨论】:

                【解决方案9】:

                我在更改 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);
                }
                

                【讨论】:

                  【解决方案10】:

                  你可以重写 JFrame 的paint方法,然后用你喜欢的颜色填充它,如下所示:

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

                  【讨论】:

                    【解决方案11】:

                    您可以将此代码块用于 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);
                    

                    【讨论】:

                      【解决方案12】:
                      public nameOfTheClass()  {
                      
                      final Container c = this.getContentPane();
                      
                        public void actionPerformed(ActionEvent e) {
                          c.setBackground(Color.white); 
                        }
                      }
                      

                      【讨论】:

                      • 一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决问题的原因,答案会更有帮助。
                      • 我用解决问题的人所遇到的问题的代码具体回答了。永远不要像你说的那样添加另一个问题。
                      • @Learning。最好写:frame.getContentPane().setBackground(Color.PINK);
                      【解决方案13】:

                      从静止状态中恢复线程。

                      2018 年,此解决方案适用于 NetBeans 中的 Swing/JFrame(应该适用于任何 IDE :):

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

                      【讨论】:

                        【解决方案14】:
                        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);
                                }
                        }
                        

                        【讨论】:

                        • 与较旧的答案相比,没有什么新的,是否存在 ;-) 加上一些禁忌:a) 如果你可以在没有 b) 的情况下达到要求,请不要扩展 b) 不要保留别名成员c) 不要没有 LayoutManager
                        【解决方案15】:

                        可能最简单的方法是这样的:

                        super.setBackground(Color.CYAN);
                        

                        在执行此操作之前,您必须在类中扩展 JFrame!

                        【讨论】:

                        • 这要么是错误的,要么是重复的,这取决于您要在哪里调用该方法,直接在框架上或在其内容窗格上
                        • 您给出的答案中不仅没有指定,而且重复了上面的答案。
                        猜你喜欢
                        • 2020-12-06
                        • 1970-01-01
                        • 1970-01-01
                        • 2011-02-11
                        • 1970-01-01
                        • 2013-01-07
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多