Java 为什么我的项目没有显示在JFrame中?

Java 为什么我的项目没有显示在JFrame中?,java,swing,user-interface,jframe,awt,Java,Swing,User Interface,Jframe,Awt,我是JFrame的新手,我想知道为什么我的项目没有显示在窗口上。我知道我没有ActionHandler,但我只希望我的文本字段显示在我的窗口上。这是我的密码: import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class FirstGUI exten

我是JFrame的新手,我想知道为什么我的项目没有显示在窗口上。我知道我没有ActionHandler,但我只希望我的文本字段显示在我的窗口上。这是我的密码:

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();
    }
}
但当我运行它时,我得到了:


JFrame的默认布局管理器是
BorderLayout

这意味着您的组件基本上都被添加到彼此的顶部

尝试将布局管理器更改为类似于
FlowLayout
(例如)

请查看和以了解更多详细信息

另外,尽可能避免使用
setSize

更新


我还想向您介绍启动UI代码时应该使用的组件…

不要将组件直接添加到框架中。而是添加到内容窗格中,JFrame在其中存储它绘制的所有组件。通常这是一个JPanel

以下是一个例子:

public class GUI
{

    private JPanel content;

    public void GUI
    {
        /*Other code*/

        content = new JPanel();
        add(content); //make content the content pane

        content.add(title);
        content.add(login);
        content.add(pass);
    }
如果失败,请在所有组件上调用
setVisible(true)
setEnabled(true)

另一方面,您可能希望将
GUI
函数设置为构造函数

但当我运行它时,我得到了:

由于在框架可见后将组件添加到框架,因此会出现一个空屏幕

  • 正如已经建议的那样,您需要使用适当的布局管理器。FlowLayout是最容易开始的
  • 将组件添加到框架后调用
    setVisible(true)
  • 因此,代码应该更像:

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

    我同意程序员的建议(+1)

    好吧,让我们看看你的节目

    实际上,您已经创建了一个包含组件的JFrame。它的工作也很好,但是你的问题“为什么我的项目没有显示在JFrame中”不是因为你做错了什么,而是因为遗漏了什么,例如重新验证()

    尝试:

    我不是说这会给你一个完美的用户界面。。我想说的是,这将帮助你更好地理解秋千。了解Swing布局管理器,然后使用UI获得更好的结果

    revalidate():此组件及其上面的所有父组件都标记为需要布局。这意味着布局管理器将尝试重新对齐组件。通常在拆下部件后使用。有些人可能会忽略这一点。我认为只有在实际使用Swing时,您才会知道这一点。

    唯一的原因是:

    setVisible(True); method for the frame should be put on the end of the code.
    

    如果在创建框架时在代码顶部给出这一行。这将导致该问题。

    我认为您需要将组件添加到
    上下文窗格中,而不是直接添加到框架中。请参阅@PM77-1我相信由于Java 5,这不再是必需的,因为
    JFrame#为您的内容窗格添加
    委托请添加此代码工作原理的解释(通过编辑答案)。
    
    import javax.swing.*;
    import java.awt.*;
    class Myframec extends JFrame
    {
    
        Myframec()
        {
            Container c = this.getContentPane();
            c.setLayout(null);
    
            this.setBounds(10,10,700,500);
            this.setTitle("Welcome");
    
            this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            panel.setBounds(0,0,700,500);
            panel.setBackground(Color.gray);
            panel.setLayout(null);
            c.add(panel);
            Font f = new Font("Arial",Font.BOLD,25);
            Font f1 = new Font("Arial",Font.BOLD,20);
            JLabel lable = new JLabel();
            lable.setBounds(130,10,400,100);
            lable.setText("Apple Inc. Member Login Port");
            lable.setFont(f);
            panel.add(lable);
            JTextField login = new JTextField("Login",10);
            login.setBounds(120,150,400,30);
            login.setFont(f1);
            panel.add(login);
            JPasswordField pass =new JPasswordField("Password");
            pass.setBounds(120,200,400,30);
            pass.setFont(f1);
    
    
            lable.setFont(f);
            panel.add(pass);
            c.setVisible(true);
            this.setVisible(true);
        }
        public static void main(String[] argm)
        {
            Myframec frame = new Myframec();
            frame.setVisible(true);
        }
    }
    
    import javax.swing.*;
    import java.awt.*;
    class Myframec extends JFrame
    {
    
        Myframec()
        {
            Container c = this.getContentPane();
            c.setLayout(null);
    
            this.setBounds(10,10,700,500);
            this.setTitle("Welcome");
    
            this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            panel.setBounds(0,0,700,500);
            panel.setBackground(Color.gray);
            panel.setLayout(null);
            c.add(panel);
            Font f = new Font("Arial",Font.BOLD,25);
            Font f1 = new Font("Arial",Font.BOLD,20);
            JLabel lable = new JLabel();
            lable.setBounds(130,10,400,100);
            lable.setText("Apple Inc. Member Login Port");
            lable.setFont(f);
            panel.add(lable);
            JTextField login = new JTextField("Login",10);
            login.setBounds(120,150,400,30);
            login.setFont(f1);
            panel.add(login);
            JPasswordField pass =new JPasswordField("Password");
            pass.setBounds(120,200,400,30);
            pass.setFont(f1);
    
    
            lable.setFont(f);
            panel.add(pass);
            c.setVisible(true);
            this.setVisible(true);
        }
        public static void main(String[] argm)
        {
            Myframec frame = new Myframec();
            frame.setVisible(true);
        }
    }