我将文本字段的内容写入文本文件:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;
import java.io.FileNotFoundException;
import java.util.Formatter;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Payroll extends JFrame implements ActionListener{
private JButton btn1;
private JButton btn2;
private JButton btnadd;
// initialize the lbl with caption name is employee information.
JLabel lbl = new JLabel("Nilai University Payroll System");
public Payroll(){
super("Nilai University Payroll System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
JLabel lblid,lblname,lblrank,lblclass,lbltype,lblpay;
JTextField txtid,txtname,txtrank,txtclass,txtEmployeeClass,txtEmployeeType;
JRadioButton rb1,rb2,rb3,rb4,rb5;
lbl.setBounds(200,50,500,100);
lbl.setHorizontalAlignment(lbl.CENTER );
lblid = new JLabel("Employee ID: ");
lblname = new JLabel("Employee Name: ");
lblrank = new JLabel("Employee Rank: ");
lblclass = new JLabel("Employee Class: ");
lbltype = new JLabel("Class Type: ");
lblpay = new JLabel("Employee Pay Rate: ");
// initialize all the label which are declared in the example above with its caption name
lblid.setBounds(300,140,100,20);
lblname.setBounds(300,180,100,20);
lblrank.setBounds(300,220,100,20);
lblclass.setBounds(300,260,100,20);
lbltype.setBounds(300,300,100,20);
lblpay.setBounds(300,340,100,20);
// add all the label on the frame
add(lblid);
add(lblname);
add(lblrank);
add(lblclass);
add(lbltype);
add(lblpay);
// initialize the text field with size
txtid=new JTextField(15);
txtname=new JTextField(15);
txtclass=new JTextField(15);
txtEmployeeClass=new JTextField(15);
txtEmployeeType=new JTextField(15);
// set a particular position on a screen with set bounds constructor
txtid.setBounds(400,140,100,20);
txtname.setBounds(400,180,100,20);
txtclass.setBounds(400,220,100,20);
txtEmployeeClass.setBounds(400,260,100,20);
txtEmployeeType.setBounds(400,300,100,20);
// add text field on a Frame
add(txtid);
add(txtname);
add(txtclass);
add(txtEmployeeClass);
add(txtEmployeeClass);
// initialize radio button with its caption
rb1 = new JRadioButton("1. Excellent");
rb2 = new JRadioButton("2. Good");
rb3 = new JRadioButton("3. Average");
rb4 = new JRadioButton("4. Fair");
rb5 = new JRadioButton("5. Poor");
// set a particular position on a Frame
rb1.setBounds(400,220,100,20);
rb2.setBounds(500,220,100,20);
rb3.setBounds(600,220,100,20);
rb4.setBounds(700,220,100,20);
rb5.setBounds(800,220,100,20);
// add button on a frame
add(rb1);
add(rb2);
add(rb3);
add(rb4);
add(rb5);
btnadd = new JButton("Add Employee");
btnadd.setToolTipText("Click this button to add employee details to a text file.");
btnadd.setBounds(400,320,150,20);
add(btnadd);
btnadd.addActionListener(this);
btnadd.setActionCommand("Add");
}
private BufferedWriter output;
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("Add"))
{
output.write("ID: "+txtid.getNume()+"\n");
}
}
public void WriteFile(){
try {
output = new BufferedWriter(new FileWriter("E:/EC3307/eclipse/Payroll.txt",true));
output.newLine();
output.close();
}
catch(IOException e)
{
System.out.println("There was a problem:" + e);
}
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run()
{
Payroll f1=new Payroll();
// set frame size
f1.setSize(1000,600);
// set frame visible true
f1.setVisible(true);
}
});
}
}