See More

package Win_SQL; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main_Frame extends JFrame{ private JFrame insert_frame; private JTextArea textarea; private int default_width=600; private int default_height=400; private JDialog init_frame; public Main_Frame(){ textarea=new JTextArea(10,20); textarea.setEditable(false); JScrollPane pane=new JScrollPane(textarea); add(pane,BorderLayout.CENTER); setTitle("VisualSQL"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setshow(Main_Frame.this,default_width,default_height); JMenuBar bar=new JMenuBar(); JMenu start_menu=new JMenu("¿ªÊ¼"); bar.add(start_menu); JMenu operate=new JMenu("²Ù×÷"); JMenuItem add=new JMenuItem("Ôö¼Ó"); add.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(insert_frame==null) { insert_frame=new MyFrame(); insert_frame.setLocation(200,300); insert_frame.setVisible(true); }else { } } }); JMenuItem delete=new JMenuItem("ɾ³ý"); JMenuItem modify=new JMenuItem("ÐÞ¸Ä"); JMenuItem query=new JMenuItem("²éѯ"); query.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // Statement statement=Service.createStatement(); // TODO Auto-generated method stub // Connection con=null; // try { // con=MyFrame.getConnection(); // String sql=JOptionPane.showInputDialog(Main_Frame.this, "ÊäÈë²éѯÓï¾ä"); // Statement state=con.createStatement(); // if (sql!="") { // ResultSet set=state.executeQuery(sql); // while (set.next()){ // Model hero=new Model(); // hero.setId(set.getInt("id")); // hero.setName(set.getString("name")); // hero.setAttack(set.getInt("gongji")); // hero.setDefend(set.getInt("fangyu")); // hero.setSex(set.getString("sex")); // hero.setCountry(set.getString("country")); // textarea.setText(hero.toString()+"\n"+textarea.getText()); // }} // textarea.setText(""); // } catch (SQLException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // }finally{ // try { // con.close(); // } catch (SQLException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } // } } } ); JMenuItem quit=new JMenuItem("Í˳ö"); JMenu connect=new JMenu("Á¬½Ó"); JMenuItem local=new JMenuItem("±¾µØ"); //local²Ëµ¥ÏîÌí¼Ó¼àÌýÆ÷ local.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String user=JOptionPane.showInputDialog(Main_Frame.this,"ÇëÊäÈëÓû§Ãû"); String passwd=JOptionPane.showInputDialog(Main_Frame.this,"ÇëÊäÈëÃÜÂë"); String db_name=JOptionPane.showInputDialog(Main_Frame.this,"ÇëÊäÈëÊý¾Ý¿âÃû"); System.out.println(user+" "+passwd+" "+db_name); File file=new File("./config.properties"); FileOutputStream fos=null; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } OutputStreamWriter osw=null; try { osw=new OutputStreamWriter(fos,"utf-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { osw.write("username:"+user+"\n"); osw.write("passwd:"+passwd+"\n"); osw.write("db_name:"+db_name); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }finally{ try { osw.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }); JMenuItem remote=new JMenuItem("Ô¶³Ì"); connect.add(local); connect.add(remote); start_menu.add(connect); //Ìí¼ÓÍ˳öµÄ¼àÌýÆ÷ quit.addActionListener(new ActionListener( ){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); }}); //"²Ù×÷"²Ëµ¥ÏîÌí¼ÓÔöɾ¸Ä²é¹¦ÄܵÄ×Ӳ˵¥ operate.add(add); operate.add(delete); operate.add(modify); operate.add(query); insert_frame=new MyFrame(); insert_frame.setTitle("²åÈë²Ù×÷"); insert_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); bar.add(operate); start_menu.addSeparator(); start_menu.add(quit); //Ìí¼Ó²Ëµ¥À¸ add(bar,BorderLayout.NORTH); setSize(default_width,default_height); if(init_frame==null){ init_frame=new InitDialog(Main_Frame.this); init_frame.setVisible(true);} } //ÉèÖô°ÌåµÄÏÔʾλÖà private void setshow(JFrame frame,int w_x,int w_y){ Dimension size=main.getScreenSize(); //ÆÁÄ»µÄ¿íºÍ¸ß int loc_x=(int) size.getWidth(); int loc_y=(int) size.getHeight(); System.out.println(loc_x+" "+loc_y+" "+w_x/2+" "+w_y/2); System.out.print(loc_x/2-w_x/2); System.out.print(loc_y/2-w_y/2); //½«´°Ìå·ÅÖÃÔÚÕýÖÐÑë frame.setLocation(loc_x/2-300,loc_y/2-200); } }