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

📄 dbgrid.java

📁 用java AWT开发的Grid表格组件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class dbGrid extends Panel implements MouseListener {
//public class dbGrid extends Container implements MouseListener {

int rows;
int rows_show;
int rows_show_max;
int rows_show_min;
int cols;
int width_size;
int width;
int height;
int width_max;
int width0;
int common_rowheight;
int common_colwidth;
int rowheight[];
int colwidth[];
int colAlignment[];
Point pLocation;
boolean b_max;
int toprow;
int row_clicked;
Color Background;
Color Foreground;
Label lab[][];
Cell cell[][];
Font font;
int rows_max=80000;
Scrollbar sb;

CheckboxGroup group;
Checkbox option[];

dbGrid(int rows_show,int cols,int common_rowheight,int width)
{
 int swidth=0;
 int sheight=0;
 this.width0=width;
 this.rows_show_min=rows_show;
 this.rows_show=rows_show;
 this.rows=rows_show;
 this.cols=cols;
 this.common_rowheight=common_rowheight;
 this.common_colwidth=50;
 this.rows_show_max=20;
 this.setLayout(null);
 this.toprow=1;
 this.row_clicked=0;
 this.width=width;
 this.b_max=false;
 this.height=rows_show*this.common_rowheight;
 this.font=new Font("Times New Roman",0,12);
 lab=new Label[rows_show_max][cols];
 cell=new Cell[this.rows_max][cols];

 for(int i=0;i<this.rows;i++)
    for(int j=0;j<this.cols;j++)
        cell[i][j]=new Cell("");
 colwidth=new int[cols];
 rowheight=new int[rows_max];
 colAlignment=new int[cols];

 this.Background=Color.white;
 this.Foreground=Color.black;

 for(int i=0;i<this.cols;i++)
   {
    this.colwidth[i]=this.common_colwidth;
    this.colAlignment[i]=0;
    for (int j=0;j<this.rows;j++)
        {
        this.cell[j][i].width=this.common_colwidth;
        this.cell[j][i].Alignment=this.colAlignment[i];
        }
   }

 for(int i=0;i<this.rows;i++)
   {
    this.rowheight[i]=this.common_rowheight;
    for (int j=0;j<this.cols;j++)
        {
        this.cell[i][j].height=this.common_rowheight;
        this.cell[i][j].font=this.font;
        this.cell[i][j].Background=this.Background;
        this.cell[i][j].Foreground=this.Foreground;
        }
    }

 for(int i=0;i<rows_show_max;i++)
    for(int j=0;j<cols;j++)
       {
       this.lab[i][j]=new Label("");
       this.add(lab[i][j]);
       }

  group=new CheckboxGroup();
  option=new Checkbox[this.rows_show_max];
  for(int i=0;i<rows_show_max;i++)
      {
      option[i]=new Checkbox("",group,false);
      //option[i]=new Checkbox("");
      this.add(option[i]);
      }
  option[0].setState(true);
  option[0].setVisible(false);


  this.sb=new Scrollbar(Scrollbar.VERTICAL,1,0,1,this.rows_show-1);
  this.sb.setLineIncrement(1);
  this.sb.setPageIncrement(this.rows_show-1);
  this.sb.setVisible(false);
  this.add(this.sb);


  for(int i=0;i<rows_show;i++)
   {swidth=0;
    for(int j=0;j<cols;j++)
       {
       this.lab[i][j].reshape(swidth+30,sheight,this.colwidth[j],this.rowheight[i]);
       swidth+=this.colwidth[j];
       }
    sheight+=this.rowheight[i];
    }


  //sheight=this.rowheight[0];
  sheight=0;
  for(int i=0;i<rows_show;i++)
   {
    this.option[i].reshape(10,sheight,20,this.rowheight[i]);
    sheight+=this.rowheight[i];
    }

    this.sb.reshape(this.width-20,0,20,this.height);
    this.reshape(0,0,this.width,this.height);



 addMouseListener(this);

}//end constructor

public void setGridBackground(Color Background)
{ this.Background=Background;
  for(int i=0;i<this.rows;i++)
    for(int j=0;j<cols;j++)
       {
       this.cell[i][j].setBackground(Background);
       }
   for(int i=0;i<this.rows_show_max;i++)
       {
       this.option[i].setBackground(Background);
       }

}
public void setGridForeground(Color Foreground)
{ this.Foreground=Foreground;
  for(int i=0;i<this.rows;i++)
    for(int j=0;j<cols;j++)
       {
       this.cell[i][j].setForeground(Foreground);
       }
}

public void setColFont(int Col,Font font)
{
  for(int i=1;i<this.rows_max;i++)
     this.cell[i][Col].setFont(font);
  for (int i=1;i<this.rows_show;i++)
    this.lab[i][Col].setFont(cell[i+this.toprow-1][Col].font);

 
}


public void setRowFont(int row,Font font)
{
  for(int i=0;i<this.cols;i++)
     this.cell[row][i].setFont(font);
  
  if((row>this.toprow-1)&&(row<this.toprow+this.rows_show-1))
    {
    for (int i=0;i<this.cols;i++)
    this.lab[row-this.toprow+1][i].setFont(cell[row][i].font);
    }
}

public void setCellFont(int row,int col,Font font)
{
   this.cell[row][col].setFont(font);
   if((row>this.toprow-1)&&(row<this.toprow+this.rows_show-1))
    this.lab[row-this.toprow+1][col].setFont(cell[row][col].font);
   
}

public void setCellForeground(int row,int col,Color CellForeground)
{
cell[row][col].setForeground(CellForeground);
if((row>this.toprow-1)&&(row<this.toprow+this.rows_show-1))
    this.lab[row-this.toprow+1][col].setForeground(cell[row][col].Foreground);
}//end setCellForeground

public  void setCellBackground(int row,int col,Color CellBackground)
{
this.cell[row][col].setBackground(CellBackground);
if((row>this.toprow-1)&&(row<this.toprow+this.rows_show-1))
    this.lab[row-this.toprow+1][col].setBackground(cell[row][col].Background);
}//end setCellBackground

public  void setRowBackground(int row,Color RowBackground)
{
 for(int i=0;i<this.cols;i++)
    this.cell[row][i].setBackground(RowBackground);
 if((row>this.toprow-1)&&(row<this.toprow+this.rows_show-1))
    {
    for (int i=0;i<this.cols;i++)
    this.lab[row-this.toprow+1][i].setBackground(cell[row][i].Background);
    }
}//end setCellBackground

public  void setRowForeground(int row,Color RowForeground)
{
 for(int i=0;i<this.cols;i++)
    this.cell[row][i].setForeground(RowForeground);
 if((row>this.toprow-1)&&(row<this.toprow+this.rows_show-1))
    {
    for (int i=0;i<this.cols;i++)
    this.lab[row-this.toprow+1][i].setForeground(cell[row][i].Foreground);
    }
}//end setCellBackground


public void setText(int row,int col,String str)
{
 this.cell[row][col].setText(str);
 if((row>this.toprow-1)&&(row<this.toprow+this.rows_show-2))
    {
    this.lab[row-this.toprow+1][col].setText(this.cell[row][col].text);
    }
}//end setCellForeground
public void drawCellImage2(int row,int col,Image img,double rate_w,double rate_h)
{
}

public void drawCellImage(int row,int col,Image img,double rate_w,double rate_h)
{
if (rate_w>1.0)
   rate_w=1.0;
if (rate_h>1.0)
   rate_h=1.0;
this.cell[row][col].img=img;
this.cell[row][col].b_img=true;
this.cell[row][col].rate_h=rate_h;
this.cell[row][col].rate_w=rate_w;
// System.out.println(row);
/*
  if(((row>this.toprow-1)&&(row<this.toprow+this.rows_show-2))&&(this.cell[row][col].b_img))
  {this.lab[row-this.toprow+1][col].getGraphics().drawImage(this.cell[row][col].img,0,(int)(this.cell[row][col].height*(1.0-this.cell[row][col].rate_h)/2),(int)(this.cell[row][col].width*this.cell[row][col].rate_w),(int)(this.cell[row][col].height*this.cell[row][col].rate_h),this);
  }
*/
}

public String getText(int row,int col)
{
 return cell[row][col].text;
}//end setCellForeground

public void setColwidth(int Col,int width)
{
 this.colwidth[Col]=width;
}//end setColwidth

public void setColAlignment(int Col,int Alignment)
{
 this.colAlignment[Col]=Alignment;
 for(int i=1;i<this.rows;i++)
     this.cell[i][Col].setAlignment(Alignment);
}//end setColwidth

public void setCellAlignment(int Row,int Col,int Alignment)
{
  this.cell[Row][Col].setAlignment(Alignment);
}//end setColwidth

/*public void paint(Graphics g)
{
 super.paint(g);
}
*/
public void refresh()
{  int sheight=0;
   int swidth=0;
   if(this.b_max)
{
 this.rows_show=this.rows_show_max;
 this.set_option_invisible();
 
}

⌨️ 快捷键说明

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