Java窗口不设置背景颜色?

发布于 2025-01-07 13:56:01 字数 442 浏览 4 评论 0原文

这可能是一个非常愚蠢的错误,但我刚刚开始学习 .awt 包。我按照教程进行操作,在视频中他的窗口背景是红色,我的代码没有错误,但它不会改变背景颜色。 感谢您的帮助!

import java.awt.Color;
import javax.swing.*;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(350,350);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Window");
f.setBackground(Color.RED);
    }

}

This is probably a really stupid error but iv'e just started learning the .awt package. I followed a tutorial to the letter, in the video his window's background is red, there are no errors in my code yet it won't change the background color.
Thanks for any help!

import java.awt.Color;
import javax.swing.*;
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(350,350);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Window");
f.setBackground(Color.RED);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2025-01-14 13:56:01

1) JFrame 无法做到这一点,您必须更改内容窗格的 Color 例如

JFrame.getContentPane().setBackground(myColor)

2) 您需要包装 GUI 相关代码(在 main 中) > 方法)到 invokeLater

例如:

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

public class GUI {

    public GUI() {
        JFrame frame = new JFrame();
        frame.setTitle("Test Background");
        frame.setLocation(200, 100);
        frame.setSize(600, 400);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.getContentPane().setBackground(Color.BLUE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                GUI gUI = new GUI();
            }
        });
    }
}

1) JFrame can't do that, you have to change Color for content pane e.g.

JFrame.getContentPane().setBackground(myColor)

2) You need to wrap GUI related code (in main method) to the invokeLater

For example:

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

public class GUI {

    public GUI() {
        JFrame frame = new JFrame();
        frame.setTitle("Test Background");
        frame.setLocation(200, 100);
        frame.setSize(600, 400);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.getContentPane().setBackground(Color.BLUE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                GUI gUI = new GUI();
            }
        });
    }
}
享受孤独 2025-01-14 13:56:01

而不是

f.setBackground(Color.RED);

调用

f.getContentPane().setBackground(Color.RED);

内容窗格就是显示的内容。

作为旁注,这里有一个 JFrame 提示:您可以调用 f.add(child) ,子项将被添加到内容窗格中。

Instead of

f.setBackground(Color.RED);

call

f.getContentPane().setBackground(Color.RED);

The content pane is what is displayed.

As a side note, here's a JFrame tip: you can call f.add(child) and the child will be added to the content pane for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文