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

📄 xlist.java

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

  public Dimension getMinimumSize() {
    return getCalculatedSize();
  }


  /************************************************************************
   * Calculates the size required to display all rows and columns.
   ************************************************************************/

  private Dimension getCalculatedSize() {
    return new Dimension(getWidth(), getHeight());
  }


  /************************************************************************
   * Resizes the XList to a certain Dimension.
   * @param dim      the XList's new Dimension
   ************************************************************************/

  public void setSize(Dimension dim) {
    super.setSize(dim);
    iNrOfVisibleLines = dim.height / getLineHeight();
    repaint();
  }


  /************************************************************************
   * Resizes the XList to a certain Dimension.
   * @param width       the XList's new width
   * @param height      the XList's new height
   ************************************************************************/

  public void setSize(int width, int height) {
    super.setSize(width, height);
    iNrOfVisibleLines = height / getLineHeight();
    repaint();
  }


  /************************************************************************
   * Reshapes the XList to a certain Dimension.
   * @param x           the XList's new x-ccordinate
   * @param y           the XList's new y-coordinate
   * @param width       the XList's new width
   * @param height      the XList's new height
   ************************************************************************/

  public void setBounds(int x, int y, int width, int height) {
    super.setBounds(x, y, width, height);
    iNrOfVisibleLines = height / getLineHeight();
    repaint();
  }


  /************************************************************************
   * Returns the current width of the XList.
   ************************************************************************/

  public int getWidth() {
    int iWidth;
    iWidth = 0;
    for (int i = 0; i < iNrOfColumns; i++) {
      iWidth += iColumnWidth[i];
    }
    return iWidth + SCROLLBAR_WIDTH;
  }


  /************************************************************************
   * Returns the height required to display one line.
   ************************************************************************/

  private int getLineHeight() {
    return getFontMetrics(fntFont).getAscent() + getFontMetrics(fntFont).getDescent() + VERTICAL_CELL_SPACING * 2;
  }


  /************************************************************************
   * Returns the current height of the XList.
   ************************************************************************/

  public int getHeight() {
    return getLineHeight() * (iNrOfVisibleLines + 1);
  }


  /************************************************************************
   * Returns the starting Point for displaying right-orientated text.
   * @param iColumnIndex      the column index of the text
   * @param iLineIndex        the line index of the text
   * @param strText           the text
   ************************************************************************/

 private Point getRightTextPosition(int iColumnIndex, int iLineIndex, String strText) {
    Point pntPosition;
    pntPosition = getLeftTextPosition(iColumnIndex, iLineIndex);

    return new Point(pntPosition.x + (iColumnWidth[iColumnIndex] - HORIZONTAL_CELL_SPACING * 2 - getFontMetrics(fntFont).stringWidth(strText)), pntPosition.y);
  }


  /************************************************************************
   * Returns the starting Point for displaying left-orientated text.
   * @param iColumnIndex      the column index of the text
   * @param iLineIndex        the line index of the text
   ************************************************************************/

 private Point getLeftTextPosition(int iColumnIndex, int iLineIndex) {
    int iX;
    iX = 0;

    for (int i = 0; i < iColumnIndex; i++) {
      iX += iColumnWidth[i];
    }
    return new Point(iX + HORIZONTAL_CELL_SPACING, getLineHeight() * (iLineIndex - iFirstVisibleLine + 2) - getFontMetrics(fntFont).getDescent() - VERTICAL_CELL_SPACING);
  }


  /************************************************************************
   * Returns the clipping rectangle of a line.
   * @param iLineIndex        the line index
   ************************************************************************/

  private Rectangle getLineRectangle(int iLineIndex) {
    int iX;
    iX = 0;

    for (int i = 0; i < iNrOfColumns; i++) {
      iX += iColumnWidth[i];
    }
    return new Rectangle(1, getLineHeight() * (iLineIndex - iFirstVisibleLine + 1), iX, getLineHeight());
  }


  /************************************************************************
   * Splits the text of a row into lines.
   * @param iRowParam        the row index
   ************************************************************************/

  private String[][] getRowSplitter(int iRowParam) {
    int iMaxNrOfLines;
    String strRowSplitter[][];
    String strSingleCell[];
    Vector vecColumnSplitter[];

    strRowSplitter = new String[iNrOfColumns][];
    vecColumnSplitter = new Vector[iNrOfColumns];
    iMaxNrOfLines = 0;
    for (int i = 0; i < iNrOfColumns; i++) {
      if (iColumnOrientation[i] == FLOW) {
        vecColumnSplitter[i] = getCellTextSplitter(i, iRowParam);
      }
      else {
        vecColumnSplitter[i] = new Vector();
        vecColumnSplitter[i].addElement(getFormatedText(iRowParam < vecContent[i].size() ? (String)vecContent[i].elementAt(iRowParam) : "", i, iColumnOrientation[i]));
      }
      iMaxNrOfLines = Math.max(iMaxNrOfLines, vecColumnSplitter[i].size());
    }
    for (int i = 0; i < iNrOfColumns; i++) {
      strRowSplitter[i] = new String[iMaxNrOfLines];
      for (int j = 0; j < iMaxNrOfLines; j++) {
        if (j < vecColumnSplitter[i].size()) {
          strRowSplitter[i][j] = (String)vecColumnSplitter[i].elementAt(j);
        }
        else {
          strRowSplitter[i][j] = "";
        }
      }
    }
    return strRowSplitter;
  }


  /************************************************************************
   * Wraps a text so it will fit into its column.
   * @param strTextParam         the text to be wrapped
   * @param iAvailableWidth      the available column width
   ************************************************************************/

  private String getBrokenText(String strTextParam, int iAvailableWidth) {
    StringTokenizer stkText;
    String strText, strWord, strBrokenWord;
    FontMetrics fnmMetrics;
    int iIndex;

    if ((iIndex = strTextParam.indexOf("\n")) != -1) {
      return getBrokenText(strTextParam.substring(0, iIndex), iAvailableWidth) + "\n" + getBrokenText(strTextParam.substring(iIndex + 1), iAvailableWidth);
    }
    else {
      stkText = new StringTokenizer(strTextParam, " ");
      strText = "";
      fnmMetrics = getFontMetrics(fntFont);

      while (stkText.hasMoreTokens()) {
        strWord = stkText.nextToken();
        if (fnmMetrics.stringWidth(strWord) < iAvailableWidth) {
          strText += (strWord + " ");
        }
        else {
          strBrokenWord = "";
          for (int i = 0; i < strWord.length() && fnmMetrics.stringWidth(strBrokenWord + strWord.charAt(i)) < iAvailableWidth; i++) {
            strBrokenWord += strWord.charAt(i);
          }
          strText += (strBrokenWord + " " + getBrokenText(strWord.substring(strBrokenWord.length()), iAvailableWidth) + " ");
        }
      }
      return strText;
    }
  }


  /************************************************************************
   * Formats a text depending on its orientation.
   * @param strTextParam           the text to be wrapped
   * @param iColumnParam           the index of the column
   * @param iOrientationParam      the orientation
   ************************************************************************/

  private String getFormatedText(String strText, int iColumnParam, int iOrientationParam) {
    String strFormated;
    FontMetrics fnmMetrics;
    int iAvailableWidth;

    strFormated = "";
    fnmMetrics = getFontMetrics(fntFont);
    iAvailableWidth = iColumnWidth[iColumnParam] - HORIZONTAL_CELL_SPACING * 2;

    if (iOrientationParam == LEFT || iOrientationParam == RIGHT) {
      if (fnmMetrics.stringWidth(strText) <= iAvailableWidth) {
        return strText;
      }
      else {
        if (iOrientationParam == RIGHT) {
          for (int i = strText.length() - 1; i >= 0 && fnmMetrics.stringWidth("..." +  strText.charAt(i) + strFormated) < iAvailableWidth; i--) {
            strFormated = strText.charAt(i) + strFormated;
          }
          return "..." + strFormated;
        }
        else {
          for (int i = 0; i < strText.length() && fnmMetrics.stringWidth(strFormated + strText.charAt(i) + "...") < iAvailableWidth; i++) {
            strFormated += strText.charAt(i);
          }
          return strFormated + "...";
        }
      }
    }
    return strFormated;
  }


  /************************************************************************
   * Splits a cell's text into several lines.
   * @param iColumnParam           the index of the column
   * @param iRowParam              the index of the row
   ************************************************************************/

 private Vector getCellTextSplitter(int iColumnParam, int iRowParam) {
    StringTokenizer stkCellText;
    Vector vecCellTextSplitter;
    String strLine, strWord;
    int iAvailableWidth, iRequiredWidth, iIndex;
    FontMetrics fnmMetrics;

    vecCellTextSplitter = new Vector();
    fnmMetrics = getFontMetrics(fntFont);
    iAvailableWidth = iColumnWidth[iColumnParam] - HORIZONTAL_CELL_SPACING * 2;

    stkCellText = new StringTokenizer(getBrokenText((String)vecContent[iColumnParam].elementAt(iRowParam), iAvailableWidth), " ");

    strLine = "";

    while (stkCellText.hasMoreTokens()) {
      strWord = stkCellText.nextToken();
      if ((iIndex = strWord.indexOf("\n")) == -1) {
        iRequiredWidth = fnmMetrics.stringWidth(getLineStarter(strLine) + strWord);
        if (iRequiredWidth < iAvailableWidth ) {
          strLine = getLineStarter(strLine) + strWord;
        }
        else if (iRequiredWidth >= iAvailableWidth) {
          vecCellTextSplitter.addElement(strLine);
          strLine = strWord;
        }
      }
      else {
        iRequiredWidth = fnmMetrics.stringWidth(getLineStarter(strLine) + strWord.substring(0, iIndex));
        if (iRequiredWidth < iAvailableWidth) {
          vecCellTextSplitter.addElement(getLineStarter(strLine) + strWord.substring(0, iIndex));
          strLine = strWord.substring(iIndex + 1);
        }
        else {
          vecCellTextSplitter.addElement(strLine);
          if (iIndex != 0) {
            vecCellTextSplitter.addElement(strWord.substring(0, iIndex));
          }
          strLine = strWord.substring(iIndex + 1);
        }
      }
    }
    vecCellTextSplitter.addElement(strLine);
    return vecCellTextSplitter;
  }


  /************************************************************************
   * Returns the beginning of a new line depending of its first word.
   * @param strLineParam      the first word
   ************************************************************************/

  private String getLineStarter(String strLineParam) {
    return strLineParam + (strLineParam.equals("") ? "" : " ");
  }


  /************************************************************************
   * Updates the scrollbar regarding to the number of lines and the lines
   * currently visible.
   ************************************************************************/

  private synchronized void updateScrollbar() {
    scbBar.setValues(iFirstVisibleLine, iNrOfVisibleLines, 0, (iNrOfLines + iNrOfVisibleLines - 1));
  }


  /************************************************************************
   * Updates the XList. No clearing needed as we use double-buffering.
   * @param g      the graphical context
   ************************************************************************/

  public void update(Graphics g) {
    paint(g);
  }

⌨️ 快捷键说明

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