JFrame 不显示任何组件 [英] JFrame not presenting any Components

查看:65
本文介绍了JFrame 不显示任何组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码创建一个非常简单的JFrame,但由于某种原因它没有显示任何组件,只是一个空白框架.为什么会这样?我创建了很多次框架,但我就是不知道出了什么问题.代码是:

I am using the following code to create a very simple JFrame, but for some reason it doesn't show any components, just a blank frame. Why is this happening? I created frames a bunch of times and I just can't figure out what is wrong. The code is:

Main(){
    JFrame frame = new JFrame("Colorizer | By: NonameSL");
    frame.setSize(400,200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    frame.setContentPane(panel);
    textField=new JTextField("Enter your name!");
    textField.setBounds(0,0,40,200);
    textField.setVisible(true);
    frame.getContentPane().add(textField);
    button=new JButton("Go!");
    button.setBounds(0, 200, 40, 200);
    button.setVisible(true);
    frame.getContentPane().add(button);
    rectangle=new RecShape(Color.WHITE);
    rectangle.setBounds(0,40,400,160);
    rectangle.setVisible(false);
    frame.getContentPane().add(rectangle);
    Main.frame=frame;
    registerButton();
}

RecShape 类是我创建的一个类,用于在屏幕上简单地创建一个矩形形状.怎么了?

The RecShape class is a class I created to simply create a rectangle shape on screen. What is wrong?

推荐答案

Put frame.setVisible(true); 添加组件后 JFrame,它将显示所有添加的组件.此外,您应该使用特定的布局而不是为组件设置边界.您可以使用 布局管理器.

Put frame.setVisible(true); after adding components to JFrame, and it will show all the added components. Moreover, you should use specific layout rather than setting bounds for components. You can use a Layout Manager.

这篇关于JFrame 不显示任何组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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