我設置等於從文件中讀取信息,每當我想將它添加到我的JFrame和裝載類選擇JLabel不會出現一個字符串的JLabel。字符串intromsgstring正在工作,我知道,因爲我用System.out.println()測試了它。的JLabel沒有驗證
下面是代碼
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();
}
那麼我該如何修復我的progarm? – user2540802
我猜測我應該在另一種方法中添加掃描儀呢? – user2540802
但是,爲什麼isint我的jlabel出現在jframe上,如果它正確加載內容的話? – user2540802