GUI Stackoverflower

时间:2017-09-12 02:16:34

标签: java swing

我正在尝试使用JAVA Swing创建一个用于学习目的的UI。我在Autobahn上运行,直到我尝试将main.java文件和面板组件分开。我在这里尝试做的是分别为不同的任务创建类,以便我可以保持我的main.java文件简短易修改以及我的独立分配文件。 我将发布预期的结果,该结果在放入单个文件时起作用,并且我的解耦代码。

Main.java

    package gui;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Main {

    //Basic Components
        JFrame frame = new JFrame();

        JMenuBar menubar = new JMenuBar();
        JMenu menuPanels = new JMenu("Assignments");
        JMenuItem assign_1 = new JMenuItem("Assignment 1");
        JMenuItem assign_2 = new JMenuItem("Assignment 2");
        JMenuItem assign_3 = new JMenuItem("Assignment 3");


        JPanel panel = new JPanel();
        JPanel pnlAss_1 = new JPanel();
        JPanel pnlAss_2 = new JPanel();
        JPanel pnlAss_3 = new JPanel();

        CardLayout cardLayout = new CardLayout();

        public Main()
        {
            panel.setLayout(cardLayout);

            Assignment_1 pnlAss_1_class = new Assignment_1();
            pnlAss_1 = pnlAss_1_class.CreatePanel(pnlAss_1);



            panel.add(pnlAss_1, "pnlAss_1");
            panel.add(pnlAss_2, "pnlAss_2");

            cardLayout.show(panel, "pnlAss_1");

            //Menubars and menupanels
            menubar.add(menuPanels);
            menuPanels.add(assign_1);
            menuPanels.add(assign_2);

            assign_1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent E)
                {
                    cardLayout.show(panel, "pnlAss_1");
                }
            });
            assign_2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    cardLayout.show(panel, "pnlAss_2");
                }
            });

            frame.setJMenuBar(menubar);
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(800, 300);
            frame.setVisible(true);
        }

        public static void main(String[] ar)
        {
            new Main();
        }
}





Assignment_1.java





    package gui;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Assignment_1 extends Main {

    JLabel ID, Name, Contact, Address; JLabel Intro;
    JTextField id, name, contact, address;

    JButton close;

    Assignment_1()
    {

    }

    JPanel CreatePanel(JPanel panel)
    {
        panel.setSize(500, 300);
        panel.setLayout(null);

        Intro = new JLabel("First Assignment"); Intro.setBounds(10, 10, 300, 20);
        panel.add(Intro);

        //Labels definition & addition
        ID = new JLabel("Student ID"); ID.setBounds(10, 30, 100, 20);
        Name = new JLabel("Name"); Name.setBounds(110, 30, 100, 20);
        Contact = new JLabel("Phone Number"); Contact.setBounds(210, 30, 100, 20);
        Address = new JLabel("GitHub Address"); Address.setBounds(310, 30, 100, 20);
        panel.add(ID); panel.add(Name); panel.add(Contact); panel.add(Address);

        //TextFields definition & addition
        id = new JTextField("2012203079"); id.setBounds(10, 50, 100, 20); id.setEditable(false);
        name = new JTextField("정상원"); name.setBounds(110, 50, 100, 20); name.setEditable(false);
        contact = new JTextField("010-8611-9898"); contact.setBounds(210, 50, 100, 20); contact.setEditable(false);
        address = new JTextField("https://github.com/swj0418"); address.setBounds(310, 50, 160, 20); address.setEditable(false);
        panel.add(id); panel.add(name); panel.add(contact); panel.add(address);

        //Buttons definition& addition
        close = new JButton("Close");   
        close.setBounds(400, 100, 70, 30);
        close.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
        panel.add(close);

        return panel;
    }
}

我认为这里的问题必须与JFrame有关。 我没有发布Assignment_2 java,因为它与Assignment_1是对称的 请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

你真的需要从Main继承Assignment1类,你根本不使用任何继承属性。

问题是在Main的对象构造函数中,您正在尝试启动Assignment1对象,该对象依次对其超级构造函数进行分类,这是Main的构造函数,后者又调用Assignment1的构造函数来创建对象,此循环继续并且递归地没有任何停顿。因此stackoverflow错误。

尝试删除extends Main,如果你真的不想继承,因为你没有使用任何来自父类的