0
嗨我無法接受用戶輸入並使其收到用戶輸入。 然後接受用戶輸入並使用它來創建新的文本空白文本文件。 我可以得到它的工作,但是當我使用JTextField它不會創建該文件。使用JTextField創建一個新文件
任何幫助將不勝感激。
這是我的代碼:
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.util.Scanner;
public class newGame extends JFrame {
private JButton reg;
private JTextField userName;
private JTextField info;
Scanner input = new Scanner(System.in);
public newGame() {
super ("Rock Paper Scissors");
//creates the text fields
info = new JTextField ("Welcome to the rock, Please enter your username below");
info.setEditable(false);
JTextField userName = new JTextField ("name");
//impliments actionlistner
newClass saver = new newClass();
userName.addActionListener(saver);
//adds the fields to the Content Layout
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(info, BorderLayout.NORTH);
content.add(userName, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(content);
setTitle("Rock Paper Scissors The Game");
pack();
}
private class newClass implements ActionListener {
public void actionPerformed (ActionEvent event) {
String newUserName = userName.getText();
File file = new File(newUserName + ".txt");
boolean blnCreated = false;
try {
blnCreated = file.createNewFile();
} catch(IOException ioe) {
}
JOptionPane.showMessageDialog
(null,String.format("%s",event.getActionCommand()));
}
}
}
非常感謝你,幫助!當地的影子。 –