为什么我的商品没有出现在JFrame中?

时间:2013-05-07 00:52:03

标签: 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 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();
    }
}

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

enter image description here

6 个答案:

答案 0 :(得分:7)

  

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

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

  1. 正如已经建议的那样,您需要使用适当的布局管理器。 FlowLayout是最容易入手的。
  2. 在将组件添加到框架后调用setVisible(true)
  3. 所以代码应该更像:

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

答案 1 :(得分:1)

JFrame的默认布局管理器为BorderLayout

这意味着您的组件基本上都是彼此添加的。

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

enter image description here

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

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

<强>更新

我还想向您介绍应该用于启动您的用户界面代码的Initial Threads ...

答案 2 :(得分:1)

我同意MadProgrammer的建议(+1)

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

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

尝试:

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

我不是说这会给你提供完美的UI ..我想说的是这将有助于你更好地理解Swing。了解Swing Layout管理器,然后在UI上工作以获得更好的结果

revalidate():此组件及其上方的所有父项都标记为需要布局。这意味着布局管理器将尝试重新对齐组件。删除组件后经常使用。一些非常尖锐的人可能会错过这个。如果你真的使用Swing,我认为你只会知道这个。

答案 3 :(得分:1)

唯一的原因是:

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

如果您将此行放在创建框架时代码的顶部。这会导致这个问题。

答案 4 :(得分:0)

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

答案 5 :(得分:-1)

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);
    }
}
相关问题