如何从GUI输出文本到文件? [英] How do I output text to a file from a GUI?
本文介绍了如何从GUI输出文本到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我正在编写一个java类,并且大部分都在使用它。问题是当我运行它,输入数据,然后单击保存时,它应该将其保存到名为client.txt的文件中。虽然它确实为文件添加了记录。这远非正确。请让我知道我哪里出错了。谢谢
Hello I am writing a java class and have it working for the most part. The problem is when I run it, enter data, and click save it is suppose to save it to a file called client.txt. While it does add records to the file. It is far from correct. Please let me know where I am going wrong. Thanks
package clientinfo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.swing.JTextField;
import javax.swing.JButton;
public class ClientInfo extends JFrame implements ActionListener
{
String content = "";
String Name = "";
String Sbalance = "";
String ID = "";
String Cbalance = "";
BufferedWriter bw;
public ClientInfo()
{
super("Client Information");
getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
JLabel lblName = new JLabel("Client Name:");
JTextField txtName = new JTextField(16);
add(lblName); add(txtName);
Name = txtName.toString();
JLabel lblSBalance = new JLabel("Starting Balance:");
JTextField txtSBalance = new JTextField(16);
add(lblSBalance); add(txtSBalance);
Sbalance = txtSBalance.toString();
JLabel lblID = new JLabel("Client ID:");
JTextField txtID = new JTextField(16);
add(lblID); add(txtID);
ID = txtID.toString();
JLabel lblCBalance = new JLabel("Closing Balance:");
JTextField txtCBalance = new JTextField(16);
add(lblCBalance); add(txtCBalance);
Cbalance = txtCBalance.toString();
JButton btnSave = new JButton("Save");
add(btnSave);
btnSave.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
try
{
bw = new BufferedWriter(new FileWriter("client.txt",true));
bw.newLine();
content = ("" + Name + " " + Sbalance + " " + ID + "" + Cbalance);
bw.write(content);
bw.close();
}
catch(IOException ex)
{
System.err.println("Output Error -- " + ex.toString());
}
Container c = getContentPane();
c.setLayout(new GridLayout(3,2,20,20));
}
public static void main(String[] args)
{
ClientInfo c = new ClientInfo();
c.setVisible(true);
c.setSize(565,150);
}
}
Example of the current output:
javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@34cd072c,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=16,columnWidth=0,command=,horizontalAlignment=LEADING] javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@34cd072c,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=16,columnWidth=0,command=,horizontalAlignment=LEADING] javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@34cd072c,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=16,columnWidth=0,command=,horizontalAlignment=LEADING]javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@34cd072c,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=16,columnWidth=0,command=,horizontalAlignment=LEADING]
推荐答案
UpdateHandler,alignmentX = 0。 0 ,alignmentY = 0. 0 ,border = javax.swing.plaf.BorderUIResource
UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource
CompoundBorderUIResource @ 34cd072c,旗帜= 296,MAXIMUMSIZE =,=的minimumSize,首选大小=,caretColor = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],disabledTextColor = javax.swing.plaf.ColorUIResource [R = 184 ,G = 207,b = 229],可编辑=真,裕度= javax.swing.plaf.InsetsUIResource [顶= 0,左= 0,底部= 0,右= 0],selectedTextColor = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],selectionColor设置= javax.swing.plaf.ColorUIResource [R = 184,G = 207,b = 229 ],columns = 16,columnWidth = 0,command =,horizontalAlignment = LEADING] javax.swing.JTextField [, 0 , 0 ,0x0,无效,layout = javax.swing.plaf.basic.BasicTextUI
CompoundBorderUIResource@34cd072c,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=16,columnWidth=0,command=,horizontalAlignment=LEADING] javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI
UpdateHandler,alignmentX = 0. 0 ,alignmentY=0. 0 ,border = javax.swing.plaf.BorderUIResource
UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource
这篇关于如何从GUI输出文本到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文