初学者在这里编码。使用java jgrasp中的GUI,如何在每次提交时提交一行代码并继续添加到文本文件中。例如,在GUI中,您可以添加一些随机答案。这可能包括日期,食物或酱汁。这是它在提交时打印出来的内容。
1. 2/2/2017 hamburger ketchup
2. 2/3/2017 fish tartar sauce
3. 6/3/2018 steak A1
目前,我只能提交一行代码。在另一次提交后,该文件重写了代码上的另一个答案。但是,我需要java jgrasp程序来保存并允许更多答案。它需要看起来像下面显示的代码,每个数字代表用户在GUI中按下提交的一次。
<pre><code>
// Creates form for gathering data and loads it into a file
// This list shows the functions used in this program
//there are many Class files used. I have listed them all FYI
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
//this makes this class file is made a subclass of JFrame and uses the ActionListener Interface
public class QuestionForm extends JFrame implements ActionListener
{
// these are class level variables for use by the entire class file
public static final int WIDTH = 700;
public static final int HEIGHT = 500;
//this sets up a constant char field size the text fields
public static final int FIELDWIDTH = 10;
// these private instance variables are defined here to be used by the GUI object form
private JTextField firstNameField;
private JTextField lastNameField;
private JTextField phoneNumField;
private JTextField dressNumField;
private JTextField dressStreetField;
private JLabel label6; // used for status messages on panel 6
private JLabel label5; // used for directions on panel 5
// menu action listener inner classes to establish each menu selection response
private class humanListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
dressStreetField.setText("humanoid");
}
} // end human listerner inner class
private class alienListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
dressStreetField.setText("Klingon");
}
} // end alien listerner inner class
// class constructor
public QuestionForm()
{
super(); // default constructor calls to the super JFrame class intially
setSize(WIDTH, HEIGHT); // these can also be sent from main as an option
setTitle("Questionaire Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// sets form grid to divide frame into multiple parts for panels to snap to
setLayout(new GridLayout(6, 1)); //fixed size for this form object class
// create first panel of text fields and labels
JPanel panel1 = new JPanel(); // panel object
panel1.setBackground(Color.PINK); //panel color
JLabel label1 = new JLabel("First Name"); // panel label
panel1.add(label1);
firstNameField = new JTextField(FIELDWIDTH); // panel text field
panel1.add(firstNameField);
add(panel1);
// create second panel of text fields and labels
JPanel panel2 = new JPanel();
panel2.setBackground(Color.YELLOW);
JLabel label2 = new JLabel("Last Name");
panel2.add(label2);
lastNameField = new JTextField(FIELDWIDTH);
panel2.add(lastNameField);
add(panel2);
// create third panel of text fields and labels
JPanel panel3 = new JPanel();
JLabel label3 = new JLabel("phone number (no dashes please)");
panel3.add(label3);
phoneNumField = new JTextField(FIELDWIDTH);
panel3.add(phoneNumField);
add(panel3);
// create fourth panel of text fields and labels
JPanel panel4 = new JPanel();
panel4.setBackground(Color.WHITE);
JLabel label4 = new JLabel("address house number");
panel4.add(label4);
dressNumField = new JTextField(FIELDWIDTH);
panel4.add(dressNumField);
add(panel4);
// create loaded fifth panel with text field label and drop down menu
JPanel panel5 = new JPanel();
JLabel label5 = new JLabel("select from menu");
panel5.add(label5);
dressStreetField = new JTextField(FIELDWIDTH);
panel5.add(dressStreetField);
// create and populate menu for panel 5
JMenuBar bar = new JMenuBar();
bar.setBackground(Color.CYAN);
JMenu select = new JMenu("species");
JMenuItem humanchoice = new JMenuItem("humanoid");
humanchoice.addActionListener(new humanListener()); // defines what happens when menu choice selected...see private inner class above
select.add(humanchoice);
JMenuItem alienchoice = new JMenuItem("Klingon");
alienchoice.addActionListener(new alienListener());
//alienchoice.setActionCommand(label5.setText("Klingon"));
select.add(alienchoice);
bar.add(select);
panel5.add(bar);
add(panel5); //fully loaded panel now snapped to frame
// create submit button panel6 with status indicator label
JPanel subpanel = new JPanel();
subpanel.setBackground(Color.CYAN);
JButton submit = new JButton("Submit");
submit.addActionListener(this); //programs the button action in method call below
label6 = new JLabel("Please fill in form, then press Submit");
subpanel.add(submit);
subpanel.add(label6);
add(subpanel);
} // end Frame building method
// submit button action defined in this public method
public void actionPerformed(ActionEvent e)
{
PrintWriter outputStream = null; // defines Writer object for file loading
String fileName = lastNameField.getText(); //creates file name from lastName on form
// This is an exception catching method for handling potential file opening errors
try
{
outputStream = new PrintWriter(new FileOutputStream(fileName)); // opens file on drive, waits for data
}
catch(FileNotFoundException f) // terminates program is file fails to open--all data lost
{
System.out.println("error opening the data store file");
System.exit(0);
}
outputStream.println(firstNameField.getText() + " " + lastNameField.getText() + " " + phoneNumField.getText() + " " + dressNumField.getText() + " " + dressStreetField.getText()); //defines text file format -- please note: all inputted data are Strings
outputStream.close(); //data store file written and closed
label6.setText("file saved...End of program"); //updates status on panel 6
} // end action method
} // end class
</code></pre>
这些代码将在java jgrasp中创建一个GUI。然后您可以输入代码,但是,您只能编写一个代码。每个其他输入的提交将重写原始提交。如何解决问题并使代码添加超出1行提交的输入?
这是有问题的代码:
<pre><code>
import java.awt.GridLayout;
import javax.swing.JFrame;
public class QuestionFormTester
{
public static void main(String[] args)
{
QuestionForm form = new QuestionForm();
form.setVisible(true);
} //endmain
} // end class
</code></pre>
id
答案 0 :(得分:0)
outputStream.println(firstNameField.getText() + " " + lastNameField.getText() + " " + phoneNumField.getText() + " " + dressNumField.getText() + " " + dressStreetField.getText());
只需将actionPerformed()函数的上一行替换为以下行
即可outputStream.append(firstNameField.getText() + " " + lastNameField.getText() + " " + phoneNumField.getText() + " " + dressNumField.getText() + " " + dressStreetField.getText());
使用append()函数代替println()。