텍스트 파일에서 질문을 읽을 수있는 퀴즈 수업을해야합니다. 프로그램을 작동 시키려고 할 때마다 답변 버튼을 누를 때마다 질문 (텍스트 파일의 줄), 나는 인터넷에서 많은 제안을 시도했지만 프로그램을 실행하면 String[] args = new String[0];
을 사용하여 첫 번째 줄을 읽었을 때 버튼을 눌렀을 때 새로운 프레임이 생겼지 만 텍스트는 ' 전혀 읽지 않는다. 당신이 읽은 마지막 줄을 추적하기 위해 lines
변수를 사용버튼을 눌렀을 때 라인을 읽으십시오.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Frame {
JFrame frame;
JPanel pan1;
static JButton boutonRetour,boutonEnregistrer;
static JTextField reponse,question;
static int counter1 = 1;
static int lines=0;
public static void main(String[] args) {
final JFrame frame = new JFrame("Frame");
frame.setSize(700,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
question=new JTextField(10);
question.setBounds(150, 220, 450, 50);
question.setEditable(true);
frame.setContentPane(new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(null, 0, 0, 700, 600, this);
}
});
boutonEnregistrer=new JButton("Answer");
boutonEnregistrer.setBounds(0,513,100,50);
boutonEnregistrer.setBackground(new Color(0, 0, 182));
boutonEnregistrer.setForeground(Color.white);
boutonEnregistrer.setFocusPainted(false);
boutonEnregistrer.setFont(new Font("Tahoma", Font.BOLD, 12));
boutonEnregistrer.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
counter1++;
String[] args = new String[0];
main(args);
/*
SwingUtilities.updateComponentTreeUI(frame);
frame.revalidate();
frame.repaint();
frame.removeAll();
frame.invalidate();
frame.validate();
*/
}
});
frame.add(question);
frame.add(boutonEnregistrer);
frame.setLayout(null);
System.out.println(counter1);
try {
BufferedReader br =
new BufferedReader(new FileReader("Questions.txt"));
String word = null;
while((word =br.readLine()) != null) {
lines++;
if (lines == counter1){
question.setText(word);
break;
}
}
} catch(IOException err) {
}
frame.validate();
System.out.println(lines);
};
}
가 전혀 읽을 수없는 것입니다 텍스트 무엇을 의미합니까를? 또한 catch 블록에서 스택 추적 (err.printStackTrace())을 인쇄하여 오류가 있음을 최소한으로 알 수 있습니다. – Sujen
처음 프로그램을 실행하면 첫 번째 줄을 읽고 jtextfield에 표시하지만 버튼을 누르면 새 프레임이 열리지 만 텍스트 필드에 텍스트가 표시되지 않고 ... 나는 err.printStackTrace ()하지만 오류가 없습니다. 내가 필요로하는 방식으로 작동하지 않습니다. – Morray
BufferedReaders를 닫으십시오. finally 블록에서'br.close()'를 호출하십시오. BufferedReader의 두 번째 인스턴스가 첫 번째 스트림에서 닫힐 때까지 대기 할 수 있으므로 문제가 발생할 수 있습니다. – Sujen