发布网友 发布时间:2022-04-12 16:39
共3个回答
懂视网 时间:2022-04-12 21:01
2.JTable的滚动
将JTable加入JScrollPane
将JScrollPane加入JFrame
通过JScrollPane面板类完成滚动。其实类似的面板容器还有许多
JTabbedPane------选项卡面板
JSplitPane-----------分割条面板
JScrollPane----------滚动条面板
JDesktopPane-------MDI面板
3.数据的存储问题
全部数据存储在集合类中,如Array,Vector
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package testh2; import java.awt.BorderLayout; import java.awt.EventQueue; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; class TableModel extends AbstractTableModel { public VectorcolumnsName=new Vector (); public Vector Data=new Vector (); //public String[] columnsName={"a","b"}; //public Object[][] Data={{"a1","b1"},{"a2","b2"}}; public int getRowCount() { return Data.size(); } public int getColumnCount() { return columnsName.size(); } public Object getValueAt(int rowIndex, int columnIndex) { return Data.get(rowIndex)[columnIndex]; } } /** * * @author wind */ public class TestH2 { public static void GUItest() throws InterruptedException { final TableModel tm=new TableModel(); tm.columnsName.add("第一列"); tm.columnsName.add("第二列"); tm.columnsName.add("第三列"); tm.columnsName.add("第四列"); tm.columnsName.add("第五列"); tm.columnsName.add("第六列"); tm.columnsName.add("第七列"); tm.columnsName.add("第八列"); EventQueue.invokeLater(new Runnable() { public void run() { JFrame jf=new JFrame(); JTable jt=new JTable(tm); jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane js=new JScrollPane(jt); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(600, 400); jf.setVisible(true); jf.add(js); } }); for(int i=3;i<50;i++) { tm.Data.add(new String[]{"a"+i,"b"+i,"c"+i,"d"+i,"e"+i,"f"+i,"g"+i,"h"+i}); //Thread.sleep(1000); } } public static void H2Test() throws ClassNotFoundException, SQLException { Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection("jdbc:h2:E:/test", "sa", "123456"); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("select * from Test"); String str; while (rs.next()) { str = rs.getString(1) + " "; str += rs.getString(2) + " "; str += rs.getString(3) + " "; str += rs.getString(4) + " "; str += " "; System.out.println(str); } } public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException { // TODO code application logic here H2Test(); GUItest(); } }
热心网友 时间:2022-04-12 18:09
jTable 不能像C#一样有数据绑定热心网友 时间:2022-04-12 19:27
觉得不大行吧,即使绑定,能解决数据库里数据改动的事件吗,还是得去刷新下吧,