日期:2014-05-20 浏览次数:20953 次
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.IOException; public class Lb extends JFrame implements ActionListener{ JButton b1 = new JButton("open"); JButton b2 = new JButton("save"); JPanel p = new JPanel(); JTextArea jta = new JTextArea(); JScrollPane jsp = new JScrollPane(jta); JFileChooser jfc = new JFileChooser(); public Lb() { this.add(jsp); this.add(p,BorderLayout.NORTH); p.add(b1); p.add(b2); b1.addActionListener(this); b2.addActionListener(this); this.setSize(320,240); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e){ String s = e.getActionCommand(); if(s.equals("save")){ save(); }else if(s.equals("open")){ open(); } } public void open(){ int n = jfc.showOpenDialog(this); String s,text=""; if(n==0){ File f = jfc.getSelectedFile(); try{ FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); while((s=br.readLine())!=null){ text+=s+"\n"; } jta.setText(text); br.close(); fr.close(); }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } }else { JOptionPane.showMessageDialog(this,"choose a file please.","系统提示",JOptionPane.INFORMATION_MESSAGE); } } public void save(){ if(jfc.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){ File f = jfc.getSelectedFile(); try{ FileWriter fr = new FileWriter(f); fr.write(jta.getText().replaceAll("\n","\r\n")); fr.close(); }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } }else{ JOptionPane.showMessageDialog(this,"choose a file please."); } } public static void main(String[] args) { new Lb(); } }
------解决方案--------------------
这个应该对了!~~不好意思!~
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; import com.microsoft.jdbc.sqlserver.SQLServerDriver; public class Phone{ public static void main(String[] args) { new Dis(); } } class Dis extends JFrame implements ActionListener{ JTable t; JScrollPane js; JButton b = new JButton("添加"); JPanel p = new JPanel(); String[] dp = {"编号","姓名","性别","生日","电话","住址"}; String[][] dc; public Dis(){ this.setTitle("通讯录"); this.setLocation(200,200); this.add(p,BorderLayout.SOUTH); p.add(b); b.addActionListener(this); open(); } public void actionPerformed(ActionEvent e){ new Add(this); } public void open(){ try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection con = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;databasename=Ph", "sa", ""); Statement sta = con.createStatement(1005,1008); String sql = "select * from Tph"; ResultSet res = sta.executeQuery(sql); res.last(); int n = res.getRow(); dc = new String[n][6]; res.first(); for(int i=0;i<n;i++){ for(int j=0;j<6;j++){ dc[i][j]=res.getString(j+1); } res.next(); } res.close(); sta.close(); t = new JTable(dc,dp); js = new JScrollPane(t); this.add(js); this.setSize(480,360); this.setDefaultCloseOperation(3); this.setVisible(true); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } } } class Add extends JFrame implements ActionListener{ JPanel[] p = new JPanel[6]; JLabel[] l = new JLabel[5]; JTextField[] f = new JTextField[5]; String[] s ={"姓名:","性别:","生日:","电话:","住址:"}; JPanel p1 = new JPanel(new GridLayout(6,1)); JButton b1 = new JButton("确定"); JButton b2 = new JButton("取消"); Dis z; public Add(Dis z){ this.z=z; this.setTitle("添加朋友"); this.setLocation(200,100); this.add(p1); b1.addActionListener(this); b2.addActionListener(this); for(int i=0;i<6;i++){ p[i] = new JPanel(); p1.add(p[i]); } for(int i=0;i<5;i++){ l[i]=new JLabel(s[i]); f[i]=new JTextField(10); } for(int i=0;i<5;i++){ p[i].add(l[i]); p[i].add(f[i]); } p[5].add(b1); p[5].add(b2); this.setSize(400,600); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e){ Object obj = e.getSource(); if(obj==b1){ tj(); this.dispose(); }else if(obj==b2){ this.dispose(); } } public void tj(){ try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection con = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;databasename=Ph", "sa", ""); Statement sta = con.createStatement(1005,1008); String s1 = f[0].getText(); String s2 = f[1].getText(); String s3 = f[2].getText(); String s4 = f[3].getText(); String s5 = f[4].getText(); String sql = "insert into Tph(sname,ssex,sbr,sph,sds) values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"')"; sta.executeUpdate(sql); sta.close(); z.remove(z.js); z.open(); }catch(ClassNotFoundException ex){ ex.printStackTrace(); }catch(SQLException ex){ ex.printStackTrace(); } } }