简体   繁体   English

需要添加滚动条

[英]Need to add Scroll Bar

I am an enthusiast rather than a programmer. 我是一个发烧友,而不是程序员。 I have thousands of pdf files to go through and extract some data. 我有成千上万个pdf文件需要提取并提取一些数据。 The data is in X inside boxes (I suspect a graphic within the pdf) there is no evidence of this data if I convert the PDF to text so... I am converting the PDF to image then looking at the image in certain areas where I expect the X to be and counting black pixels, so far so good. 数据在X框内(我怀疑是pdf内有图形),如果将PDF转换为文本,则没有任何证据,因此...将PDF转换为图像,然后在某些区域查看图像我希望X能够算出黑色像素,到目前为止一切顺利。 The PDF does not fit in the window height-wise so I need to add a scrollbar. PDF不适合高度窗口,因此我需要添加滚动条。

I don't understand how I can add a scrollbar to the main window. 我不明白如何在主窗口中添加滚动条。

Can someone just steer me in the right direction 有人可以指引我正确的方向吗

Thank you 谢谢

The code is incomplete but working: 代码不完整,但可以正常工作:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class ImageViewer {
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable()
        {
            public void run(){


                ImageFrame frame = new ImageFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        }
        );
    }
}

class ImageFrame extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public ImageFrame(){
        setTitle("Image Viewer");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        ImageComponent component = new ImageComponent();
        add(component);        

    }

    public static final int DEFAULT_WIDTH = 860;
    public static final int DEFAULT_HEIGHT = 1000;
}


class ImageComponent extends JComponent{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Image image;
    public ImageComponent(){
        try{
            File image2 = new File("Files/4.jpg");
            image = ImageIO.read(image2);

        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
    public void paintComponent (Graphics g){
        if(image == null) return;
        int imageWidth = image.getWidth(this);
        int imageHeight = image.getHeight(this);


        g.drawImage(image, 0, 0, this);

        // Draw on the BufferedImage via the graphics context.
        g.setColor(Color.RED);
        g.drawRect(445, 153, 20, 20);
        g.drawRect(552, 153, 20, 20);
        g.drawRect(661, 153, 20, 20);
        g.drawRect(445, 182, 20, 20);
        g.drawRect(552, 182, 20, 20);
        g.drawRect(661, 182, 20, 20);
        g.drawRect(445, 226, 20, 20);
        g.drawRect(552, 226, 20, 20);
        g.drawRect(661, 226, 20, 20);
        g.drawRect(445, 271, 20, 20);
        g.drawRect(552, 271, 20, 20);
        g.drawRect(661, 271, 20, 20);


        for (int i = 0; i*imageWidth <= getWidth(); i++)
            for(int j = 0; j*imageHeight <= getHeight();j++)
                if(i+j>0) g.copyArea(0, 0, imageWidth, imageHeight, i*imageWidth, j*imageHeight);

        //Count black pixels to see if the box contains an X
        int A1 = 0;
        for (int y = 153; y < 173; y++)
        {
            for (int x = 445; x < 465; x++)
            {
                int c = ((BufferedImage) image).getRGB(x,y);
                Color color = new Color(c);
                if (c != -1)
                {
                    A1++;
                }
            }
        }
        System.out.println("First box pixel count = " + A1);

        //Count black pixels to see if the box contains an X
        int A2 = 0;
        for (int y = 153; y < 173; y++)
        {
            for (int x = 552; x < 572; x++)
            {
                int c = ((BufferedImage) image).getRGB(x,y);
                if (c != -1)
                {
                    A2++;
                }
            }
        }
        System.out.println("Second box pixel count = " + A2);

        //Count black pixels to see if the box contains an X
        int A3 = 0;
        for (int y = 153; y < 173; y++)
        {
            for (int x = 661; x < 681; x++)
            {
                int c = ((BufferedImage) image).getRGB(x,y);
                if (c != -1)
                {
                    A3++;
                }
            }
        }
        System.out.println("Third box pixel count = " + A3);

    }

}

寻找类似JScrollPane myPane = new JScrollPane();的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM