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

📄 xlist.java

📁 是学习swing 开发的基本程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:


  /************************************************************************
   * Paints the XList.
   * @param g      the graphical context
   ************************************************************************/

  public synchronized void paint(Graphics g) {
    int iX, iY, iWidth, iHeight, iNrOfRows, iLineHeight, iNrOfLinesSoFar;
    String strRowSplitter[][];
    String strHeader;
    Rectangle recLine;
    Point pntPosition;
    Graphics graBuffer;

    iWidth = getWidth();
    iHeight = getHeight();

    if (imgBuffer == null || imgBuffer.getWidth(this) != iWidth || imgBuffer.getHeight(this) != iHeight) {
      imgBuffer = createImage(iWidth, iHeight);
    }

    graBuffer = imgBuffer.getGraphics();
    graBuffer.setColor(Color.white);
    graBuffer.fillRect(0, 0, iWidth, iHeight);

    iNrOfRows = getNrOfRows();
    iLineHeight = getLineHeight();

    graBuffer.setFont(fntFont);

    iNrOfLinesPerRow = new int[iNrOfRows];
    iNrOfLinesUntilRow = new int[iNrOfRows];
    iNrOfLinesSoFar = 0;
    for (int i = 0; i < iNrOfRows; i++) {
      strRowSplitter = getRowSplitter(i);
      iNrOfLinesPerRow[i] = strRowSplitter[0].length;
      iNrOfLinesUntilRow[i] = iNrOfLinesSoFar;
      for (int j = 0; j < iNrOfLinesPerRow[i]; j++) {
        if (iNrOfLinesSoFar >= iFirstVisibleLine && iNrOfLinesSoFar <= iFirstVisibleLine + iNrOfVisibleLines) {
          if (i == iSelectedRow && bSelectable) {
            recLine = getLineRectangle(iNrOfLinesSoFar);
            graBuffer.setColor(Color.gray);
            graBuffer.fillRect(recLine.x, recLine.y, recLine.width, recLine.height);
            graBuffer.setColor(Color.white);
          }
          else if (vecColor.size() > i) {
            graBuffer.setColor((Color)vecColor.elementAt(i));
          }
          for (int k = 0; k < iNrOfColumns; k++) {
            if (iColumnOrientation[k] == RIGHT) {
              pntPosition = getRightTextPosition(k, iNrOfLinesSoFar, strRowSplitter[k][j]);
            }
            else {
              pntPosition = getLeftTextPosition(k, iNrOfLinesSoFar);
            }
            graBuffer.drawString(strRowSplitter[k][j], pntPosition.x, pntPosition.y);
          }
        }
        iNrOfLinesSoFar++;
      }
    }
    if (iNrOfRows > 0) {
      iNrOfLines = iNrOfLinesUntilRow[iNrOfRows - 1] + iNrOfLinesPerRow[iNrOfRows - 1];
    }

    graBuffer.setColor(Color.lightGray);
    graBuffer.fillRect(1, 0, iWidth - 1, iLineHeight - 1);

    iX = 1;

    for (int i = 0; i < iNrOfColumns; i++) {
      graBuffer.setColor(Color.white);
      graBuffer.drawLine(iX, 1, iX + iColumnWidth[i] - 2, 1);
      graBuffer.drawLine(iX, 1, iX, iLineHeight - 1);

      graBuffer.setColor(Color.darkGray);
      graBuffer.drawLine(iX, iLineHeight - 1, iX + iColumnWidth[i] - 2, iLineHeight - 1);
      graBuffer.drawLine(iX + iColumnWidth[i] - 2, 1, iX + iColumnWidth[i] - 2, iLineHeight - 1);

      graBuffer.setColor(Color.black);
      graBuffer.drawLine(iX + iColumnWidth[i] - 1, 1, iX + iColumnWidth[i] - 1, iHeight);
      iX += iColumnWidth[i];

      strHeader = getFormatedText(strColumnHeader[i], i, iColumnOrientation[i] == RIGHT ? RIGHT : LEFT);
      if (iColumnOrientation[i] == RIGHT) {
        pntPosition = getRightTextPosition(i, iFirstVisibleLine - 1, strHeader);
      }
      else {
        pntPosition = getLeftTextPosition(i, iFirstVisibleLine - 1);
      }
      graBuffer.drawString(strHeader, pntPosition.x, pntPosition.y);
    }

    graBuffer.setColor(Color.black);
    graBuffer.drawRect(0, 0, iWidth - 1, iHeight - 1);

    g.drawImage(imgBuffer, 0, 0, this);
    updateScrollbar();

  }


/**
 * Invoked when the mouse has been dragged on a component.
 *
 * @param event      the MouseEvent
 */

  public void mouseDragged(MouseEvent event) {
    int iDeltaX;
    if (getCursor().getType() == Cursor.E_RESIZE_CURSOR) {
      iDeltaX = event.getX() - iColumnDraggedX;
      if (iColumnWidth[iColumnSeparator] + iDeltaX >= MINIMUM_COLUMNWIDTH && iColumnWidth[iColumnSeparator + 1] - iDeltaX >= MINIMUM_COLUMNWIDTH) {
        iColumnWidth[iColumnSeparator] += iDeltaX;
        iColumnWidth[iColumnSeparator + 1] -= iDeltaX;
        iColumnDraggedX = event.getX();
        repaint();
        if (iFirstVisibleLine > iNrOfLines - 1) {
          scrollTo(iNrOfLines - 1);
        }
      }
    }
  }


/**
 * Invoked when the mouse has been moved on a component.
 *
 * @param event      the MouseEvent
 */

  public void mouseMoved(MouseEvent event) {
    if (getCursor().getType() == Cursor.E_RESIZE_CURSOR && getColumnSeparator(event.getX()) == -1) {
      setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }
    else if (getCursor().getType() == Cursor.DEFAULT_CURSOR && (iColumnSeparator = getColumnSeparator((iColumnDraggedX = event.getX()))) != -1) {
      setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
    }
  }


/**
 * Invoked when a key has been pressed on a component.
 *
 * @param event      the KeyEvent
 */

  public void keyPressed(KeyEvent event) {
    if (bSelectable) {
      if (event.getKeyCode() == KeyEvent.VK_DOWN && iSelectedRow < getNrOfRows() - 1) {
        iSelectedRow++;
        if (!rowIsVisible(iSelectedRow)) {
          scrollTo(iNrOfLinesUntilRow[iSelectedRow] + iNrOfLinesPerRow[iSelectedRow] - iNrOfVisibleLines);
        }
        else {
          repaint();
        }
      }
      else if (event.getKeyCode() == KeyEvent.VK_UP && iSelectedRow > 0) {
        iSelectedRow--;
        if (!rowIsVisible(iSelectedRow)) {
          scrollTo(iNrOfLinesUntilRow[iSelectedRow]);
        }
        else {
          repaint();
        }
      }
    }
  }


/**
 * Invoked when a key has been released on a component.
 *
 * @param event      the KeyEvent
 */

  public void keyReleased(KeyEvent event) {
  }


/**
 * Invoked when a key has been typed on a component.
 *
 * @param event      the KeyEvent
 */

  public void keyTyped(KeyEvent event) {
  }


/**
 * Invoked when the mouse has been released on a component.
 *
 * @param event      the MouseEvent
 */

  public void mouseReleased(MouseEvent event) {
    int iRow;
    if (bSelectable) {
      iRow = getRowAtPixel(event.getY());
      if (iRow != -1 && iRow != iSelectedRow) {
        iSelectedRow = getRowAtPixel(event.getY());
        repaint();
      }
    }
  }


/**
 * Invoked when the mouse has entered a component.
 *
 * @param event      the MouseEvent
 */

  public void mouseEntered(MouseEvent event) {
  }


/**
 * Invoked when the mouse has exited a component.
 *
 * @param event      the MouseEvent
 */

  public void mouseExited(MouseEvent event) {
  }


/**
 * Invoked when the mouse has been pressed on a component.
 *
 * @param event      the MouseEvent
 */

  public void mousePressed(MouseEvent event) {
  }


/**
 * Invoked when the mouse has been clicked on a component.
 *
 * @param event      the MouseEvent
 */

  public void mouseClicked(MouseEvent event) {
  }


/**
 * Invoked when the value of the adjustable has changed.
 *
 * @param event      the AdjustmentEvent
 */

  public void adjustmentValueChanged(AdjustmentEvent event) {
    scrollTo(event.getValue());
  }


  /************************************************************************
   * Scrolls to a certain line.
   * @param iLineParam      the line's index
   ************************************************************************/

  private synchronized void scrollTo(int iLineParam) {
    if (iLineParam >= 0 && iLineParam < iNrOfLines && iLineParam != iFirstVisibleLine) {
      iFirstVisibleLine = iLineParam;
      repaint();
    }
  }


  /************************************************************************
   * Determines if a row is visible or not.
   * @param iRowParam      the row's index
   ************************************************************************/

  private boolean rowIsVisible(int iRowParam) {
    return iRowParam >= getRowOfLine(iFirstVisibleLine) && (iNrOfLines < iNrOfVisibleLines || iRowParam <= getRowOfLine(iFirstVisibleLine + iNrOfVisibleLines - 1));
  }


  /************************************************************************
   * Returns the row's index a line is belonging to.
   * @param iLineParam      the line's index
   ************************************************************************/

  private synchronized int getRowOfLine(int iLineParam) {
    int iLineCounter;
    iLineCounter = 0;
    for (int i = 0; i < getNrOfRows(); i++) {
      iLineCounter += iNrOfLinesPerRow[i];
      if (iLineCounter > iLineParam) {
        return i;
      }
    }
    return -1;
  }


  /************************************************************************
   * Returns the row's index at a certain pixel.
   * @param iYParam      the pixel's y-coordinate
   ************************************************************************/

  private synchronized int getRowAtPixel(int iYParam) {
    return (iYParam > getLineHeight() ? getRowOfLine(iFirstVisibleLine + iYParam / getLineHeight() - 1) : -1);
  }


  /************************************************************************
   * Returns the index of the column-separator at a certain pixel.
   * @param iXParam      the pixel's x-coordinate
   ************************************************************************/

  public synchronized int getColumnSeparator(int iXParam) {
    int iX;
    iX = 0;
    for (int i = 0; i < iNrOfColumns - 1; i++) {
      iX += iColumnWidth[i];
      if (iX - HORIZONTAL_CELL_SPACING <= iXParam && iXParam <= iX + HORIZONTAL_CELL_SPACING) {
        return i;
      }
    }
    return -1;
  }


}

⌨️ 快捷键说明

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