我正在尝试将JFrame
文本文件中的每一行读入 aJLabel
并将其JLabel
放入 aJScrollPane
中,JScrollPane
然后将其添加到我的窗口的容器中。内容和标签已加载,但有一个问题,一旦窗口加载我的JScrollPane
拉链并离开,Container
留下一个空白窗口。有什么帮助吗?这是代码:
import java.awt.Container;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class Helper extends JFrame{
String filepath = "assets/help.txt";
Scanner reader;
Container contentpane;
JScrollPane scrp = new JScrollPane(null);
JLabel line = new JLabel();
int align_x, align_y;
int window_width = 700;
int window_height = 700;
public Helper(){
setSize(window_height,window_width);
setVisible(true);
setTitle("Electric Force Calculator Helper");
scrp.setSize(window_height,window_width);
scrp.setVisible(true);
contentpane = getContentPane();
reader = new Scanner(getClass().getResourceAsStream(filepath));
align_x = 10;
align_y = 10;
while(reader.hasNextLine()){
line = new JLabel(reader.nextLine());
line.setBounds(align_x,align_y,window_width,20);
scrp.add(line);
align_y+=20;
}
contentpane.add(scrp);
}
public static void showHelpFrame(){
Helper newHelper = new Helper();
}
}