JTable示例

package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

public class Frame1 extends JFrame
{
 JPanel jPanel1 = new JPanel();

 JPanel jPanel2 = new JPanel();

 JScrollPane jScrollPane1 = new JScrollPane();

 JTable jTable1 = new JTable();

 JLabel jLabel1 = new JLabel();

 JTextField jTextField1 = new JTextField();

 JButton jButton1 = new JButton();

 JButton jButton2 = new JButton();

 JButton jButton3 = new JButton();

 JButton jButton4 = new JButton();

 public Frame1()
 {
  try
  {
   jbInit();
  } catch (Exception e)
  {
   e.printStackTrace();
  }
 }

 public static void main(String[] args)
 {
  Frame1 frame1 = new Frame1();
  // 定义显示窗体的大小显示窗口
  frame1.setSize(400, 350);
  frame1.setVisible(true);
  // 激活“查询”按钮事件
  frame1.jButton1.doClick();
 }

 private void jbInit() throws Exception
 {
  this.getContentPane().setLayout(null);
  jPanel1.setBackground(Color.red);
  jPanel1.setBounds(new Rectangle(43, 15, 297, 48));
  jPanel1.setLayout(null);
  jPanel2.setBounds(new Rectangle(50, 232, 297, 48));
  jPanel2.setLayout(null);
  jPanel2.setBackground(Color.orange);
  jScrollPane1.getViewport().setBackground(Color.pink);
  jScrollPane1.setBounds(new Rectangle(53, 74, 287, 138));
  jLabel1.setFont(new java.awt.Font("Dialog", 0, 15));
  jLabel1.setText("输入查内容");
  jLabel1.setBounds(new Rectangle(44, 12, 80, 27));
  jTextField1.setText("");
  jTextField1.setBounds(new Rectangle(125, 14, 70, 22));
  jButton1.setBounds(new Rectangle(221, 12, 61, 24));
  jButton1.setFont(new java.awt.Font("Dialog", 0, 13));
  jButton1.setText("查询");
  jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
  jButton2.setBounds(new Rectangle(13, 14, 66, 27));
  jButton2.setFont(new java.awt.Font("Dialog", 0, 13));
  jButton2.setText("增加");
  jButton3.setText("删除");
  jButton3.setBounds(new Rectangle(115, 14, 66, 27));
  jButton3.setFont(new java.awt.Font("Dialog", 0, 13));
  jButton4.setText("修改");
  jButton4.setBounds(new Rectangle(217, 14, 66, 27));
  jButton4.setFont(new java.awt.Font("Dialog", 0, 13));
  this.addHierarchyBoundsListener(new Frame1_this_hierarchyBoundsAdapter(this));
  this.addComponentListener(new Frame1_this_componentAdapter(this));
  this.getContentPane().add(jPanel1, null);
  jPanel1.add(jTextField1, null);
  jPanel1.add(jButton1, null);
  jPanel1.add(jLabel1, null);
  this.getContentPane().add(jPanel2, null);
  jPanel2.add(jButton3, null);
  jPanel2.add(jButton4, null);
  jPanel2.add(jButton2, null);
  this.getContentPane().add(jScrollPane1, null);
  jScrollPane1.getViewport().add(jTable1, null);
 }

 void this_componentResized(ComponentEvent e)
 {
  // 设置jPanel1的起点坐标
  jPanel1.setLocation(0, 0);
  // 设置jPanel1的高度和宽度
  jPanel1.setSize(this.getWidth(), jPanel1.getHeight());
  // 设置jPanel2的起点坐标(37是标题栏的高度)
  jPanel2.setLocation(0, this.getHeight() - jPanel2.getHeight() - 37);
  // 设置jPanel2的高度和宽度
  jPanel2.setSize(this.getWidth(), jPanel2.getHeight());
  // 设置jScrollPanel的起点坐标
  jScrollPane1.setLocation(0, jPanel1.getHeight());
  // 设置jScrollPanel的高度和宽度
  jScrollPane1.setSize(this.getWidth(), jPanel2.getY() - jPanel1.getHeight());
 }

 void jButton1_actionPerformed(ActionEvent e)
 {
  // “查询”按钮事件,模拟从数据库中取数据并显示在表格中
  try
  {
   Vector vcol = new Vector(); // 列名
   Vector vrow = new Vector(); // 内容
   // 用一个for循环来模拟数据库的内容
   for (int col = 1; col < 31; col++)
   {
    vcol.addElement("列" + col);
   }
   for (int row = 1; row < 101; row++)
   {
    Vector vr1 = new Vector();
    for (int col = 1; col < 31; col++)
    {
     vr1.addElement(row + "/" + col);
    }
    vrow.addElement(vr1);
   }
   DefaultTableModel dtm = new DefaultTableModel(vrow, vcol);
   jTable1 = new JTable(vrow, vcol);
   // 滚动条设置左右滚
   jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
   // 在滚动条中放入表
   this.jScrollPane1.getViewport().add(jTable1, null);
  } catch (Exception ex)
  {
   System.out.println("错误:" + ex);
  }
 }
}

class Frame1_this_hierarchyBoundsAdapter extends java.awt.event.HierarchyBoundsAdapter
{
 Frame1 adaptee;

 Frame1_this_hierarchyBoundsAdapter(Frame1 adaptee)
 {
  this.adaptee = adaptee;
 }

 public void ancestorResized(HierarchyEvent e)
 {
 }
}

class Frame1_this_componentAdapter extends java.awt.event.ComponentAdapter
{
 Frame1 adaptee;

 Frame1_this_componentAdapter(Frame1 adaptee)
 {
  this.adaptee = adaptee;
 }

 public void componentResized(ComponentEvent e)
 {
  adaptee.this_componentResized(e);
 }
}

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener
{
 Frame1 adaptee;

 Frame1_jButton1_actionAdapter(Frame1 adaptee)
 {
  this.adaptee = adaptee;
 }

 public void actionPerformed(ActionEvent e)
 {
  adaptee.jButton1_actionPerformed(e);
 }

你可能感兴趣的:(java,swing)