【问题标题】:Put multiple GUI classes into one window JAVA将多个 GUI 类放入一个窗口 JAVA
【发布时间】:2017-08-02 00:45:31
【问题描述】:

我希望将多个 GUI 类放入一个窗口中。我通过创建一个“主”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);
    }

用户界面类

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

图形界面类

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


    }

【问题讨论】:

  • 因为你所有的类都继承Frame 这是一个顶级窗口。它们需要变成小部件并放置在容器类中

标签: java swing user-interface interface


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 我已将 UserGUI 和 StockGUI 转换为扩展的 JPanel 而不是 Frame - 现在我的 GUI 类中出现“无法共享 boxlayout”的错误。如果我切换到 CardLayout - 如何将我的 JPanel 添加到 JFrame?
  • now I'm getting an error in my GUI class that says "boxlayout can't be shared". - 所以不要共享 BoxLayout。您需要使用不同的面板作为构造函数中的参数创建两个 BoxLayout 类。阅读How to use BoxLayout 上的教程部分,了解更多信息和工作示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 2013-11-04
  • 2016-09-23
  • 2021-07-27
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
相关资源
最近更新 更多