Java null布局导致空白屏幕 [英] Java null layout results in a blank screen

查看:211
本文介绍了Java null布局导致空白屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用setLayout(null)时,我得到一个普通的灰色屏幕,并且没有任何组件存在。我是否需要为ColorPanel中的每个组件提供x,y值?

When I try to use setLayout(null), I get a plain gray screen and none of my components are present. Do I need to give every component in ColorPanel an x, y value?

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

public class GUI{

    public static void main(String[] args){
        JFrame GUI = new JFrame();
        GUI.setLayout(null);
        GUI.setTitle("Betrai");
        GUI.setSize(500, 500);
        GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ColorPanel panel = new ColorPanel(Color.white);
        Container pane = GUI.getContentPane();
        JButton continueButton = new JButton("TEST");
        continueButton.setBackground(Color.red);
        continueButton.setBounds(10, 10, 60, 40);
        pane.add(continueButton);
        GUI.setVisible(true);
        GUI.setBounds(0, 0, 500, 500);
        GUI.setResizable(false);
    }
}


推荐答案

我尝试了代码,我可以看到你的按钮,就像你指定的一样!

I tried the code and I can see your button, just like you specified!

你需要添加 ColorPanel 到布局和设置边界(就像你为测试按钮所做的那样)。

You need to add the ColorPanel to the layout and set the bounds (just like you did for the test button).

    panel.setBounds(50, 50, 100, 100);
    pane.add(panel);

但你不应该使用 null 布局。几乎总有另一种布局可以满足您的需求!

But you should not use null layout. There is almost always another layout that can fullfill your needs!

这篇关于Java null布局导致空白屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆