If I create a main method in my GUI class, and make an object, the program runs and works fine. However, when I try creating an object of this class in another file, the window quickly disappears.
I'm not sure why this is the case, I tried setting the frame to be visible and that doesn't work, I also tried creating a method that allows one to explicitly call the set visibility method and that didn't work either. I don't know what I'm doing wrong, any help is greatly appreciated.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class BoxGUI extends JFrame{
private JFrame frame;
public BoxGUI(String text1, String text2) {
//components
String title = "Test";
frame = new JFrame(title);
Container content = frame.getContentPane();
content.setLayout(new GridLayout(0,2, 3, 4)); //Creates Grid layout
JTextArea leftTextArea = new JTextArea();
leftTextArea.append("i am on the left");
leftTextArea.append(text1);
JTextArea rightTextArea = new JTextArea() {
public boolean isManagingFocus() {
return false;
}
};
rightTextArea.append("i am on the right");
rightTextArea.append(text2);
content.add(leftTextArea);
//leftTextArea.paste();
content.add(rightTextArea);
//rightTextArea.paste();
frame.pack();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);// closes frame on exit
}
public void displayGUI() {
this.frame.setVisible(true);
}
public void CreateTextAreaComponents(String text1,String text2) {
String title = "Test";
frame = new JFrame(title);
Container content = frame.getContentPane();
content.setLayout(new GridLayout(0,2, 3, 4)); //Creates Grid layout
JTextArea leftTextArea = new JTextArea();
leftTextArea.append("i am on the left");
leftTextArea.append(text1);
JTextArea rightTextArea = new JTextArea() {
public boolean isManagingFocus() {
return false;
}
};
rightTextArea.append("i am on the right");
rightTextArea.append(text2);
content.add(leftTextArea);
//leftTextArea.paste();
content.add(rightTextArea);
//rightTextArea.paste();
frame.pack();
frame.setVisible(true);
content.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);// closes frame on exit
}
public static void main (String []alex) {
/*
String text1, text2;
text1 = "\nhi, i amm on the left";
text2 = "\nhi, i am on the right!";
BoxGUI test = new BoxGUI(text1, text2);
test.displayGUI();
*/
}
}
my other file.
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Project1{
public static void main(String []args){
String text1, text2;
text1 = "\nhi, i amm on the left";
text2 = "\nhi, i am on the right!";
BoxGUI test = new BoxGUI(text1, text2);
test.displayGUI();
}
}