파일을 열어 텍스트 영역에 추가하려고하지만 텍스트 파일이 작동하지 않는 이유를 정확히 알지 못합니다.하지만 컴파일러가 함수/메소드에 도달하여 읽음을 구현한다는 것을 알고 있습니다. 파일 코드. 여기에 내가 이클립스를 실행하고 코드Java 입력 및 파일 판독기
public class PictureAndButton extends JFrame implements ActionListener
{
private JMenuItem menuOptionOne = new JMenuItem("Lägg till text", KeyEvent.VK_L);
private JTextField textFalt = new JTextField();
public PictureAndButton()
{
menuInfo.add(menuOptionOne);
menuOptionOne.addActionListener(this);
textField.addActionListener(this);
setSize(350, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == menuOptionOne)
{
readInFile(textField.getText());
}
}
private void readInFile(String hej)
{
try
{
BufferedReader inFile = new BufferedReader(new FileReader(hej));
while(true)
{
String rad = inFile.readLine();
if(rad == null)
break;
textArea.append(rad);
}
}
catch(IOException e){}
}
public static void main(String[] args)
{
PictureAndButton peanutButter = new PictureAndButton();
}
}
내가 오류를 받고 있지 않다, 텍스트 필드에 입력 된 텍스트는 모두 변경 나던. 텍스트 영역에 아무 것도 추가되지 않습니다. 정말이
1) 무한 루프가 있습니다. 2.) 코드에서 readInFile()을 호출하지 마십시오. 오, 죄송합니다, – Polygnome
. 코드의 잘못된 부분을 입력했습니다. –
조언 몇; 코드 예제를 제공 할 때 일반적으로 변수의 이름을 영어로 된 단어에 대한 단서를 제공하는 데 도움이됩니다. 예를 들어, rad -> line, hej -> file, "lag to text"-> "add text"는 사람들이 여러분이 훑어보고있는 것을 이해하고 궁극적으로 유용한 답을 얻는 데 도움이됩니다. – posdef