Making the background of a JFrame window white by commenting out the data in the paint method

huangapple go评论84阅读模式
英文:

Making the background of a JFrame window white by commenting out the data in the paint method

问题

我正在练习创建一个JFrame窗口,并遇到了一个问题。我观看的教程视频说要注释掉paint方法中给定的数据,以使窗口的背景变为白色而不是灰色。当我这样做时,窗口变成了透明的,并且捕捉了您计算机上显示的任何内容,并将其设置为窗口的背景。该视频是在2012年发布的,因此我认为Eclipse已经有一些进展引起了这个问题。那么,我如何将JFrame窗口的背景颜色更改为白色呢?

英文:

I was practicing making a JFrame window and stumbled upon an issue. The tutorial video i was watching said to comment out the data given in the paint method in order to make the background of the window, white insdead of grey. When I did that, the window turned transparent and it captured whatever was displayed on your computer and set it as the window's background. The vided was posted in 2012 so, i believe that there have been some advancements in Eclipse that caused the problem. So, how can i change the background colour of a JFrame window to white?

答案1

得分: 2

以下是您要翻译的内容:

So, how can i change the background colour of a JFrame window to white?

首先,通过更改contentPanebackgroundColor来开始,例如

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.getContentPane().setBackground(Color.WHITE);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

注意:我将其更改为白色以说明观点...

但这种方法有限,因为除非您更改布局管理器,否则一旦向其添加组件,它们将被覆盖。

一个“更好”的解决方案可能是创建一个组件,更改其backgroundColor并将其添加到窗口

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBackground(Color.WHITE);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }
}

或者甚至将其作为contentPane,根据您的需要。

i believe that there have been some advancements in Eclipse that caused the problem

Eclipse与此无关。问题可能是由于对JRE或操作系统进行的更改引起的。

said to comment out the data given in the paint method

好的,绘制是一个复杂的过程,有许多重要的步骤,除非您清楚了解这些步骤并愿意接管并完成它们的工作,否则不要随意更改它 Making the background of a JFrame window white by commenting out the data in the paint method

英文:

> So, how can i change the background colour of a JFrame window to white?

Start by changing the backgroundColor of the contentPane, for example

Making the background of a JFrame window white by commenting out the data in the paint method

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {
    public static void main(String[] args) {
        new Test();
    }
    
    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.getContentPane().setBackground(Color.RED);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

*nb I made it red to make the point...

This is limiting though, because, unless you change the layout manager, the moment you add a component to it, it will be covered.

A "better" solution might be to make a component and change it's backgroundColor and add it to the frame

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBackground(Color.RED);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }
}

or even make it the contentPane, what ever you need.

> i believe that there have been some advancements in Eclipse that caused the problem

Eclipse has nothing to do with it. Will either be changes made to the JRE or OS

> said to comment out the data given in the paint method

Okay, painting is complicated process, with a number of important steps, unless you have a clear understanding of what those steps are AND are willing to take over from them and do their jobs, don't mess with it Making the background of a JFrame window white by commenting out the data in the paint method

huangapple
  • 本文由 发表于 2020年7月31日 17:55:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63189611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定