?? linenumber.java
字號:
package org.jr.jzj.editor;
/**
* <p>Copyright: Copyright (c) 2002-2003</p>
* <p>Company: JavaResearch(http://www.javaresearch.org)</p>
* <p>最后更新日期:2003年3月20日
* @author Barney,Cherami,Brain
* @version 0.8
*/
import java.awt.*;
import javax.swing.*;
public class LineNumber
extends JComponent {
private final static Color DEFAULT_BACKGROUND = Color.white;
private final static Color DEFAULT_FOREGROUND = new Color(153, 153, 204);
private final static Color DEFAULT_LINECLR = new Color(192, 192, 192);
private final static Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 12);
//private final static
private final static int HEIGHT = Integer.MAX_VALUE - 1000000;
private final static int MARGIN = 5;
private FontMetrics fontMetrics;
private int lineHeight;
private int currentRowWidth;
private JComponent component;
private int componentFontHeight;
private int componentFontAscent;
public LineNumber(JComponent component) {
if (component == null) {
setBackground(DEFAULT_BACKGROUND);
setForeground(DEFAULT_FOREGROUND);
setFont(DEFAULT_FONT);
this.component = this;
}
else {
setBackground(DEFAULT_BACKGROUND);
setForeground(component.getForeground());
setFont(component.getFont());
this.component = component;
}
componentFontHeight = component.getFontMetrics(component.getFont()).
getHeight();
componentFontAscent = component.getFontMetrics(component.getFont()).
getAscent();
setPreferredWidth(9999);
this.setBorder(BorderFactory.createLineBorder(DEFAULT_LINECLR, 1));
}
public void setPreferredWidth(int row) {
int width = fontMetrics.stringWidth(String.valueOf(row));
if (currentRowWidth < width) {
currentRowWidth = width;
setPreferredSize(new Dimension(2 * MARGIN + width, HEIGHT));
}
}
public void setFont(Font font) {
super.setFont(font);
fontMetrics = getFontMetrics(getFont());
}
public int getLineHeight() {
if (lineHeight == 0) {
return componentFontHeight;
}
else {
return lineHeight;
}
}
public void setLineHeight(int lineHeight) {
if (lineHeight > 0) {
this.lineHeight = lineHeight;
}
}
public int getStartOffset() {
return component.getInsets().top + componentFontAscent;
}
public void paintComponent(Graphics g) {
int lineHeight = getLineHeight();
int startOffset = getStartOffset();
Rectangle drawHere = g.getClipBounds();
g.setColor(getBackground());
g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);
g.setColor(getForeground());
int startLineNumber = (drawHere.y / lineHeight) + 1;
int endLineNumber = startLineNumber + (drawHere.height / lineHeight);
int start = (drawHere.y / lineHeight) * lineHeight + startOffset;
for (int i = startLineNumber; i <= endLineNumber; i++) {
String lineNumber = String.valueOf(i);
int width = fontMetrics.stringWidth(lineNumber);
g.drawString(lineNumber, MARGIN + currentRowWidth - width, start);
start += lineHeight;
}
setPreferredWidth(endLineNumber);
//draw line
// g.setColor(DEFAULT_LINECLR);
// g.drawLine(this.getWidth(),0,this.getWidth(),this.getHeight());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -