如何编写一个带有输入文本框,点击按钮之后输出到文件中的java GUI界面?
展开全部
给你写个简单的,参考一下吧
package uni;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Guiwriter extends JFrame
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JLabel l = new JLabel("请输入内容:");
JTextField f = new JTextField();
JButton b = new JButton("执行");
public Guiwriter() //构造方法
{
this.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(3, 2));
p1.add(l);
p1.add(f);
this.add(p1, BorderLayout.NORTH);
p2.add(b, BorderLayout.CENTER);
this.add(p2, BorderLayout.CENTER);
this.setSize(200, 200);
this.setVisible(true);
b.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str =f.getText();
run(str);
}
});
}
public void run(String str){
String rtnFile1 = "d:/OK.txt";//输出文件路径
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(rtnFile1));
String str1 = str ;
bw.write(str1);
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main (String args[]){
new Guiwriter();
}
}
package uni;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Guiwriter extends JFrame
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JLabel l = new JLabel("请输入内容:");
JTextField f = new JTextField();
JButton b = new JButton("执行");
public Guiwriter() //构造方法
{
this.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(3, 2));
p1.add(l);
p1.add(f);
this.add(p1, BorderLayout.NORTH);
p2.add(b, BorderLayout.CENTER);
this.add(p2, BorderLayout.CENTER);
this.setSize(200, 200);
this.setVisible(true);
b.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str =f.getText();
run(str);
}
});
}
public void run(String str){
String rtnFile1 = "d:/OK.txt";//输出文件路径
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(rtnFile1));
String str1 = str ;
bw.write(str1);
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main (String args[]){
new Guiwriter();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询