I'm an IT student, I've recently been using Swing in Java, I tried to create a very simple class that extends JFrame, which takes a String in the constructor and adds the contents of the string inside a JLabel, contained in turn in the JFrame,
public class DocumentFrame extends JFrame {
private JLabel textLabel;
private JScrollPane scrollPane;
public DocumentFrame(String title, String text) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,500);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
getContentPane().setBackground(Color.BLACK);
String processText = text.replaceAll("[.;]", "$0<br>");
textLabel = new JLabel("<html>"+processText+"</html>");
textLabel.setHorizontalAlignment(JLabel.LEFT);
textLabel.setVerticalAlignment(JLabel.TOP);
textLabel.setForeground(Color.WHITE);
JScrollPane scrollPane = new JScrollPane(textLabel);
add(scrollPane, BorderLayout.CENTER);
pack();
setVisible(true);
}
}
If I try to instantiate an object of this class inside the main method where I created the class itself, everything is fine, the frame is displayed correctly; if instead I try with the same instruction to instantiate an object of that class within a project I'm doing with some of my friends, I don't know why the frame appears, but it's all "buggated", the image is not displayed, the X key at the top right of the frame does not work, to close the frame I would have to finish the execution of the program manually.
This is what I do in the main method contained in the class itself
public static void main(String[] args) {
String text = "I put a long text";
new DocumentFrame("title frame", text);
}
and that's fine, if I do this from another part of my project it doesn't work.