Why are my items not showing up in JFrame?(为什么我的项目没有出现在 JFrame 中?)
问题描述
我对 JFrame 还很陌生,我想知道为什么我的项目没有显示在窗口上.我知道我没有 ActionHandler 但我只想让我的文本字段显示在我的窗口上.这是我的代码:
I'm fairly new to JFrame and I want to know why my items are not showing up on the window. I know i dont have a ActionHandler but I just want my textfield's to show up on my window. Here's my code:
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class FirstGUI extends JFrame{
public void GUI(){
setTitle("Welcome");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(600,600);
JLabel title = new JLabel();
title.setText("Apple Inc. Member Login Port");
title.setFont(new Font("Arial", Font.PLAIN, 24));
JTextField login = new JTextField("Login",10);
JPasswordField pass = new JPasswordField("Password");
add(title);
add(login);
add(pass);
}
public static void main(String[] args){
FirstGUI a = new FirstGUI();
a.GUI();
}
}
但是当我运行它时,我得到了这个:
but when i run it i get this:
推荐答案
但是当我运行它时,我得到了这个:
but when i run it i get this:
您得到一个空白屏幕,因为您在框架可见后将组件添加到框架.
You get an empty screen because you add the components to the frame after the frame is visible.
- 如前所述,您需要使用适当的布局管理器.FlowLayout 是最容易上手的.
- 在将组件添加到框架之后调用
setVisible(true)
.
所以代码应该更像:
panel.add(...);
panel.add(...);
add(panel);
pack();
setVisible(true);
这篇关于为什么我的项目没有出现在 JFrame 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我的项目没有出现在 JFrame 中?


- 转换 ldap 日期 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01