我目前正在使用Swing和awt用GUI编码hangman。目前,我想将输出保存到文本文件中。但是,当我尝试将内容保存在文本文件中时,代码只会删除文本文件的内容,而不会在文本文件中写入任何内容。另外,如果我在方法[Tester()]的开头不包含[throw FileNotFoundException],则代码也不会运行。是否有人有解决此问题的见识?我对Java还是很陌生,我还不了解非常复杂的命令等。如果可以的话,请尝试使其保持简单。
我的最终目标是将已经按下的字母(在gui上的按钮)保存到outputString中,以便下次按下按钮时将输出所按下字母的所有实例以及之前所按下的字母。
我想要代码做什么: 如果我按下按钮E:
_e ___ ____e E
如果在以下情况下按下按钮W:
_e ___ W____ E
代码的作用:
如果我按下按钮E:
_e ___ ____ E
如果在以下情况下按下按钮W:
_____ W_____ _
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class Tester extends JFrame implements ActionListener {
File myFile = new File ("outputString.txt");
PrintWriter printOut = new PrintWriter (myFile);
JButton okButton = new JButton ("OK");
JButton buttonA = new JButton("a");
JButton buttonB = new JButton("b");
JButton buttonC = new JButton("c");
JButton buttonD = new JButton("d");
JButton buttonE = new JButton("e");
JButton buttonF = new JButton("f");
JButton buttonG = new JButton("g");
JButton buttonH = new JButton("h");
JButton buttonI = new JButton("i");
JButton buttonJ = new JButton("j");
JButton buttonK = new JButton("k");
JButton buttonL = new JButton("l");
JButton buttonM = new JButton("m");
JButton buttonN = new JButton("n");
JButton buttonO = new JButton("o");
JButton buttonP = new JButton("p");
JButton buttonQ = new JButton("q");
JButton buttonR = new JButton("r");
JButton buttonS = new JButton("s");
JButton buttonT = new JButton("t");
JButton buttonU = new JButton("u");
JButton buttonV = new JButton("v");
JButton buttonW = new JButton("w");
JButton buttonX = new JButton("x");
JButton buttonY = new JButton("y");
JButton buttonZ = new JButton("z");
JPanel panIntro = new JPanel();
JPanel pan1 = new JPanel(); //define new panels
JPanel panMain = new JPanel();
JLabel name = new JLabel("Name: ");
JTextField nameField = new JTextField(" ",10);
JButton nextButton = new JButton("Next");
/** Creates, adds, and outputs Frames, Panels, and Buttons to the GUI program
* @return the GUI with questions
*/
public Tester() throws FileNotFoundException {//start of meathod
File myFile = new File ("outputString.txt");
PrintWriter printOut = new PrintWriter (myFile);
setTitle ("GUI"); //constructor puts everything together
setSize (900, 400); //set up the frame size
setResizable(false);//set the window to not to be resizable
GridLayout grid1 = new GridLayout(); // setting the frame into
setLayout (grid1);
BoxLayout box0 = new BoxLayout(panMain,BoxLayout.Y_AXIS);//set frame as box layout
panMain.setLayout(box0);
FlowLayout flow1 = new FlowLayout(); //adding what to include in each panel
pan1.setLayout (flow1);
pan1.add(buttonA);
pan1.add(buttonB);
pan1.add(buttonC);
pan1.add(buttonD);
pan1.add(buttonE);
pan1.add(buttonF);
pan1.add(buttonG);
pan1.add(buttonH);
pan1.add(buttonI);
pan1.add(buttonJ);
pan1.add(buttonK);
pan1.add(buttonL);
pan1.add(buttonM);
pan1.add(buttonN);
pan1.add(buttonO);
pan1.add(buttonP);
pan1.add(buttonQ);
pan1.add(buttonR);
pan1.add(buttonS);
pan1.add(buttonT);
pan1.add(buttonU);
pan1.add(buttonV);
pan1.add(buttonW);
pan1.add(buttonX);
pan1.add(buttonY);
pan1.add(buttonZ);
buttonA.addActionListener(this);
buttonB.addActionListener(this);
buttonC.addActionListener(this);
buttonD.addActionListener(this);
buttonE.addActionListener(this);
buttonF.addActionListener(this);
buttonG.addActionListener(this);
buttonH.addActionListener(this);
buttonI.addActionListener(this);
buttonJ.addActionListener(this);
buttonK.addActionListener(this);
buttonL.addActionListener(this);
buttonM.addActionListener(this);
buttonN.addActionListener(this);
buttonO.addActionListener(this);
buttonP.addActionListener(this);
buttonQ.addActionListener(this);
buttonR.addActionListener(this);
buttonS.addActionListener(this);
buttonT.addActionListener(this);
buttonU.addActionListener(this);
buttonV.addActionListener(this);
buttonW.addActionListener(this);
buttonX.addActionListener(this);
buttonY.addActionListener(this);
buttonZ.addActionListener(this);
JLabel guess = new JLabel("Vigilante Hackers are called: ", JLabel.LEFT);
panMain.add(pan1);
add(panMain);
pan1.setVisible(true);
setVisible(true);
}//end of meathod
public void actionPerformed(ActionEvent event){//start of actionPerfomed meathod
String command = event.getActionCommand(); //get button action command
char guessChar = 'o';
if (command.equals("a")){//next button triggered
String name = nameField.getText();
guessChar = 'a';
}//end of if
else if (command.equals("b")){//next button triggered
String name = nameField.getText();
guessChar = 'b';
}//end of if
else if (command.equals("c")){//next button triggered
String name = nameField.getText();
guessChar = 'c';
}//end of if
else if (command.equals("d")){//next button triggered
String name = nameField.getText();
guessChar = 'd';
}//end of if
else if (command.equals("e")){//next button triggered
String name = nameField.getText();
guessChar = 'e';
}//end of if
else if (command.equals("f")){//next button triggered
String name = nameField.getText();
guessChar = 'f';
}//end of if
else if (command.equals("g")){//next button triggered
String name = nameField.getText();
guessChar = 'g';
}//end of if
else if (command.equals("h")){//next button triggered
String name = nameField.getText();
guessChar = 'h';
}//end of if
else if (command.equals("i")){//next button triggered
String name = nameField.getText();
guessChar = 'i';
}//end of if
else if (command.equals("j")){//next button triggered
String name = nameField.getText();
guessChar = 'j';
}//end of if
else if (command.equals("k")){//next button triggered
String name = nameField.getText();
guessChar = 'k';
}//end of if
else if (command.equals("l")){//next button triggered
String name = nameField.getText();
guessChar = 'l';
}//end of if
else if (command.equals("m")){//next button triggered
String name = nameField.getText();
guessChar = 'm';
}//end of if
else if (command.equals("n")){//next button triggered
String name = nameField.getText();
guessChar = 'n';
}//end of if
else if (command.equals("o")){//next button triggered
String name = nameField.getText();
guessChar = 'o';
}//end of if
else if (command.equals("p")){//next button triggered
String name = nameField.getText();
guessChar = 'p';
}//end of if
else if (command.equals("q")){//next button triggered
String name = nameField.getText();
guessChar = 'q';
}//end of if
else if (command.equals("r")){//next button triggered
String name = nameField.getText();
guessChar = 'r';
}//end of if
else if (command.equals("s")){//next button triggered
String name = nameField.getText();
guessChar = 's';
}//end of if
else if (command.equals("t")){//next button triggered
String name = nameField.getText();
guessChar = 't';
}//end of if
else if (command.equals("u")){//next button triggered
String name = nameField.getText();
guessChar = 'u';
}//end of if
else if (command.equals("v")){//next button triggered
String name = nameField.getText();
guessChar = 'v';
}//end of if
else if (command.equals("w")){//next button triggered
String name = nameField.getText();
guessChar = 'w';
}//end of if
else if (command.equals("x")){//next button triggered
String name = nameField.getText();
guessChar = 'x';
}//end of if
else if (command.equals("y")){//next button triggered
String name = nameField.getText();
guessChar = 'y';
}//end of if
else if (command.equals("z")){//next button triggered
String name = nameField.getText();
guessChar = 'z';
}//end of if
String phrase = "Hello World E";
String phraseLowercase = "";
phraseLowercase = phrase.toLowerCase();
String outputString = "";
for (int counter = 0; counter < phrase.length(); counter++){
char index = phraseLowercase.charAt(counter);
if (index == guessChar){
outputString += phrase.charAt(counter);
}
else if (index == ' '){
outputString += " ";
}
else if (index != guessChar){
outputString += "_";
}
}
System.out.println(outputString);
printOut.println(outputString);
}//end of meathod
public static void main (String[] args) throws FileNotFoundException //main method to activate GUI
{
new Tester();//calls QuizGUI meathod
}//end of main
}//end of class