繁体   English   中英

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

[英]Why are my items not showing up in JFrame?

我对 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();
    }
}

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

在此处输入图片说明

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

您会得到一个空白屏幕,因为您在框架可见后将组件添加到框架中。

  1. 正如已经建议的那样,您需要使用适当的布局管理器。 FlowLayout 是最容易开始的。
  2. 在将组件添加到框架后调用setVisible(true)

所以代码应该更像:

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

JFrame的默认布局管理器是BorderLayout

这意味着您的组件基本上都是相互叠加的。

尝试将布局管理器更改为FlowLayout东西(例如)...

在此处输入图片说明

有关更多详细信息,请查看布局管理器的可视化指南使用布局管理器。

另外,尽可能避免使用setSize ,而是使用Window#pack

更新

我还想向您介绍初始线程,它应该用于启动您的 UI 代码...

我同意 MadProgrammer 的建议 (+1)

好吧,让我们看看你的程序

您实际上已经创建了一个带有组件的 JFrame。 它也可以正常工作,但是您关于“为什么我的项目没有出现在 JFrame 中”的问题不是因为您做错了什么,而是因为错过了一些东西,即 revalidate()

尝试:

public static void main(String[] args){
        FirstGUI a = new FirstGUI();
        a.GUI();
        a.revalidate();
    }

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

revalidate():这个组件和它上面的所有父组件都被标记为需要布局。 这意味着布局管理器将尝试重新对齐组件。 通常在移除组件后使用。 一些真正剧烈摇摆的人可能会错过这一点。 我认为您只有在实际使用 Swing 时才会知道这一点。

唯一的一个原因:

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

如果您在创建框架时的代码顶部给出这一行。 这会导致那个问题。

不要将组件直接添加到您的框架中。 而是添加到内容窗格,这是 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函数设为构造函数。

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM