【发布时间】:2020-06-23 05:26:56
【问题描述】:
我现在正在学习 java,我想创建一个简单的程序来获取我的文本并将其放入 txt 文件中。这是我的代码,但没有任何效果,它会创建 txt 文件,但不会从文本字段输入实际文本。
public static void main(String[] args) {
File file = new File("information.txt");
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
JLabel user = new JLabel("user");
user.setBounds(10, 25, 100, 25);
panel.add(user);
JTextField username = new JTextField();
username.setBounds(100, 25, 300, 25);
panel.add(username);
JButton button = new JButton("login");
button.setBounds(185, 150, 100, 50);
panel.add(button);
String name = username.getText();
panel.setLayout(null);
frame.setVisible(true);
try {
if (button.getModel().isPressed()){
file.createNewFile();
}
PrintWriter pw = new PrintWriter(file);
pw.println(name);
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
}
【问题讨论】:
标签: java windows text build printwriter