写一个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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询