Initial feeling of java object-oriented thinking

  Abstract: The initial feeling of java object-oriented thinking 

  : / * Create this example, the initial feeling you java object-oriented essence, jtable only used to display, and the use of templates to change jtable properties, the realization of forms and settings from the show * / 

  Import javax.swing .*; 
  Import java.awt .*; 
  Import javax.swing.table .*; 
  Import java.util .*; 
  Import java.awt.event .*; 
  Import javax.swing.table.TableColumn; 
  Import java.sql .*; 
  Import sun.jdbc.odbc.JdbcOdbcDriver; 
  Class Typec extends JFrame 
  ( 
  / / Form controls and form 
  JPanel pnla = null; 
  JPanel pnlb = null; 
  JTable table = null; 
  TestModela md = null; 
  TestModelb mdb = null; 
  TableColumn tc = null; 
  Javax.swing.JComboBox jc = null; 
  Javax.swing.JCheckBox jboxa = null; 
  Javax.swing.JCheckBox jboxb = null; 
  DataBase db = new DataBase (); / / sql data link 

  Javax.swing.JScrollPane js; 
  JButton but; 
  JTextField jt1 = null; 
  JTextField jt2 = null; 
  JTextField jt3 = null; 
  JTextField jt4 = null; 
  JTextField jt5 = null; 

  / / Object array structure jtable 
  Object [] [] a = new Object [2] [3]; 
  Object [] b = ( "a" and "b" and "c"); 

  / / Object array structure jtable 
  Object [] [] = ((aa "a" and "b" and "c")); 

  / / Vector array structure jtable 
  Vector va, vb, vc; 

  / / Function to achieve 
  / / 1) Object array structure jtable added to the form boost 
  / / 2) will be the first out JTable attributes to the text box, the second in the drop-down box to change attributes 
  Void addObject () 
  ( 
  Table = new JTable (a, b); 

  Tc = new TableColumn (); 
  Jt1 = new javax.swing.JTextField (10); 
  Jt1.setBackground (Color.red); 
  Jc = new JComboBox (); 
  Jc.addItem ( "M"); 
  Jc.addItem ( "F"); 
  Tc = table.getColumnModel (). GetColumn (1); 
  Tc.setCellEditor (new DefaultCellEditor (jt1)); 
  Tc = table.getColumnModel (). GetColumn (2); 
  Tc.setCellEditor (new DefaultCellEditor (jc)); 

  Js = new JScrollPane (table); 
  This.getContentPane (). Add (js, BorderLayout.CENTER); 
  This.setSize (700,350); / / must be added after that would not otherwise normal display 
  This.show (); 
  ) 

  / / Function to achieve 
  / / 1) Object templates to create JTable 
  / / 2) to achieve JTable forms the border and hide and display b specified for the hidden and display 
  Void addObjectMd () 
  ( 
  Pnla = new JPanel (); 
  Jt1 = new JTextField (10); 
  But = new JButton ( "to set up high"); 
  But.addActionListener (new butsj ()); 
  Jboxa = new JCheckBox ( "hidden", false); 
  Jboxa.addItemListener (new MyItemListener ()); 
  Jboxb = new JCheckBox (the "delete", false); 
  Jboxb.addItemListener (new MyItemListener ()); 
  Pnla.add (jt1); 
  Pnla.add (but); 
  Pnla.add (jboxa); 
  Pnla.add (jboxb); 

  Md = new TestModela (aa, b) / / templates must be used, there are an array of value 
  Table = new JTable (md); 
  Js = new JScrollPane (table); 
  Pnlb = (JPanel) this.getContentPane (); 
  Pnlb.setLayout (new BorderLayout ()); 
  Pnlb.add (pnla, BorderLayout.NORTH); 
  Pnlb.add (js, BorderLayout.CENTER); 

  This.setSize (700,350); / / must be added after that would not otherwise normal display 
  This.show (); 

  ) 

  Void addVector () 
  ( 

  Vc = new Vector (); 
  Vc.add (the "va"); 
  Vc.add ( "vb"); 
  Vc.add ( "vc"); 
  Va = new Vector (); 
  Va.add (vc); 
  Vb = new Vector (); 
  Vb.add (the "va"); 
  Vb.add ( "vb"); 
  Vb.add ( "vc"); 
  Table = new JTable (va, vb); 
  Js = new JScrollPane (table); 
  This.getContentPane (). Add (js, BorderLayout.CENTER); 
  This.setSize (700,350); / / must be added after that would not otherwise normal display 
  This.show (); 
  ) 
  Void addVectorMd () / / class template czVector 
  ( 

  / / Definition of a data table, the equivalent of a one-dimensional array 
  Vc = new Vector (); 
  Vc.add (the "va"); 
  Vc.add ( "vb"); 
  Vc.add ( "vc"); 
  Vc.add (new Boolean (false)); 
  Va = new Vector (); 
  / / Data to be generated in to va 
  Va.add (vc); 
  Vb = new Vector (); 
  / / Definition of the title of JTable 
  Vb.add (the "va"); 
  Vb.add ( "vb"); 
  Vb.add ( "vc"); 
  Vb.add ( "vd"); 
  Mdb = new TestModelb (va, vb); 
  Table = new JTable (mdb); 
  / / Forms have been completed structure 

  Js = new JScrollPane (table); 
  Jt1 = new JTextField (10); 
  Jt2 = new JTextField (10); 
  Jt3 = new JTextField (10); 
  Jt4 = new JTextField (10); 

  But = new JButton ( "add"); 
  But.addActionListener (new butsj ()); 

  Pnla = new JPanel (); 
  Pnla.add (jt1); 
  Pnla.add (jt2); 
  Pnla.add (jt3); 
  Pnla.add (jt4); 

  Pnla.add (but); 

  Pnlb = (JPanel) this.getContentPane (); 
  Pnlb.setLayout (new BorderLayout ()); 
  Pnlb.add (pnla, BorderLayout.NORTH); 
  Pnlb.add (js, BorderLayout.CENTER); 
  This.setSize (700,350); / / must be added after that would not otherwise normal display 
  This.show (); 
  ) 

  Void addVectorSql () 
  ( 
  DataBase.FillVector ( "select * from students"); 
  Va = DataBase.data; / / Get a row in the database data 
  Vb = DataBase.title; / / database access to the title 

  Mdb = new TestModelb (va, vb); 
  Table = new JTable (mdb); 
  Js = new JScrollPane (table); 
  Pnla = new JPanel (); 
  Pnlb = (JPanel) this.getContentPane (); 
  Pnlb.setLayout (new BorderLayout ()); 
  Pnlb.add (pnla, BorderLayout.NORTH); 
  Pnlb.add (js, BorderLayout.CENTER); 
  This.setSize (700,350); / / must be added after that would not otherwise normal display 
  This.show (); 
  ) 
  / / / / Click incident 
  Private class butsj implements ActionListener 
  ( 
  Public void actionPerformed (ActionEvent e) 
  ( 
  If ((e.getSource () == but) & & but.getText (). Equals ( "add")) 
  ( 
  Vc = new Vector (); 
  Vc.add (jt1.getText ()); 
  Vc.add (jt2.getText ()); 
  Vc.add (jt3.getText ()); 
  Vc.add (new Boolean (jt4.getText (). EqualsIgnoreCase ( "true ")));// this String comparison with another String, and not consider case. 
  Mdb.AddRow (vc); 
  ) 
  Else if ((e.getSource () == but) & & but.getText (). Equals ( "to set up high")) 
  ( 

  Int h = Integer.parseInt (jt1.getText ());// be imported duty 
  If (h> 0) 
  Table.setRowHeight (h) / / set up to the high table of 
  ) 

  ) 
  ) 

  / / CheckBox elections incident 
  Private class MyItemListener implements ItemListener 
  ( 
  Public void itemStateChanged (ItemEvent e) 
  ( 
  / / First source of the incident to determine the source judge whether the incident was selected 
  If (e.getSource () == jboxa) 
  If (jboxa.isSelected ()) 
  ( 
  / / Settings are displayed form lines 
  Table.setShowHorizontalLines (true); 
  Table.setShowVerticalLines (true); 
  ) 
  Else 
  ( 

  Table.setShowHorizontalLines (false); 
  Table.setShowVerticalLines (false); 
  ) 
  / / Delete scores out whether 
  If (e.getSource () == jboxb) 
  If (select whether jboxb.isSelected ())// 
  ( 
  Tc = table.getColumnModel (). GetColumn (2); 
  Table.getColumnModel (). RemoveColumn (tc); 
  Table.updateUI (); 
  ) 
  Else 
  ( 
  Table.getColumnModel (). AddColumn (tc); 
  ) 

  ) 
  ) 

  ) / / This is the right Typec brackets, not confuse you 
  / / Object templates to create jtable 
  Class TestModela extends javax.swing.table.DefaultTableModel 
  ( 
  Object [] [] data; 
  Object [] title; 
  TestModela (Object [] [] d, Object [] t) 
  ( 
  Super (d, t); 
  This.data = d; 
  This.title = t; 
  ) 
  Public Object getValueAt (int r, int c) 
  ( 
  Return data [r] [c] / / c will be r disaggregated data 
  ) 
  Public Class getColumnClass (int c) 
  ( 
  / / Will be different types of data 
  / / In the form of correspondence, we can compare this example in the show 
  / / And a few examples of the difference between the former 
  Return data [0] [c]. GetClass (); 
  ) 
  / / Set to r, c column values for the value 
  / / When the user to amend the data in the JTable, can automatically call, and modify the data in the data 
  Public void setValueAt (Object value, int r, int c) 
  ( 
  Data [r] [c] = value; 
  ) 
  ) 
  / / Use Vector to create templates jtable 
  / / The JTable, it is a two-dimensional structure, the entire data in the table by a Vector 
  / / For each row in the table is a Vector 
  Class TestModelb extends javax.swing.table.DefaultTableModel 
  ( 

  Vector a; 
  Vector b; 
  TestModelb (Vector a, Vector b) 
  ( 
  Super (a, b); 
  This.a = a; 
  This.b = b; 
  ) 
  Public Object getValueAt (int c, int b) / / by each disaggregated data 
  ( 
  Vector v = (Vector) a.get (c); / / Retrieve data in the array variable 
  Return v.get (b); / / are not an every line of the value of 
  ) 
  Public Class getColumnClass (int c) 
  ( 
  Vector v = (Vector) a.get (0); 
  Return v.get (c). GetClass () / / of the string into the types of data 
  ) 

  Public boolean isCellEditable (int r, int c) 
  ( 
  / / In addition to the second column is not to be revised 
  / / Other may be amended 
  If (c == 2) 
  Return false; 
  Return true; 
  ) 

  / / Custom TableModel in the method of rewriting the AddRow 
  / / Add data to a Vector, representing an increase of a record 
  Public void AddRow (Vector row) 
  ( 
  A.add (row); 
  This.fireTableDataChanged ();// notify all interception, all forms of cell line may have changes, if we do not call will not change 
  ) 

  //??????????????????? How to delete it 
  ) 
  / * 
  / / In the pubs database built a table students 
  Create table students (st_xh varchar (10), st_name varchar (10), st_age int) 
  Insert into students 
  Values ('1000 0001 ',' Wang ', 25) 
  Odbc to build a data source test 
  Link pubs 
  * / 
  Class DataBase / / Database Class cz 
  ( 
  Public static Connection conn = null; / / This is a connection object 
  Public static ResultSet rs = null; / / This is a record set targets 
  Public static Statement st = null; / / This is a language of object 
  Public static PreparedStatement pst = null; / / with the implementation of the sentence according to parameters 
  Public static CallableStatement cst = null; / / storage implementation process 
  Public static ResultSetMetaData rsmd = null; / / record set by the original information 
  Public static Vector data, title, row; 
  / / Constructor function in the database to link 
  Public DataBase () 
  ( 
  Data = new Vector (); 
  Title = new Vector (); / / abnormal capture must be used, otherwise the error procedures 
  Try ( 
  Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver"); 
  Conn = DriverManager.getConnection ( "jdbc: odbc: test", "sa", "sa"); 
  St = conn.createStatement (); 
  ) Catch (SQLException e) 
  ( 
  E.printStackTrace (); 
  ) Catch (Exception e) 
  ( 
  E.printStackTrace (); 
  ) 
  ) 
  / / According to the two strSql to increase data collection 
  / / Data stored in each record in the table, each record also Add in a Vector 
  / / Title in the storage of information in the table, according to provide ResultSetMetaData 
  Public static void FillVector (String strSql) 
  ( 
  Int s; 
  Int fieldCount; 
  Try ( 
  Data.clear (); 
  Title.clear (); 
  Rs = st.executeQuery (strSql); / / Set the outcome 
  Rsmd = rs.getMetaData ();// guitar investigation by the original data set 
  FieldCount = rsmd.getColumnCount (); 
  While (rs.next ()) 
  ( 
  Row = new Vector (); 
  For (int i = 0; i    ( 
  Row.add (rs.getString (i +1)); 
  ) 
  / / Add a pass on the increase in a false or true 
  If (Integer.parseInt (row.get (row.size () -1). ToString ())> 60) 
  Row.add (new Boolean (true)); 
  Else 
  Row.add (new Boolean (false)); 

  Data.add (row); 
  System.out.println (data.size ()); 
  ) 
  For (int i = 0; i    ( 
  Title.add (rsmd.getColumnLabel (i +1)); 
  ) 
  Title.add ( "Pass"); 
  ) Catch (SQLException e) 
  ( 
  E.printStackTrace (); 
  ) 
  ) 
  ) 

  Class test 
  ( 
  Public static void main (String args []) 
  ( 
  Typec tc = new Typec (); 

  / / Tc.addObject (); 
  / / Tc.addObjectMd (); 
  / / Tc.addVector (); 
  / / Tc.addVectorMd (); 
  Tc.addVectorSql (); 

  ) 
  ) Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp , scjd / * create this example, the initial feeling you java-oriented? 
  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply