Reputation: 168
I am trying to create a UI with JAVA Swing for studying purpose. I was running on an Autobahn until I tried to separate my main.java file and my panel components. What I was trying to do here was to separately create classes for different assignments so that I could keep my main.java file short and easy to modify as well as my independent assignment files. I will post an anticipated result which works when put in a single file, and my decoupled codes.
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;
}
}
I think the problem here has to do something with JFrame. I hava not posted Assignment_2 java since it is symmetrical with Assignment_1 Please help me out with this matter.
Upvotes: 0
Views: 396
Reputation: 203
Do you really need Assignment1 class to be inherited from Main, you are not at all using any inheritance properties.
The problem is On object constructor of Main, you are trying to initiate Assignment1 object, which in turn class its super constructor, which is Main's constructor, which in turn again invokes constructor of Assignment1 to create a object, this loops go on and on recursively without any stoppage. Hence stackoverflow error.
Just try removing extends Main, if you really donot want inheritance as you are not using any from parent class
Upvotes: 1