我将 JLabel 设置为等于从文件中读取信息的字符串,每当我想将它添加到我的 JFrame 并加载 JLabel 不会显示的类时。字符串 intromsgstring 正在工作,我知道这是因为我使用 System.out.println() 对其进行了测试。
这是代码
import java.awt.*;
import java.util.Scanner;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.color.*;
import java.awt.Graphics;
import java.io.File;
import java.io.FileNotFoundException;
public class intro extends JFrame{
//Variables
public Font font = new Font("Arial", Font.BOLD, 20);
public String playername;
public String intromsgstring;
public JLabel intromsg = new JLabel(intromsgstring);
public JFrame window = new JFrame();
//Screen Settings
public intro(){
setSize(1100,650);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
setTitle("TEST");
setBackground(Color.BLACK);
}
//Paint Method
public void paint(){
try {
Scanner scan = new Scanner(new File("/Users/AlecStanton/Desktop/op1/name.txt"));
intromsgstring = "Oh! Hello there. Welcome to the land of lylaid. So your name is " + scan.nextLine();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Intro Box
intromsg.setFont(font);
intromsg.setAlignmentX(170);
intromsg.setAlignmentY(535);
window.add(intromsg);
}
//Object
public static void main(String[] args) {
intro intro = new intro();
}