求JAVA源代码,要有注释,所有财富都在下面了
1个回答
展开全部
每天有时间的话 , 会回答一两个图形界面的问题, 但是分数最好还是高点才有兴趣.
具体代码和详细的注释如下
员工类
public class Emp {
private int num;//工号
private String name;//姓名
private double basicPay;//基本工资
private double meritPay;//绩效工资
public Emp(){//无参数构造器
}
public Emp(int num, String name, double basicPay, double meritPay) {//有参数构造器
super();
this.num = num;
this.name = name;
this.basicPay = basicPay;
this.meritPay = meritPay;
}
//重写Object的toString 方法
public String toString() {
return "工号:"+num+"\t姓名:"+name+"\t基本工资:"+basicPay+"\t绩效工资"+meritPay;
}
//下面是属性的set和get
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasicPay() {
return basicPay;
}
public void setBasicPay(double basicPay) {
this.basicPay = basicPay;
}
public double getMeritPay() {
return meritPay;
}
public void setMeritPay(double meritPay) {
this.meritPay = meritPay;
}
}
输入界面类
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class EmpFrome extends JFrame implements ActionListener {
JTextField jtfnum, jtfname, jtfbp, jtfmp;
JButton jbwtf;
public EmpFrome() {
JLabel jl1 = new JLabel("工号");
jtfnum = new JTextField(8);
add(jl1);
add(jtfnum);
JLabel jl2 = new JLabel("姓名");
jtfname = new JTextField(8);
add(jl2);
add(jtfname);
JLabel jl3 = new JLabel("基本工资");
jtfbp = new JTextField(8);
add(jl3);
add(jtfbp);
JLabel jl4 = new JLabel("绩效工资");
jtfmp = new JTextField(8);
add(jl4);
add(jtfmp);
JLabel jl5 = new JLabel();
jbwtf = new JButton("写入文件");
jbwtf.addActionListener(this);
add(jl5);
add(jbwtf);
setLayout(new GridLayout(5, 2));
setTitle("员工信息录入");
setSize(290, 230);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("写入文件")) {
try{
//获取数据
int num = Integer.parseInt(jtfnum.getText().trim());
String name = jtfname.getText().trim();
double bp = Double.parseDouble(jtfbp.getText().trim());
double mp = Double.parseDouble(jtfmp.getText().trim());
Emp emp = new Emp(num, name, bp, mp);
writeToFile(emp);
JOptionPane.showMessageDialog(this, "录入成功");//提示成功
//清空文本框
jtfnum.setText("");
jtfname.setText("");
jtfbp.setText("");
jtfmp.setText("");
}catch(Exception ex){
//当输入不符合规范时 , 提示错误
JOptionPane.showMessageDialog(this, "请输入正确的数据:\n工号整型,工资浮点型","录入错误",JOptionPane.ERROR_MESSAGE);
}
}
}
//定义的文件路径
final static String FILE_PATH = "employee.dat";
public void writeToFile(Emp emp) {//IO操作,追加写入
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(FILE_PATH), true));//为true表示追加
bw.write(emp.toString());//写入员工信息
bw.newLine();//换行
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
测试类
public class EmpTest {
public static void main(String[] args) {
new EmpFrome();
}
}
测试效果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询