2013-04-20 120 views
-7

我相當新的Java,所以請容易對我!字符串解析 - Java

我已經創建了一個窗口,窗格,標籤,文本字段等

什麼是看到什麼用戶已經輸入到文本字段(如字符串)的最簡單的代碼,因爲我知道一個浮動你可以使用Float.parseFloat(txtName.getText());

+1

這個問題非常模糊,需要更多的澄清才能得到正確的回答。 – syb0rg 2013-04-20 16:38:15

+0

不知道爲什麼這得到這麼多downvotes,尤其是。沒有評論。我相信其他人一定會問同樣的事情。無論如何,你的方法是正確的,但如果用戶沒有輸入有效的浮點數,一定要用try/catch包圍。 – 2013-04-20 16:39:54

+0

我發現你的答案很有用,但愛德華福克的答案與我正在寫的程序更相關! – Olivia 2013-04-20 17:49:00

回答

1

注意:如果你想真正做它是正確的,你可以實現一個JTextField的子類,它甚至不會讓用戶輸入無效浮點數的字符:

public class FloatField extends JTextField { 

    public FloatField(int cols) { 
     super(cols) 
    } 

    protected Document createDefaultModel() { 
     return new FloatDocument(); 
    } 

    static class FloatDocument extends PlainDocument { 

     public void insertString(int offs, String str, AttributeSet a) 
          throws BadLocationException { 
      if(str == null) 
       return; 

      // Reject any string with invalid float characters. 
      // TODO: this could be more sophisticated 
      int len = str.length(); 
      for (int i = 0; i < len; ++i) { 
       char c = str.charAt(i); 
       if (c != '.' && c != '-' && !Character.isDigit(c)) 
        return; 
      } 
      super.insertString(offs, str, a); 
     } 
    } 
} 

注意:這只是一個不完美的實現,僅用於說明目的,仍然會有許多無效的浮點數,這將允許泄漏。

1

對於我的問題的理解,這裏是輸出的JTextField輸入了什麼東西,並將它附加到JTextArea的一個簡單的例子:

enter image description here

public class Test { 
    private static String ENTER = "Enter"; 
    static JButton enterButton; 
    public static JTextArea output; 
    public static JTextField input; 
    static JFrame frame; 
    static JPanel panel; 

    public static void main(String... args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     createFrame(); 
    } 

    public static void createFrame() 
    { 
     frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     panel = new JPanel(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
     panel.setOpaque(true); 
     ButtonListener buttonListener = new ButtonListener(); 
     output = new JTextArea(15, 50); 
     output.setWrapStyleWord(true); 
     output.setEditable(false); 
     JScrollPane scroller = new JScrollPane(output); 
     scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     JPanel inputpanel = new JPanel(); 
     inputpanel.setLayout(new FlowLayout()); 
     input = new JTextField(20); 
     enterButton = new JButton("Enter"); 
     enterButton.setActionCommand(ENTER); 
     enterButton.addActionListener(buttonListener); 
     input.setActionCommand(ENTER); 
     input.addActionListener(buttonListener); 
     DefaultCaret caret = (DefaultCaret) output.getCaret(); 
     caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); 
     panel.add(scroller); 
     inputpanel.add(input); 
     inputpanel.add(enterButton); 
     panel.add(inputpanel); 
     frame.getContentPane().add(BorderLayout.CENTER, panel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     // Center of screen 
     // frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setResizable(false); 
     input.requestFocus(); 
    } 

    public static class ButtonListener implements ActionListener 
    { 

     public void actionPerformed(final ActionEvent ev) 
     { 
      Thread thread = new Thread() 
      { 

       public void run() 
       { 
        if (!input.getText().trim().equals("")) 
        { 
         String cmd = ev.getActionCommand(); 
         if (ENTER.equals(cmd)) 
         { 
          output.append(input.getText() + "\n"); 
         } 
        } 
        input.setText(""); 
        input.requestFocus(); 
       } 
      }; 
      thread.start(); 
     } 
    } 
}