有一个提交按钮 输入一个人名 点 提交 就会建立一个该人名的txt
例如 :输入 张翰 提交后 生成一个 张翰.txt
请问怎么实现?
java 每输入一个人就建立以人名为名称的txt
答案:3 悬赏:80 手机版
解决时间 2021-03-11 17:45
- 提问者网友:皆是孤独
- 2021-03-10 19:52
最佳答案
- 五星知识达人网友:佘樂
- 2021-03-10 21:06
带简单的Swing代码。在D盘下创建一个输入信息为名的TXT文件
package cn.com.test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test {
private static String path = "D:\\";
public static void main(String[] args) throws Exception {
getInterface();
}
private static void getInterface(){
final JFrame frame = new JFrame();
JLabel label = new JLabel("请输入人名");
final JTextField input = new JTextField(20);
JButton btn = new JButton("确定");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = input.getText();
String filePath = path+name+".txt";
try {
boolean flag = createFileByName(filePath);
if (flag) {
JOptionPane.showMessageDialog(frame, "Create " + filePath + " Success ! ");
} else {
JOptionPane.showMessageDialog(frame, "Create " + filePath + " Failed ! ");
}
} catch (IOException e1) {
JOptionPane.showMessageDialog(frame, "Create " + filePath + " Failed. \r\n " + e1.getMessage());
e1.printStackTrace();
}
}
});
JPanel panel = new JPanel();
panel.add(label);
panel.add(input);
panel.add(btn);
frame.add(panel);
frame.setBounds(300, 200, 300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static boolean createFileByName(String path) throws IOException{
File file = new File(path);
if (file.exists()) {
file.delete();
}
return file.createNewFile();
}
}
package cn.com.test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test {
private static String path = "D:\\";
public static void main(String[] args) throws Exception {
getInterface();
}
private static void getInterface(){
final JFrame frame = new JFrame();
JLabel label = new JLabel("请输入人名");
final JTextField input = new JTextField(20);
JButton btn = new JButton("确定");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = input.getText();
String filePath = path+name+".txt";
try {
boolean flag = createFileByName(filePath);
if (flag) {
JOptionPane.showMessageDialog(frame, "Create " + filePath + " Success ! ");
} else {
JOptionPane.showMessageDialog(frame, "Create " + filePath + " Failed ! ");
}
} catch (IOException e1) {
JOptionPane.showMessageDialog(frame, "Create " + filePath + " Failed. \r\n " + e1.getMessage());
e1.printStackTrace();
}
}
});
JPanel panel = new JPanel();
panel.add(label);
panel.add(input);
panel.add(btn);
frame.add(panel);
frame.setBounds(300, 200, 300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static boolean createFileByName(String path) throws IOException{
File file = new File(path);
if (file.exists()) {
file.delete();
}
return file.createNewFile();
}
}
全部回答
- 1楼网友:詩光轨車
- 2021-03-10 23:05
这个功能简单啊,如果你不急的话,我晚上回去帮你写一个!
- 2楼网友:执傲
- 2021-03-10 22:31
你好!
String name = "张三";
File file=new File("f:/"+name+".txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
至于你那个名字是怎么传过来的就不用我写了吧
仅代表个人观点,不喜勿喷,谢谢。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯