我可以加载文件(例如 .txt)并在 JTextArea 中查看其内容。这适用于一个小文件。如果文件太大这是一个问题,我想使用带有两个 Jbuttons(上一个,下一个)的分页并浏览文件。
有什么建议吗?
我可以加载文件(例如 .txt)并在 JTextArea 中查看其内容。这适用于一个小文件。如果文件太大这是一个问题,我想使用带有两个 Jbuttons(上一个,下一个)的分页并浏览文件。
有什么建议吗?
使用 JTextPane 而不是 JTextArea 并尝试此代码在您的项目中实现
/**
*
* @author sony
*/
import javax.swing.text.*;
import javax.swing.*;
import java.awt.*;
public class CetiEditorKit extends StyledEditorKit {
protected static final int PAGE_WIDTH = 400;
protected static final int PAGE_HEIGHT = 612;
protected static final int PAGE_INSET = 20;
protected static final Insets PAGE_MARGIN = new Insets(10, 10, 10, 10);
protected static final Color BACKGROUND = Color.GRAY;
private ViewFactory factory;
public static void main(String[] args) {
JTextPane pane = new JTextPane();
pane.setEditorKit(new CetiEditorKit());
JPanel jpan = new JPanel(new BorderLayout());
jpan.add(new JScrollPane(pane));
JFrame frame = new JFrame();
frame.getContentPane().add(jpan);
frame.setSize(1024, 800);
frame.show();
}
public CetiEditorKit() {
factory = new PagingViewFactory();
}
public ViewFactory getViewFactory() {
return factory;
}
protected int calculatePageBreak(int pageNumber) {
int pageBreak = (pageNumber * PAGE_HEIGHT) - PAGE_INSET - PAGE_MARGIN.bottom;
return pageBreak;
}
private class PagingViewFactory implements ViewFactory {
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new LabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new PagingParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new SectionView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
private class SectionView extends BoxView {
private int pageNumber;
/**
* Creates a view from an element that spans the supplied axis
* @param element
* @param axis
*/
public SectionView(Element element, int axis) {
super(element, axis);
setInsets((short) (0),
(short) (PAGE_INSET + PAGE_MARGIN.left),
(short) (0),
(short) (PAGE_INSET + PAGE_MARGIN.right));
}
protected void layout(int width, int height) {
width = PAGE_WIDTH - 2 * PAGE_INSET - PAGE_MARGIN.left - PAGE_MARGIN.right;
super.layout(width, height);
}
public float getPreferredSpan(int axis) {
float span = 0;
if (axis == View.X_AXIS) {
span = PAGE_WIDTH;
} else {
span = pageNumber * PAGE_HEIGHT;
}
return span;
}
public float getMinimumSpan(int axis) {
return getPreferredSpan(axis);
}
public float getMaximumSpan(int axis) {
return getPreferredSpan(axis);
}
protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
super.layoutMajorAxis(targetSpan, axis, offsets, spans);
int totalOffset = PAGE_INSET + PAGE_MARGIN.top;
int pageBreak;
pageNumber = 1;
PagingParagraphView view;
for (int i = 0; i < offsets.length; i++) {
offsets[i] = totalOffset;
pageBreak = calculatePageBreak(pageNumber);
view = (PagingParagraphView) getView(i);
view.setPargraphOffset(totalOffset);
view.setStartPage(pageNumber);
if ((spans[i] + offsets[i]) > pageBreak) {
view.layout(view.getWidth(), getHeight());
pageNumber = view.getEndPage();
spans[i] += view.getAdjustedSpan();
}
totalOffset = offsets[i] + spans[i];
}
}
public void paint(Graphics g, Shape a) {
super.paint(g, a);
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle) a : a.getBounds();
Rectangle page = new Rectangle(alloc.x, alloc.y, PAGE_WIDTH, PAGE_HEIGHT);
for (int i = 0; i < pageNumber; i++) {
page.y = alloc.y + PAGE_HEIGHT * i;
if (page.intersects(alloc)) {
paintPageFrame(g, page);
}
}
// Fills in any unpainted areas
if ((alloc.y + alloc.height) > (page.y + page.height)) {
g.setColor(BACKGROUND);
g.fillRect(page.x, page.y + page.height, page.width, alloc.height - page.height);
}
}
private void paintPageFrame(Graphics g, Rectangle page) {
Color oldColor = g.getColor();
//borders
g.setColor(BACKGROUND);
g.fillRect(page.x, page.y, page.width, PAGE_INSET);
g.fillRect(page.x, page.y, PAGE_INSET, page.height);
g.fillRect(page.x, page.y + page.height - PAGE_INSET, page.width, PAGE_INSET);
g.fillRect(page.x + page.width - PAGE_INSET, page.y, PAGE_INSET, page.height);
//frame
g.setColor(Color.black);
g.drawRect(page.x + PAGE_INSET,
page.y + PAGE_INSET,
page.width - 2 * PAGE_INSET,
page.height - 2 * PAGE_INSET);
//shadow
g.fillRect(page.x + page.width - PAGE_INSET,
page.y + PAGE_INSET + 4,
4,
page.height - 2 * PAGE_INSET);
g.fillRect(page.x + PAGE_INSET + 4,
page.y + page.height - PAGE_INSET,
page.width - 2 * PAGE_INSET,
4);
g.setColor(oldColor);
}
}
private class PagingParagraphView extends ParagraphView {
private int startPage, endPage;
private int adjustedSpan;
private int paragraphOffset;
public PagingParagraphView(Element elem) {
super(elem);
startPage = 1;
endPage = 1;
adjustedSpan = 0;
paragraphOffset = 0;
}
public void setStartPage(int sp) {
startPage = sp;
}
public int getEndPage() {
return endPage;
}
public int getAdjustedSpan() {
return adjustedSpan;
}
public void setPargraphOffset(int po) {
paragraphOffset = po;
}
public void layout(int width, int height) {
super.layout(width, height);
}
protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
super.layoutMajorAxis(targetSpan, axis, offsets, spans);
if (paragraphOffset != 0) {
endPage = startPage;
int relativeBreak = calculatePageBreak(endPage) - paragraphOffset;
int correctedOffset;
adjustedSpan = 0;
for (int i = 0; i < offsets.length; i++) {
// determine the location of the page break
if (offsets[i] + spans[i] > relativeBreak) {
correctedOffset = relativeBreak
+ PAGE_MARGIN.bottom + (2 * PAGE_INSET) + PAGE_MARGIN.top
- offsets[i];
for (int j = i; j < offsets.length; j++) {
offsets[j] += correctedOffset;
}
adjustedSpan += correctedOffset;
endPage++;
relativeBreak = calculatePageBreak(endPage) - paragraphOffset;
}
}
}
}
}
}
它没有 NEXT 和 PREV 按钮,但它会给你在 JTextPane 中的分页
解决此问题的一个非常简单的方法是将您简单地添加TextArea
到ScrollPane
.
例如:
JTextArea text = new JTextArea();
JScrollPane scrollpane = new JScrollPane(text);
frame.add(scrollpane);