Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用分页在JTextField中查看文件-Java_Java_File_Pagination - Fatal编程技术网

使用分页在JTextField中查看文件-Java

使用分页在JTextField中查看文件-Java,java,file,pagination,Java,File,Pagination,我可以加载一个文件(例如.txt)并在JTextArea中查看其内容。这适用于小文件。如果文件太大,这是一个问题,我想使用带有两个jbutton(prev,next)的分页,并在文件中导航 有什么建议吗?解决这个问题的一个非常简单的方法是将文本区域添加到滚动窗格 例如: JTextArea text = new JTextArea(); JScrollPane scrollpane = new JScrollPane(text); frame.add(scrollpane); 使用JTextP

我可以加载一个文件(例如.txt)并在JTextArea中查看其内容。这适用于小文件。如果文件太大,这是一个问题,我想使用带有两个jbutton(prev,next)的分页,并在文件中导航


有什么建议吗?

解决这个问题的一个非常简单的方法是将
文本区域
添加到
滚动窗格

例如:

JTextArea text = new JTextArea();
JScrollPane scrollpane = new JScrollPane(text);
frame.add(scrollpane);

使用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;
                    }
                }
            }
        }
    }
}
/**
*
*@作者索尼
*/
导入javax.swing.text.*;
导入javax.swing.*;
导入java.awt.*;
公共类CetiEditorKit扩展了StyledEditorKit{
受保护静态最终整版页宽=400;
受保护静态最终整版页高=612;
受保护的静态最终整型页面_INSET=20;
受保护静态最终插页页边=新插页(10,10,10,10);
受保护的静态最终颜色背景=Color.GRAY;
私营工厂;
公共静态void main(字符串[]args){
JTextPane=新的JTextPane();
setEditorKit(新的CetiEditorKit());
JPanel jpan=newjpanel(newborderlayout());
jpan.add(新的JScrollPane(pane));
JFrame=新JFrame();
frame.getContentPane().add(jpan);
帧设置大小(1024800);
frame.show();
}
公共CetiEditorKit(){
工厂=新分页视图工厂();
}
公共ViewFactory getViewFactory(){
返回工厂;
}
受保护的整型calculatePageBreak(整型页码){
int pageBreak=(页码*页高)-页码插入-页码页边距.bottom;
返回分页符;
}
私有类PagingViewFactory实现ViewFactory{
公共视图创建(元素元素){
字符串种类=elem.getName();
如果(种类!=null){
if(kind.equals(AbstractDocument.ContentElementName)){
返回新的LabelView(elem);
}else if(kind.equals(AbstractDocument.ParagraphElementName)){
返回新的分页段落视图(elem);
}else if(kind.equals(AbstractDocument.SectionElementName)){
返回新的剖视图(图元、视图Y_轴);
}else if(kind.equals(StyleConstants.ComponentElementName)){
返回新组件视图(elem);
}else if(kind.equals(StyleConstants.IconElementName)){
返回新的IconView(元素);
}
}
//默认为文本显示
返回新的LabelView(elem);
}
}
私有类SectionView扩展了BoxView{
专用int页码;
/**
*从跨越提供的轴的图元创建视图
*@param元素
*@param轴
*/
公共剖面视图(图元,int轴){
超级(元素,轴);
设置插图((短)(0),
(短)(第页插图+第页页边空白。左),
(短)(0),
(短)(第_页插图+第_页页边空白。右));
}
受保护的空心布局(内部宽度、内部高度){
宽度=页码宽度-2*页码插图-页码页边距.left-页码页边距.right;
超级布局(宽度、高度);
}
公共浮点getPreferredSpan(int轴){
浮动范围=0;
如果(轴==视图X_轴){
span=页面宽度;
}否则{
span=页码*页码高度;
}
返回跨度;
}
公共浮点getMinimumSpan(int轴){
返回getPreferredSpan(轴);
}
公共浮点getMaximumSpan(整数轴){
返回getPreferredSpan(轴);
}
受保护的空心布局主轴线(int targetSpan、int轴、int[]偏移量、int[]跨距){
超级布局主轴线(目标跨度、轴、偏移、跨度);
int TOTALPOFFSET=页\插图+页\页边距.top;
分页符;
页码=1;
分页段落视图;
对于(int i=0;ipageBreak){
布局(view.getWidth(),getHeight());
pageNumber=view.getEndPage();
span[i]+=view.getAdjustedSpan();
}
总偏移量=偏移量[i]+跨距[i];
}
}
公共空隙涂料(图形g,形状a){
超级油漆(g,a);
矩形alloc=(矩形的实例)?(矩形)a:a.getBounds();
矩形页面=新矩形(alloc.x、alloc.y、页面宽度、页面高度);
对于(int i=0;i(page.y+page.height)){
g、 设置颜色(背景);
g、 fillRect(第x页,第y页+第页高度,第页宽度,所有高度-第页高度);
}
}
专用void paintPageFrame(图形g,矩形页面){
Color oldColor=g.getColor();
//边界
g、 设置颜色(背景);
g、 fillRect(第x页、第y页、第宽度、第_页插图);
g、 fillRect(第x页、第y页、第_页插图、第.页高度);
g、 fillRect(page.x,page.y+page.height-page\u INSET,page.width,page\u INSET);
g、 fillRect(page.x+page.width-page_INSET,page.y,page_INSET,pa