本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。import javax.swing.*;import javax.swing.event.*;import java.awt.*;public class java3 extends JFrame{public static JTextPane textPane;public static JScrollPane scrollPane;JPanel panel;public java3(){super("java3()");panel=new JPanel();panel.setLayout(new BorderLayout());panel.setBorder(BorderFactory.createEmptyBor-der(20,20,20,20));textPane=new JTextPane();textPane.setFont(new Font("monospaeed",Font.PLAIN,12));scrollPane=new JScrollPane(textPane);panel.add(scrollPane);scrollPane.setPreferredsize(new Dimension(300,250));setContentPane(panel);setCloseOperation(JFrame.EXIT_ON_CLOSE);LineNumber lineNumber=new LineNumber();scrollPane.setRowHeaderView(lineNumber);}public static void main(String[]args){java3 ttp=new java3();ttp.pack();ttp.setVisible(true);}}class LineNumber extends JTextPane{private final static Color DEFAULT_BACK-GROUND=Color.gray;private final static Color DEFAULT_FORE-GROUND=Color.black;private final static Font DEFAUl。T—FONT=newFont("monospaced",Font.PLAIN,12);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);setForegroun"DEFAULT_FOREGROUND);setFont(DEFAULT FONT);this.component=this;}else{setBaekground(DEFAULT_BACKGROUND);setForeground(component.getForeground());setFont(component.getFont());this.component=component;}componentFontHeight=component.getFontMet-rics(component.getFont()).getHeight();componentFontAscent=component.getFontMet-ries(component.getFont()).getAscent();setPreferredWidth(9999);}public void setPreferredWidth(int row){int width=fontMetrics.stringWidth(String.val-ueOf(row));if(currentRowWidth<;width){currentRowWidth=width;setPreferredSize(new Dfimension(2 * MARGIN+width,HEIGHT));}}public void setFont(Font font){super.setFont(font);fontMetrics=getFontMetrics(getFont());}public int getLineHeight(){if(hneHeight= =0)return componentFontHeight;elsereturn lineHeight;}public void setLineHeight(int lineHeight){if(hneHeight>;0)this.lineHeight=lineHeight;}public int getStartOffset(){return component.getlnsets().top+component-FontAscent;}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/line-Height)+1;int endLineNUmber = startLineNumber+(drawHere.height/lineHeight);int start=(drawHere.Y/hneHeight)*line-Height+startOffset;for(int i=startLineNumber;i<;=endLineN-umber;i++){String lineNumber=String.valueOf(i);int width=fontMetrics.stringWidth(lineNumber);g.drawstring(lineNumber,MARGIN+current-RowWidth-width,start);start+=lineHeight:}setPreferredWidth(endLineNumber);}}
本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class java3 extends JFrame
{
public static JTextPane textPane;
public static JScrollPane scrollPane;
JPanel panel;
public java3()
{
super("java3()");
panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBor-
der(20,20,20,20));
textPane=new JTextPane();
textPane.setFont(new Font("monospaeed",
Font.PLAIN,12));
scrollPane=new JScrollPane(textPane);
panel.add(scrollPane);
scrollPane.setPreferredsize(new Dimension(300,
250));
setContentPane(panel);
setCloseOperation(JFrame.EXIT_ON_CLOSE);
LineNumber lineNumber=new LineNumber();
scrollPane.setRowHeaderView(lineNumber);
}
public static void main(String[]args)
{
java3 ttp=new java3();
ttp.pack();
ttp.setVisible(true);
}
}
class LineNumber extends JTextPane
{
private final static Color DEFAULT_BACK-
GROUND=Color.gray;
private final static Color DEFAULT_FORE-
GROUND=Color.black;
private final static Font DEFAUl。T—FONT=new
Font("monospaced",Font.PLAIN,12);
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);
setForegroun"DEFAULT_FOREGROUND);
setFont(DEFAULT FONT);
this.component=this;
}
else
{
setBaekground(DEFAULT_BACKGROUND);
setForeground(component.getForeground());
setFont(component.getFont());
this.component=component;
}
componentFontHeight=component.getFontMet-
rics(component.getFont()).getHeight();
componentFontAscent=component.getFontMet-
ries(component.getFont()).getAscent();
setPreferredWidth(9999);
}
public void setPreferredWidth(int row)
{
int width=fontMetrics.stringWidth(String.val-
ueOf(row));
if(currentRowWidth<;width)
{
currentRowWidth=width;
setPreferredSize(new Dfimension(2 * MARGIN
+width,HEIGHT));
}
}
public void setFont(Font font)
{
super.setFont(font);
fontMetrics=getFontMetrics(getFont());
}
public int getLineHeight()
{
if(hneHeight= =0)
return componentFontHeight;
else
return lineHeight;
}
public void setLineHeight(int lineHeight)
{
if(hneHeight>;0)
this.lineHeight=lineHeight;
}
public int getStartOffset()
{
return component.getlnsets().top+component-
FontAscent;
}
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/line-
Height)+1;
int endLineNUmber = startLineNumber+
(drawHere.height/lineHeight);
int start=(drawHere.Y/hneHeight)*line-
Height+startOffset;
for(int i=startLineNumber;i<;=endLineN-
umber;i++)
{
String lineNumber=String.valueOf(i);
int width=fontMetrics.stringWidth(lineNumber
);
g.drawstring(lineNumber,MARGIN+current-
RowWidth-width,start);
start+=lineHeight:
}
setPreferredWidth(endLineNumber);
}
}