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

查看:306
本文介绍了为什么我的项目没有出现在JFrame的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的JFrame的,我想知道为什么我的项目没有显示在窗口中了。我知道我没有一个的ActionHandler,但我只是希望我的文本框的出现在我的窗口上。这里是我的code:

 进口java.awt.Font中;
进口javax.swing.JFrame中;
进口javax.swing.JLabel中;
进口javax.swing.JPasswordField中;
进口javax.swing.JTextField中;公共类FirstGUI扩展的JFrame {
    公共无效GUI(){
       的setTitle(欢迎);
       的setResizable(假);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       调用setVisible(真);
       的setSize(600600);       JLabel的标题=新的JLabel();
       title.setText(苹果公司会员登录端口);
       title.setFont(新字体(宋体,Font.PLAIN,24));       JTextField的登录名=新的JTextField(登录,10);       JPasswordField中通=新JPasswordField(密码);       添加标题);
       加(登录);
       加(PASS);   }    公共静态无效的主要(字串[] args){
        FirstGUI一个=新FirstGUI();
        a.GUI();
    }
}

但是当我运行它,我得到这样的:


解决方案

  

但是当我运行它,我得到这样的:


由于您添加组件到帧之后的帧是可见的,您得到一个空白屏幕。


  1. 正如已经建议需要使用到一个适当的布局管理器。的FlowLayout是最容易下手。

  2. 调用调用setVisible(真)之后增加的组件框架。

所以,code应该更像:

  panel.add(...);
panel.add(...);
加(面板);
包();
调用setVisible(真);

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. As has already been suggested you need to use an appropriate layout manager. FlowLayout is the easiest to start with.
  2. invoke setVisible(true) AFTER adding the components to the frame.

So the code should be more like:

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

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

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