当前位置:编程学习 > JAVA >>

有完整的JAVA编程嘛?

就是可以规定怎么输入的那种,几行就好
答案:

老弟,我花了点时间弄了下,你看行不!

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileWriter;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField;

public class 保存 extends JFrame implements ActionListener {
JButton btn_save,btn_del; JTextField id,name,age,sex,address,phone,email; JLabel id_lab,name_lab,age_lab,sex_lab,address_lab,phone_lab,email_lab; public 保存() { this.setSize(600,450); this.setLocation(200,170); //初始化组建 newCompnent(); //添加组建 addCompnent(); //添加事件 addListener(); this.setVisible(true); } private void addListener() { btn_save.addActionListener(this); btn_del.addActionListener(this); } private void addCompnent() { this.setLayout(null); this.add(id_lab); this.add(id); this.add(name_lab); this.add(name); this.add(age_lab); this.add(age); this.add(sex_lab); this.add(sex); this.add(address_lab); this.add(address); this.add(phone_lab); this.add(phone); this.add(email_lab); this.add(email); this.add(btn_save); this.add(btn_del); } private void newCompnent() { btn_save=new JButton("添加"); btn_save.setBounds(40, 250, 120, 40); btn_del=new JButton("删除按钮"); btn_del.setBounds(200, 250, 120, 40); id_lab=new JLabel("id"); id_lab.setBounds(40, 20, 50, 20); id=new JTextField(8); id.setBounds(85, 20, 120, 20); name_lab=new JLabel("name"); name_lab.setBounds(40, 45, 50, 20); name=new JTextField(20); name.setBounds(85, 45, 120, 20); age_lab=new JLabel("age"); age_lab.setBounds(40, 70, 50, 20); age=new JTextField(20); age.setBounds(85, 70, 50, 20); sex_lab=new JLabel("sex"); sex_lab.setBounds(40, 95, 50, 20); sex=new JTextField(20); sex.setBounds(85, 95, 50, 20); address_lab=new JLabel("address"); address_lab.setBounds(40, 120, 50, 20); address=new JTextField(20); address.setBounds(85, 120, 50, 20); phone_lab=new JLabel("phone"); phone_lab.setBounds(40, 145, 50, 20); phone=new JTextField(20); phone.setBounds(85, 145, 50, 20); email_lab=new JLabel("email"); email_lab.setBounds(40, 170, 50, 20); email=new JTextField(20); email.setBounds(85, 170, 50, 20); } public static void main(String[] args) { new 保存();
} public void actionPerformed(ActionEvent e) { if(e.getSource()==btn_save){ File file=new File("C:\\save.txt"); String st=id.getText().toString() "," name.getText().toString() "," age.getText().toString() "," sex.getText().toString() "," address.getText().toString() "," phone.getText().toString() "," email.getText().toString(); System.out.print(st); try { FileWriter out=new FileWriter(file); out.write(st); out.close(); } catch (Exception e1) { e1.printStackTrace(); } }else { id.setText(null); name.setText(null); age.setText(null); sex.setText(null); address.setText(null); phone.setText(null); email.setText(null); } }
}








你要输入什么,读取键盘输入的字符吗,

Scanner s = new Scanner(System.in);
String  i = s.next();
记得导入包

上一个:接口有什么用?JAVA问题
下一个:C++和Java各自的用途

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,