将多个GUI类放入一个窗口JAVA

时间:2017-03-11 18:56:45

标签: java swing user-interface interface

我有多个GUI类,我希望将它放在一个窗口中。我通过创建一个“Master”gui类来尝试这个,它创建了我所制作的各个gui的对象 - 但是它们只显示在他们自己的单独窗口中。

如何将它们全部添加到一个窗口?

我以为我只是在创建容器,然后我可以将它放入主容器中。

这是我的代码:

StockGUI类

public class StockGUI extends Frame implements ActionListener
{
    // instance variables - replace the example below with your own
    private JTextPane currentStockValue = new JTextPane();

    Font f = new Font(Font.SANS_SERIF, 1, 30);

    /**
     * Constructor for objects of class StockGUI
     */
    public StockGUI()
    {
        //calling the super constructor from class Frame
        super("Stock Market Display");
        //setting the window size of the GUI
        setSize(600, 600);
        //selecting a predefined layout for the GUI
        setLayout(new GridLayout());
        currentStockValue.setFont(f);
        add(currentStockValue);

        setVisible(true);
    }

UserGUI类

public class UserGUI extends Frame implements ActionListener
{
    // instance variables - replace the example below with your own
    private JTextPane currentStockValue = new JTextPane();
    private JButton createUser;
    private JLabel enterUsername;
    private JTextField username;
    Font f = new Font(Font.SANS_SERIF, 1, 30);

    /**
     * Constructor for objects of class UserGUI
     */
    public UserGUI()
    {
        // initialise instance variables
        super("User Info");
        //setting the window size of the GUI
        setSize(600, 400);
        //selecting a predefined layout for the GUI
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        enterUsername = new JLabel("Enter your new username:");
        enterUsername.setFont(f);
        username = new JTextField();
        username.setFont(f);

        createUser = new JButton("Create User");
        createUser.setFont(f);
        createUser.addActionListener(this);        

        add(enterUsername);
        add(username);
        add(createUser);

        setVisible(true);

        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent ev){
                    System.exit(0);
                }
            });
    }

GUI类

public class GUI extends Frame
{
    // instance variables - replace the example below with your own
    private UserGUI userui;
    private StockGUI stockgui;

    /**
     * Constructor for objects of class GUI
     */
    public GUI() 
    {
        // initialise instance variables
        super("User Info");
        //setting the window size of the GUI
        setSize(600, 400);
        //selecting a predefined layout for the GUI
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        stockgui = new StockGUI();
        userui = new UserGUI();


        //add(userui);
        //add(stockgui);

        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent ev){
                    System.exit(0);
                }
            });

        setVisible(true);


    }

1 个答案:

答案 0 :(得分:0)

首先,Swing组件以“J”开头,因此您应该使用JFrame作为顶级容器。

  

我以为我只是在创建容器,然后我可以将它放入主容器中。

是的,但用于保存Swing组件的容器是JPanel。因此,您将拥有一个主类来创建框架和相关的子面板。

在您的情况下,GUI类似乎是主类。然后UserGUIStockGUI应该是可以添加到框架中的JPanel。

您可能希望使用CardLayout来显示这些面板,然后您可以根据需要交换面板。查看How to Use CardLayout上Swing教程中的部分,了解更多信息和工作示例,以帮助您入门。

相关问题