这个问题我一点思路也没有..... 请问在一般的编译环境中,编辑区域的行数显示条是怎样加到编辑文本框中的?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //看看是不是您要的结果import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JTextArea;import javax.swing.JScrollPane;import java.awt.Color;import java.awt.Font;import java.awt.Dimension;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Insets;import java.awt.BorderLayout;import java.awt.Rectangle;public class TestLineNumber extends JFrame { private static final long serialVersionUID = 1; public static JTextArea output; private JScrollPane scrollPane; private static JFrame fr = new JFrame(); private Font font = new Font("arial", Font.PLAIN, 12); public TestLineNumber() { output = new JTextArea(); output.setMargin(new Insets(5, 5, 5, 5)); output.setFont(font); LineNumber lineNumber = new LineNumber(output); lineNumber.setPreferredSize(9999); scrollPane = new JScrollPane(output); scrollPane.setPreferredSize(new Dimension(600, 400)); scrollPane.setRowHeaderView(lineNumber); fr.getContentPane().add(scrollPane, BorderLayout.CENTER); } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); new TestLineNumber(); fr.setTitle("My - Editor"); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fr.pack(); fr.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } class LineNumber extends JComponent { public LineNumber(JComponent jcomponent) { lineHeight = 0; if (jcomponent == null) { setBackground(DEFAULT_BACKGROUND); setForeground(DEFAULT_FOREGROUND); setFont(DEFAULT_FONT); } else { setBackground(DEFAULT_BACKGROUND); setForeground(jcomponent.getForeground()); setFont(jcomponent.getFont()); } setPreferredSize(9999); } public void setPreferredSize(int i) { int j = fontMetrics.stringWidth(String.valueOf(i)); if (currentRowWidth < j) { currentRowWidth = j; setPreferredSize(new Dimension(2 + j, 0x7ff0bdbf)); } } public void setFont(Font font) { super.setFont(font); fontMetrics = getFontMetrics(getFont()); fontLineHeight = fontMetrics.getHeight(); } public int getLineHeight() { if (lineHeight == 0) return fontLineHeight; else return lineHeight; } public void setLineHeight(int i) { if (i > 0) lineHeight = i; } public int getStartOffset() { return 0; } public void paintComponent(Graphics g) { int i = getLineHeight(); int j = getStartOffset(); Rectangle rectangle = g.getClipBounds(); g.setColor(getBackground()); g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); g.setColor(getForeground()); int k = rectangle.y / i + 1; int l = k + rectangle.height / i; int i1 = (((rectangle.y / i) * i + i) - j) + 1; for (int j1 = k; j1 <= l; j1++) { String s = String.valueOf(j1); int k1 = fontMetrics.stringWidth(s); g.drawString(s, (1 + currentRowWidth) - k1, i1 + 1); i1 += i; } setPreferredSize(l); } private static final long serialVersionUID = 1; private final Color DEFAULT_BACKGROUND = new Color(225, 225, 255); private final Color DEFAULT_FOREGROUND = Color.black; private final Font DEFAULT_FONT = new Font("arial", 0, 15); private int lineHeight; private int fontLineHeight; private int currentRowWidth; private FontMetrics fontMetrics; }} vector循环遍历导致程序退出 请教关于线程同步的一些见解!!谢谢!! java初学者提问,谢谢解答 JAVA基础问题 java认证的问题 请教~ 如何用jfreechart画平滑的曲线 请问现在最流行高效的连接数据库的方式是什么?教科书上的jdbc-odbc早就过时了吧 求助 JCreator 中如何加入"watch" 查看变量值? 怎样将STRING转化为INT,BYTE,BOOLEAN,等数字类型(在线等待) java中怎样将char[]和String相互转换?急 java环境配置 点击“esc”后自动关闭窗口(窗口里有jTextField控件的)
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.BorderLayout;
import java.awt.Rectangle;public class TestLineNumber extends JFrame {
private static final long serialVersionUID = 1; public static JTextArea output; private JScrollPane scrollPane; private static JFrame fr = new JFrame(); private Font font = new Font("arial", Font.PLAIN, 12); public TestLineNumber() {
output = new JTextArea();
output.setMargin(new Insets(5, 5, 5, 5));
output.setFont(font); LineNumber lineNumber = new LineNumber(output);
lineNumber.setPreferredSize(9999); scrollPane = new JScrollPane(output);
scrollPane.setPreferredSize(new Dimension(600, 400));
scrollPane.setRowHeaderView(lineNumber); fr.getContentPane().add(scrollPane, BorderLayout.CENTER);
} private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
new TestLineNumber();
fr.setTitle("My - Editor");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.pack();
fr.setVisible(true);
} public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
} class LineNumber extends JComponent { public LineNumber(JComponent jcomponent) {
lineHeight = 0;
if (jcomponent == null) {
setBackground(DEFAULT_BACKGROUND);
setForeground(DEFAULT_FOREGROUND);
setFont(DEFAULT_FONT);
} else {
setBackground(DEFAULT_BACKGROUND);
setForeground(jcomponent.getForeground());
setFont(jcomponent.getFont());
}
setPreferredSize(9999);
} public void setPreferredSize(int i) {
int j = fontMetrics.stringWidth(String.valueOf(i));
if (currentRowWidth < j) {
currentRowWidth = j;
setPreferredSize(new Dimension(2 + j, 0x7ff0bdbf));
}
} public void setFont(Font font) {
super.setFont(font);
fontMetrics = getFontMetrics(getFont());
fontLineHeight = fontMetrics.getHeight();
} public int getLineHeight() {
if (lineHeight == 0)
return fontLineHeight;
else
return lineHeight;
} public void setLineHeight(int i) {
if (i > 0)
lineHeight = i;
} public int getStartOffset() {
return 0;
} public void paintComponent(Graphics g) {
int i = getLineHeight();
int j = getStartOffset();
Rectangle rectangle = g.getClipBounds();
g.setColor(getBackground());
g.fillRect(rectangle.x, rectangle.y, rectangle.width,
rectangle.height);
g.setColor(getForeground());
int k = rectangle.y / i + 1;
int l = k + rectangle.height / i;
int i1 = (((rectangle.y / i) * i + i) - j) + 1;
for (int j1 = k; j1 <= l; j1++) {
String s = String.valueOf(j1);
int k1 = fontMetrics.stringWidth(s);
g.drawString(s, (1 + currentRowWidth) - k1, i1 + 1);
i1 += i;
} setPreferredSize(l);
} private static final long serialVersionUID = 1; private final Color DEFAULT_BACKGROUND = new Color(225, 225, 255); private final Color DEFAULT_FOREGROUND = Color.black; private final Font DEFAULT_FONT = new Font("arial", 0, 15); private int lineHeight; private int fontLineHeight; private int currentRowWidth; private FontMetrics fontMetrics; }}