⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 right7_2_3_19.htm

📁 清华JAVA教程。不用多说了吧
💻 HTM
字号:
<html><head><title>JAVA编程语言</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../../css/text.css" type="text/css"></head><body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" ><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">  <tr>     <td valign="top">       <table width="97%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">        <tr>          <td height="244" valign="top">             <p> <span class="pt9-black">   表格是Swing新增加的组件,主要功能是把数据以二维表格的形式显示出来。使用表格,依据M-V-C的思想,最好先生成一个MyTableModel类型的对象来表示数据,这个类是从AbstractTableModel类中继承来的,其中有几个方法是一定要重写,例如getColumnCount,getRowCount,getColumnName,getValueAt。因为Jtable会从这个对象中自动获取表格显示所必需的数据,AbstractTableModel类的对象负责表格大小的确定(行、列)、内容的填写、赋值、表格单元更新的检测等等一切跟表格内容有关的属性及其操作。JTable类生成的对象以该TableModel为参数,并负责将TableModel对象中的数据以表格的形式显示出来。<br>              <br>                JTable类常用的方法有:<br>                getModel() <font color="339900">//获得表格的数据来源对象</font><br>                JTable(TableModel dm) <font color="339900">//dm对象中包含了表格要显示的数据</font><br>                <font color="339900">//下列两个构造方法,第一个参数是数据,第二个参数是表格第一行中显示的内容</font><br>                JTable(object[][]rowData,object[]columnNams);<br>                JTable(Vector[][]rowData,Vector[]columnNams);</span></p>            <p class="pt9-black"><img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"> <font color="000099">例7.3               RecorderOfWorkers</font><br>                import javax.swing.JTable;<br>                import javax.swing.table.AbstractTableModel;<br>                import javax.swing.JScrollPane;<br>                import javax.swing.JFrame;<br>                import javax.swing.SwingUtilities;<br>                import javax.swing.JOptionPane;<br>                import java.awt.*;<br>                import java.awt.event.*;</p>            <p class="pt9-black">  public class TableDemo extends JFrame {<br>                  private boolean DEBUG = true;<br>                  public TableDemo() { <font color="339900">//实现构造方法</font><br>                    super(&quot;RecorderOfWorkers&quot;); <font color="339900">//首先调用父类JFrame的构造方法生成一个窗口</font><br>                    MyTableModel myModel = new MyTableModel();<font color="339900">//myModel存放表格的数据</font><br>                    JTable table = new JTable(myModel);<font color="339900">//表格对象table的数据来源是myModel对象</font><br>                    table.setPreferredScrollableViewportSize(new Dimension(500,               70));<font color="339900">//表格的显示尺寸 </font><br>              <br>                    <font color="339900">//产生一个带滚动条的面板 </font><br>                    JScrollPane scrollPane = new JScrollPane(table);</p>            <p class="pt9-black">       <font color="339900">//将带滚动条的面板添加入窗口中</font><br>                    getContentPane().add(scrollPane, BorderLayout.CENTER);</p>            <p class="pt9-black">       addWindowListener(new WindowAdapter()               {<font color="339900">//注册窗口监听器</font><br>                      public void windowClosing(WindowEvent e) {<br>                        System.exit(0);<br>                      }<br>                    });<br>                }<br>                      <font color="339900">//把要显示在表格中的数据存入字符串数组和Object数组中</font><br>                class MyTableModel extends AbstractTableModel {<br>                   <font color="339900">//表格中第一行所要显示的内容存放在字符串数组columnNames中</font><br>                    final String[] columnNames = {&quot;First Name&quot;, <br>                                &quot;Position&quot;,<br>                                &quot;Telephone&quot;,<br>                                &quot;MonthlyPay&quot;,<br>                                &quot;Married&quot;};<br>                   <font color="339900">//表格中各行的内容保存在二维数组data中</font><br>                    final Object[][] data = {<br>                      {&quot;Wangdong&quot;, &quot;Executive&quot;, <br>                      &quot;01068790231&quot;, new Integer(5000), new Boolean(false)},<br>                      {&quot;LiHong&quot;, &quot;Secretary&quot;, <br>                      &quot;01069785321&quot;, new Integer(3500), new Boolean(true)},<br>                      {&quot;LiRui&quot;, &quot;Manager&quot;,<br>                      &quot;01065498732&quot;, new Integer(4500), new Boolean(false)},<br>                      {&quot;ZhaoXin&quot;, &quot;Safeguard&quot;,<br>                      &quot;01062796879&quot;, new Integer(2000), new Boolean(true)},<br>                      {&quot;ChenLei&quot;, &quot;Salesman&quot;,<br>                      &quot;01063541298&quot;, new Integer(4000), new Boolean(false)}<br>                    };</p>            <p class="pt9-black">       <font color="339900">//下述方法是重写AbstractTableModel中的方法,其主要用途是被JTable对象调用,以便在表格中正确的显示出来。程序员必须根据采用的数据类型加以恰当实现。               <br>               <br>                    //获得列的数目</font><br>                    public int getColumnCount() {<br>                       return columnNames.length;<br>                    }</p>            <p class="pt9-black">       <font color="339900">//获得行的数目</font><br>                    public int getRowCount() {<br>                       return data.length;<br>                    }<br>              <br>                    <font color="339900">//获得某列的名字,而目前各列的名字保存在字符串数组columnNames中</font><br>                    public String getColumnName(int col) {<br>                       return columnNames[col];<br>                    }<br>              <br>                    <font color="339900">//获得某行某列的数据,而数据保存在对象数组data中</font><br>                    public Object getValueAt(int row, int col) {<br>                       return data[row][col];<br>                    }</p>            <p class="pt9-black">       <font color="339900">//判断每个单元格的类型</font><br>                    public Class getColumnClass(int c) {<br>                       return getValueAt(0, c).getClass();<br>                    }</p>            <p class="pt9-black">       <font color="339900">//将表格声明为可编辑的</font><br>                    public boolean isCellEditable(int row, int col) {<br>              <br>                       if (col &lt; 2) { <br>                         return false;<br>                       } else {<br>                         return true;<br>                       }<br>                    }</p>            <p class="pt9-black">       <font color="339900">//改变某个数据的值</font><br>                    public void setValueAt(Object value, int row, int col) {<br>                       if (DEBUG) {<br>                         System.out.println(&quot;Setting value at &quot; + row               + &quot;,<br>                               &quot; + col<br>                                + &quot; to &quot; + value<br>                                + &quot; (an instance of &quot; <br>                                + value.getClass() + &quot;)&quot;);<br>                       }</p>            <p class="pt9-black">          if (data[0][col] instanceof Integer               <br>                           &amp;&amp; !(value instanceof Integer)) { <br>                        try {<br>                           data[row][col] = new Integer(value.toString());<br>                           fireTableCellUpdated(row, col);<br>                        } catch (NumberFormatException e) {<br>                           JOptionPane.showMessageDialog(TableDemo.this,<br>                            &quot;The \&quot;&quot; + getColumnName(col)<br>                            + &quot;\&quot; column accepts only integer values.&quot;);<br>                        }<br>                    } else {<br>                        data[row][col] = value;<br>                        fireTableCellUpdated(row, col);<br>                    }</p>            <p class="pt9-black">       if (DEBUG) {<br>                        System.out.println(&quot;New value of data:&quot;);<br>                        printDebugData();<br>                    }<br>                 }</p>            <p><span class="pt9-black">    private void printDebugData() {</span><br>                   <span class="pt9-black">int numRows = getRowCount();<br>                    int numCols = getColumnCount();</span></p>            <p class="pt9-black">       for (int i=0; i &lt; numRows; i++) {<br>                      System.out.print(&quot; row &quot; + i + &quot;:&quot;);<br>                      for (int j=0; j &lt; numCols; j++) {<br>                        System.out.print(&quot; &quot; + data[i][j]);<br>                      }<br>                      <span class="pt9-black">System.out.println();<br>                    }<br>                    System.out.println(&quot;--------------------------&quot;);<br>                 }<br>                } </span> </p>            <p class="pt9-black">   public static void main(String[] args) {<br>                 TableDemo frame = new TableDemo();<br>                 frame.pack();<br>                 frame.setVisible(true);<br>                }<br>               }</p>            </td>        </tr>      </table>    </td>  </tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -