【问题标题】:Why are my items not showing up in JFrame?为什么我的项目没有出现在 JFrame 中?
【发布时间】:2022-01-16 01:46:46
【问题描述】:

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

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

【问题讨论】:

标签: java swing user-interface jframe awt


【解决方案1】:

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

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

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

所以代码应该更像:

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

【讨论】:

    【解决方案2】:

    JFrame 的默认布局管理器是 BorderLayout

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

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

    查看A Visual Guide to Layout ManagersUsing Layout Managers 了解更多详情。

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

    更新

    我还想向您介绍Initial Threads,它应该用于启动您的 UI 代码...

    【讨论】:

      【解决方案3】:

      我同意 MadProgrammer 的建议 (+1)

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

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

      试试:

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

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

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

      【讨论】:

        【解决方案4】:

        唯一的原因:

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

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:
            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);
                }
            }
            

            【讨论】:

            • 请添加对这段代码如何工作的解释(通过编辑答案)。
            猜你喜欢
            • 2012-10-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-08-03
            • 1970-01-01
            • 2015-12-14
            • 2012-10-03
            相关资源
            最近更新 更多