所以,我必須使用Textfield
採取從GUI中的字符串輸入,並使其在PDF文檔中顯示出來,當我使用ActionListener
:Java的:任何方式的字符串輸入保存成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);
}
}
}
}
您是否遇到任何問題?你可以使用外部庫作爲[itext](http://itextpdf.com/)? –
嘗試調試並查看tf1 [0] .getText()是否實際從Textfield獲取數據。 – optimus
這可能會幫助你:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ –