2017-01-20 3 views
-1

나는 게임을하고 있습니다. 이 부분에서는 새 창이 열리고 게임 지침이 표시됩니다. 유일한 문제는 JTextArea가 20 개 이상의 행이있을 때 .txt 파일의 한 행만 표시한다는 것입니다. 나는 이것에 초보자이다. 그래서 나는 내가 무엇을 놓치고 있는지 모른다. 감사!자바 스윙 JTextArea가 작동하지 않습니다.

class Instruction extends JFrame 
{ 
private JTextArea read; 
private JScrollPane scroll; 
Instruction(String x) 
{ 
super(x); 

try 
{ 
    BufferedReader readers = new BufferedReader (new FileReader("instructions.txt")); //read from file 

    read = new JTextArea(readers.readLine()); 
    scroll = new JScrollPane(read); 
    read.setFont(new Font("Comic Sans MS", Font.BOLD, 16)); // change font 
    read.setEditable(false); 
    add(read); 
} 

catch(IOException exception) 
{ 
    exception.printStackTrace(); 
} 
} 
}  
+1

'readers.readLine()'은 한 줄만 읽습니다. – MadProgrammer

+0

.............. yikes! –

답변

3

BufferedReader#readLine은 다음 라인을 읽고 (또는 읽을 수 더 이상 선이없는 경우 null를 반환)은 JavaDoc을 좀 더 자세히 살펴 경우 JTextAreaJTextComponent에서 read(Reader, Object)를 상속 것을 발견 할 것이다

read = new JTextArea(); 
try (Reader reader = new BufferedReader(new FileReader("instructions.txt"))) { 
    read.read(reader, null); 
} catch (IOException exception) { 
    exception.printStackTrace(); 
} 
scroll = new JScrollPane(read); 
read.setFont(new Font("Comic Sans MS", Font.BOLD, 16)); // change font 
read.setEditable(false); 
add(read); 
,174의 라인을 따라 더 해결되는 (대부분의) 문제의이

뭔가

당신은 또한

을 위해 노력하고 무엇을 달성 할 수있다, 당신은 그들이 지역의 가시 경계를 넘어 확장 할 경우 단어를 자동으로 배치 할 수 있도록

read.setLineWrap(true); 
read.setWrapStyleWord(true); 

를 호출 할 필요가 있습니다.

-1

파일에서 한 줄만 읽습니다. 대신이 파일을 사용하여 전체 파일을로드 해보십시오.

List<String> lines; 
try { 
    lines = Files.readAllLines(); 
} catch (IOException ex) { 
    ex.printStackTrace(); 
} 

StringBuilder text = new StringBuilder(); 
for (String line : lines) { 
    text.append(line); 
} 

read = new JTextArea(text.toString()); 
+0

'JTextArea # read'는 ~ 5 행을 한 줄로 처리합니다 ...하지만 게으 르네요 : P – MadProgrammer

+0

@MadProgrammer 그래, 잘 모르겠다. – ElectroWeak

관련 문제