2016-07-22 16 views
0

所以,我必須使用Textfield採取從GUI中的字符串輸入,並使其在PDF文檔中顯示出來,當我使用ActionListenerJava的:任何方式的字符串輸入保存成PDF

b1[0].addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent arg0) 
{ 
    s=tf1[0].getText(); 
} 
}); 

當我運行gui並鍵入tf1並按b1,它應該將它保存在正確的位置?但它沒有這樣做,但是如果我只是硬編碼s="string"它顯示在PDF中。有任何想法嗎?

忘了提我使用iText的,所以我有唯一的問題是,如果你想將String s沒有充分利用TextField

還在爲actionlistener類的代碼保存它:

public class GUI { 
public static String s; 
public static void gui(){ 

    { 
     try{ 
     String File_Name="C:/Users/Ray/Desktop/test.txt"; 
     ReadFile rf=new ReadFile(File_Name); 
     JFrame f1=new JFrame("Maintest"); 
     JPanel p1=new JPanel(); 
     JPanel p2=new JPanel(); 
     final String[] aryLines=rf.OpenFile(); 
     final JTextField tf1[]=new JTextField[22]; 
     JButton []b1=new JButton[6]; 
     String bNames="OK"; 
     final JTextField tf2[]=new JTextField[aryLines.length]; 
     f1.setSize(200,450); 


     JLabel l1[]=new JLabel[20]; 

     for (int i=0; i < aryLines.length; i++) 
     { 
      b1[i]=new JButton(bNames); 
      l1[i]=new JLabel("Enter Serial# for "+ aryLines[i]); 

      p1.add(l1[i]);p1.add(tf1[i] = new JTextField());p1.add(b1[i]); 
     } 


      p1.setLayout(new BoxLayout(p1,BoxLayout.PAGE_AXIS)); 

      f1.add(p1,BorderLayout.WEST); 



       b1[0].addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent arg0) 
       { 
        s=""+tf1[0].getText(); 
        System.out.println(s); 
       } 
       }); 

      f1.show(); 
     } 

      catch(Exception e) 
      { 
       System.out.print(e); 
      } 
    } 
} 
} 
+0

您是否遇到任何問題?你可以使用外部庫作爲[itext](http://itextpdf.com/)? –

+0

嘗試調試並查看tf1 [0] .getText()是否實際從Textfield獲取數據。 – optimus

+0

這可能會幫助你:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ –

回答

1

對不起,我不能更詳細地回答,但你可以檢查tf1[0].getText()是否工作。只需將System.out.println("Text: "tf1[0].getText());放入您的ActionListener即可。我很確定它會爲空或「Text:」。爲了獲得進一步的幫助,你必須提供更多的代碼。

Please take a look at this link

+0

保存我補充說,然後在gui文本框中添加「哈哈」,輸出是文本:哈哈,所以我想它是採取輸入,但爲什麼不是它然後將其保存在s – Commongrate

+0

這真的很奇怪。也許試試用String.valueOf(tf1 [0] .getText())。你能否提供更多的代碼?同時將valueOf添加到System.out.println()。 –

+0

我編輯我的帖子爲行動監察員代碼 – Commongrate

相關問題