为什么我的项目没有出现在 JFrame 中? [英] Why are my items not showing up in JFrame?

查看:41
本文介绍了为什么我的项目没有出现在 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.

  1. 如前所述,您需要使用适当的布局管理器.FlowLayout 是最容易上手的.
  2. 在将组件添加到框架之后调用 setVisible(true).

所以代码应该更像:

panel.add(...);
panel.add(...);
add(panel);
pack();
setVisible(true);

这篇关于为什么我的项目没有出现在 JFrame 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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