本文介绍了为什么我的项目没有显示在 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:
推荐答案
您会得到一个空白屏幕,因为您在框架可见后将组件添加到框架中.
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 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!