JFrame not presenting any Components(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 不显示任何组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JFrame 不显示任何组件
基础教程推荐
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 double 转换为 Int,向下舍入 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
