2016-02-18 34 views
2

我還是java的新手,所以愚蠢的問題即將到來。我一直在使用一個簡單的軟件,使用JFramePrintWriter,FileScanner來創建和讀取文本文件,我用您輸入的名稱保存文件,然後將您輸入的數據保存到JTextField,問題是:只要你鍵入一個空格它不保存的文本空間到.txt文件後:當字符串在java中有空格時,沒有得到整個字符串

輸入

等它

WriteToFile(textarea.getText(), name.getText()); 
// my function input text   name 

輸出:

WAITFOR

但是,如果我輸入文本手動這種方式:

WriteToFile("waitFor it", name.getText()); 
// my function input text   name 

輸出:

等它

這使我認爲,我的功能可能不會造成這一點,但我又是個菜鳥。

Main.java

package creator; 

import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 


import library.Database; 

public class Main extends Database{ 

    public static void main(String[] args) { 

     JFrame frame = new JFrame(); 

     JButton button = new JButton("Erase"); 
     JButton button2 = new JButton("Add"); 
     JButton button3 = new JButton("Save to Database"); 

     Font font = new Font("Courier", Font.BOLD, 12); 
     Font font2 = new Font("Courier", Font.BOLD, 14); 

     final JTextField name = new JTextField(); 
     final JTextField editorPane = new JTextField(); 
     final JTextField textarea = new JTextField(); 

     final JScrollPane scroll = new JScrollPane (textarea, 
     JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

     frame.setTitle("Array Creator"); 
     frame.setSize(401, 250); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.setResizable(false); 
     frame.setLayout(null); 

     frame.add(button); 
     frame.add(name); 
     frame.add(button2); 
     frame.add(button3); 
     frame.add(editorPane); 
     frame.add(scroll); 

     button.setBounds(0, 200, 80, 22); 
     name.setBounds(82, 200, 80, 22); 
     button2.setBounds(164, 200, 80, 22); 
     button3.setBounds(246, 200, 148, 22); 
     editorPane.setBounds(100,175,200,18); 
     scroll.setBounds(50, 10, 300, 160); 
     textarea.setEditable(false); 

     button.setFont(font); 
     name.setFont(font); 
     button2.setFont(font); 
     button3.setFont(font); 
     editorPane.setFont(font); 
     textarea.setFont(font2); 

     textarea.setText("["); 

     frame.setVisible(true); 

     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       textarea.setText("["); 
       editorPane.setText(""); 
      } 
     }); 
     button2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       textarea.setText(textarea.getText()+"["+editorPane.getText()+"],"); 
       editorPane.setText(""); 
      } 
     }); 
     button3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       String temp = textarea.getText(); 
       temp = temp.substring(0, temp.length()-1); 
       textarea.setText(temp+"]"); 
       editorPane.setText(""); 
       WriteToFile(textarea.getText(), name.getText()); 
      } 
     }); 
     name.addKeyListener(new KeyListener() { 
      public void keyTyped(KeyEvent arg0) { 
      } 

      public void keyReleased(KeyEvent arg0) { 
       textarea.setText(readFile(name.getText())); 
      } 

      public void keyPressed(KeyEvent arg0) { 
      } 
     }); 

    } 

} 

Database.java

package library; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import java.io.UnsupportedEncodingException; 
import java.util.Scanner; 

public class Database { 

    public static String WriteToFile(String data, String name){ 
     String output = "ERROR"; 
     PrintWriter writer = null; 
     try { 
      writer = new PrintWriter(name+".txt", "UTF-8"); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     writer.println(data); 
     writer.close(); 

     File file = new File(name+".txt"); 
     try { 
      @SuppressWarnings("resource") 
      Scanner sc = new Scanner(file); 
      output = sc.next(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     return output; 
    } 

    public static String readFile(String name){ 
     String output = "["; 
     File file = new File(name+".txt"); 
     try { 
      @SuppressWarnings("resource") 
      Scanner sc = new Scanner(file); 
      output = sc.next(); 
     } catch (FileNotFoundException e) { 

     } 

     return output; 
    } 

} 

月有人向我提供關於爲什麼這是一個解釋?

+2

相反'輸出= sc.next();'用'輸出= sc.nextLine() ;'。 – Satya

+0

@Satya它的工作,感謝兄弟/ dudette +1 – Kyle

+2

避免使用'null'佈局,像素完美的佈局是現代UI設計中的幻想。影響組件的個體大小的因素太多,其中沒有一個可以控制。Swing旨在與佈局經理一起工作,放棄這些將導致問題和問題的終結,您將花費越來越多的時間來嘗試糾正 – MadProgrammer

回答

相關問題