gpt4 book ai didi

java - GUI 堆栈溢出错误

转载 作者:行者123 更新时间:2023-12-02 12:16:10 26 4
gpt4 key购买 nike

我正在尝试使用 JAVA Swing 创建一个 UI 以用于学习目的。我一直在高速公路上行驶,直到我尝试将 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对称请帮我解决这个问题。

最佳答案

您是否真的需要从 Main 继承Assignment1 类,您根本没有使用任何继承属性。

问题是在 Main 的对象构造函数上,您尝试启动Assignment1对象,该对象又将其 super 构造函数(即Main的构造函数)类化,该对象又再次调用Assignment1的构造函数来创建对象,此循环继续进行并递归地进行,没有任何停止。因此出现堆栈溢出错误。

如果您确实不想继承,因为您没有使用父类中的任何继承,请尝试删除 extends Main

关于java - GUI 堆栈溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com