2016-11-04 1 views
-1

이것은 완전한 코드는 아니지만 특정 경우에 따라 JTextArea에 파일의 내용을 추가하려고합니다. 내 .txt 파일 나는 개방 파일 내용을 보여주는 Jtextarea에 추가 스캔 된 내용 중 isntead

TEST 
TEST 
TEST 
COLOR 100 100 100 

다음과 같은 결과

Input Instruction Not Recognized1 
Input Instruction Not Recognized2 
Input Instruction Not Recognized3 

디버깅 내 콘솔에 게시 얻을 내 JTextArea에이
C:\Users\c3462292\Documents\MuseLog.txt 

그러나 나는 그것을 걸려 포함이 포함되어있어 컬러 100 100 100 인 유일한 유효한 라인을 표시하고 JTextAera에서 디버깅 된 라인을 순서대로 표시하지만 누군가가 도움을 얻을 수 있다는 희망을 파악할 수 없습니다.

  JMenuItem mntmOpen = new JMenuItem("Open"); 
    mntmOpen.setIcon(new ImageIcon(Fucksakes.class.getResource("/Icons/Open.png"))); 
    mntmOpen.setFont(new Font("Roboto Condensed", Font.PLAIN, 14)); 
    mnFile.add(mntmOpen); 
    mntmOpen.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     JFileChooser broswer = new JFileChooser(); 
     int result = broswer.showOpenDialog(contentPane); 
     if (result == JFileChooser.APPROVE_OPTION) { 
     File selectFile = broswer.getSelectedFile(); 
     try { 
     Scanner sc = new Scanner(selectFile); 
     while (sc.hasNext()) { 
      lineNo++; 
      process(sc.nextLine()); 
     } 
     textArea.append(selectFile + ""); 
     } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     JOptionPane.showMessageDialog(contentPane, "Cannot Open file"); 
     } 
     } 
     } 
     }); 

public static void process(String s) { 
    String[] a = s.split(" "); 

    // Strings can be used in switch statements in Java 7 and later versions 
    try { 
     switch (a[0]) { 
      case "COLOR": 
       currentColor = new Color(Integer.parseInt(a[1]), Integer.parseInt(a[2]), Integer.parseInt(a[3])); 
       break; 

      default: 
       System.out.println("Input Instruction Not Recognized"); 
     } 

답변

0

당신은 위의 코드에 일단 JTextArea에 append(...)를 호출하고 완전한 놀라운 일이 안 있도록, 당신이보고있는 텍스트를 추가합니다. 아니요. 파일에 텍스트를 추가하려고하는 경우도 있습니다. 따라서 내가 할 수있는 일은 먼저 코드를 편집하여 그렇게하는 것입니다.

다음으로 a 배열의 첫 번째 문자열 만 테스트하고 a[0]은 TEST 만 포함합니다. 물론 스위치 블록은 기본 라인으로 갈 것입니다.

+0

JTextArea에 다른 행을 추가하는 방법을 이해하는 데 어려움을 겪고 있으므로 예제를 줄 수있는 기회가 있습니까? – Savage

+0

@Savage : 예를 들면? 당신은 그 코드에 너무 많은 이슈를 가지고 있습니다 - 한 번에 하나씩 쪼개고 정복하고 해결하려고 노력하십시오. –

+0

"Input Instruction Not Recognized"를 제 jtextarea에 넣는 것을 도와 주시겠습니까? 나는 하루 종일 고생하고있다. – Savage