在GUI中一次只能显示一个项目

时间:2016-03-23 22:04:28

标签: java user-interface

我试图在我的GUI中添加标签,按钮等,但程序只显示添加到窗口的最后一项。我不明白为什么他们不会一个接一个地去。几乎看起来我把它们放在彼此之上。附上我的代码。

import java.awt.*;
import java.awt.event.*;

public class ZooGUIStafford extends Frame {

    //Text fields for animal input
    TextField ani1, ani2, ani3, ani4, ani5;

    public ZooGUIStafford()
    {


        //Size of window
        this.setTitle("Zoo Database");
        this.setSize(400, 400);
        this.setVisible(true);

        //Welcome label
        Label welcome = new Label("Welcome to the Zoo Database!");
        this.add(welcome);

        Label an1 = new Label("Enter animal type 1: ");
        this.add(an1);



        addWindowListener(new MyWindowAdapter(this));

    }

    class MyWindowAdapter extends WindowAdapter{

        ZooGUIStafford myWindow = null;

        MyWindowAdapter(ZooGUIStafford myWindow)
        {
            this.myWindow = myWindow;
        }

        public void windowClosing(WindowEvent e)
        {
            myWindow.setVisible(false);
        }
    }



    public static void main(String args[])
    {
        new ZooGUIStafford();

    }

}

1 个答案:

答案 0 :(得分:0)

拜托,

添加布局,

 this.setLayout(new FlowLayout());

OutPut:

enter image description here