How would one go about sharing an object between static methods? Is this even possible? Is there a better way?
In my code, I have one frame in one class. I have different static methods to do stuff with this frame in the same class. I call these methods in another class.
Is there a better way of doing this for swing? Essentially I want to create a Frame, in which I can edit the size, and colour, and contents through the methods of other classes (these classes would have Panels which I would use on this frame).
Class calling the static methods:
import javax.swing.*;
public class Main{
// Display Login Window
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try
{
new loginFrames();
loginFrames.showFrame("hello", 1200, 800);
}
catch (Exception e)
{
e.printStackTrace();
}
});
}
}
Class with the static methods:
import javax.swing.*;
import java.awt.*;
public class loginFrames{
public JFrame appFrame;
public void createFrame(String frameName, int frameW, int frameH) {
// Create JFrame with frame name, width, and height
appFrame = new JFrame(frameName);
appFrame.setSize(frameW, frameH);
// Get the content pane and set the background colour black
Container absFrameContentPane = appFrame.getContentPane();
absFrameContentPane.setBackground(Color.BLACK);
// Show the frame
appFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
appFrame.setVisible(true);
}
// Callable from Main class
public static void showFrame(String frameName, int frameW, int frameH) {
loginFrames applicationFrame = new loginFrames();
applicationFrame.createFrame(frameName, frameW, frameH);
}
// Callable from Main class
public static void appAddPanel(String frameName, int frameW, int frameH) {
// How do I call the same object above, here?
}
}