【问题标题】:My JscrollPane isn't scrolling (vertically) the entire image,我的 JscrollPane 没有(垂直)滚动整个图像,
【发布时间】:2013-12-22 08:43:37
【问题描述】:

当我增加preferredSize 时,会找到更多的Bufferedimage(在JPanel 上,在JFrame 上)。

图像的大小将根据我的程序正在读取的文件的大小而有所不同,

我设置滚动窗格的位置;

public Frame(String title, int theWidth, int theHeight) throws FileNotFoundException {

        super(String.format("Title", title));
        this.width = theWidth;
        this.height = theHeight;
        this.panel = new Panel(width, height);
        this.scroller = new JScrollPane(this.panel);
        this.panel.drawLinesAndTab();
        this.scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.scroller.setPreferredSize(new Dimension(this.width, this.height));
        this.getContentPane().add(this.scroller);
    }

还有(别开枪),这是我的 Panel 课;

public class Panel extends JPanel {

    private BufferedImage image;
    private ReadTabFile tab;
    private String title;
    private String theNotes;
    private String theFlag;
    private int a, b, c, x, y, beginBarlineX, beginBarlineY, endBarX, endBarY;
    private ArrayList list;
    private Font letterFont;
    private Font flagFont;
    private int width;
    private int height;
    boolean newLine = false;

    public Panel(int theWidth, int theHeight) throws FileNotFoundException {

        this.width = theWidth;
        this.height = theHeight;
        this.tab = new ReadTabFile("tabSource.txt");
        this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = this.image.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.image.getWidth(), this.image.getHeight());
        setBorder(BorderFactory.createLineBorder(Color.black));
        this.setFocusable(true);

    }

    public void drawLinesAndTab() {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        this.list = tab.readTabCode();
        this.a = 20;
        this.b = 100;
        this.c = 60;
        this.x = 40;
        this.y = 100;
        this.beginBarlineX = 20;
        this.beginBarlineY = 100;
        this.endBarY = 980;
        this.endBarY = 100;
        this.title = tab.getTitle();
        g.drawString(this.title, 40, 20);

        for (int i = 0; i < this.list.size(); i++) {
            Bar theBar = (Bar) this.list.get(i);
            drawBarline(a, b, a, b + 125);
            ArrayList<String> stuff = theBar.getLinesInBar();

            for (int j = 0; j < stuff.size(); j++) {
                String line = stuff.get(j);
                theFlag = line.substring(0, 1);
                theNotes = line.substring(1, line.length());
                if (newLine = true){
                    //make some bar lines.
                }

                try {

                    int w = this.image.getWidth();
                    //int h = this.image.getHeight();

                    if (c <= (w - 40)) {

                        newLine = false;
                        String zero = theFlag;
                        drawFlag(zero, x + 5, y - 20);

                        String one = theNotes.substring(0, 1);
                        g.drawLine(a, b, c, b);
                        drawLetter(one, x, y);

                        String two = theNotes.substring(1, 2);
                        drawLetter(two, x, y += 25);
                        g.drawLine(a, b += 25, c, b);

                        String three = theNotes.substring(2, 3);
                        drawLetter(three, x, y += 25);
                        g.drawLine(a, b += 25, c, b);

                        String four = theNotes.substring(3, 4);
                        drawLetter(four, x, y += 25);
                        g.drawLine(a, b += 25, c, b);

                        String five = theNotes.substring(4, 5);
                        drawLetter(five, x, y += 25);
                        g.drawLine(a, b += 25, c, b);

                        String six = theNotes.substring(5, 6);
                        drawLetter(six, x, y += 25);
                        g.drawLine(a, b += 25, c, b);
                        this.repaint();

                        b -= 125;
                        y -= 125;
                        x += 40;
                        a += 40;
                        c += 40;

                    } else {
                        newLine = true;
                        a = 20;
                        x = 20;
                        b += 225;
                        c = 60;
                        y += 225;
                        beginBarlineX = 20;
                        beginBarlineY += 100;
                        endBarX += 100;
                        endBarY = 100;
                        this.repaint();
                    }

                } catch (Exception ex) {
                    System.err.println(ex + " within Panel, drawtab for loop");
                }
            }
        }
    }

    public void drawBarline(int xTop, int yTop, int xBot, int yBot) {
        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        g.drawLine(xTop, yTop, xBot, yBot);

    }

    public Point makeBarline(int xTop, int yTop, int xBot, int yBot) {
        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        g.drawLine(xTop, yTop, xBot, yBot);
        return (new Point());
    }

    public Point drawLetter(String letter, int x, int y) throws FontFormatException, IOException {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        g.setFont(letterFont(letter).deriveFont(20.0f));
        g.drawString(letter, x, y);

        return (new Point());
    }

    public Point drawFlag(String letter, int x, int y) throws FontFormatException, IOException {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        g.setFont(flagFont(letter).deriveFont(30.0f));
        g.drawString(letter, x, y);

        return (new Point());
    }

    public Font letterFont(String fontString) throws FontFormatException, IOException {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);

        if (!Character.isDigit(fontString.charAt(0))) {
            this.letterFont = Font.createFont(Font.TRUETYPE_FONT, new File("LeRoy.ttf"));
            g.getFontMetrics(this.letterFont);
            g.setFont(this.letterFont);
            return this.letterFont;
        } else {
            return null;
        }
    }

    public Font flagFont(String fontString) throws FontFormatException, IOException {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);

        if (!Character.isDigit(fontString.charAt(0))) {
            this.flagFont = Font.createFont(Font.TRUETYPE_FONT, new File("LeroyLuteNotes1.ttf"));
            g.getFontMetrics(this.flagFont);
            g.setFont(this.flagFont);
            return this.flagFont;
        } else {
            return null;
        }

    }

    @Override
    public Dimension getPreferredSize() {
        return (new Dimension(this.image.getWidth(), this.image.getHeight()));
    }

    public BufferedImage getImage() {
        return this.image;
    }

    @Override
    public void paintComponent(Graphics graphics) {

        Graphics g = graphics.create();
        g.drawImage(this.image, 0, 0, null);
    }
}

我认为我设置正确,我没有使用布局管理器,因为我所做的基本上就是将线条和文本写入图像/面板。它工作正常,但我需要能够向下滚动到图像底部。

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: java swing jpanel jscrollpane preferredsize


    【解决方案1】:
    1. JScrollPane 尊重组件首选大小来设置它的内容视图,而不是组件的内容。
    2. 如果您希望滚动内容:图像,请覆盖 Panel 类的 getPreferreedSize(Dimension) 函数并将图像大小作为组件的首选大小返回。
    3. 我们应该始终在覆盖的 paintComponent(Graphics) 函数中调用 super.paintComponent(g),而您现在不这样做。

    【讨论】:

    • 我想我已经在 Panel 类中覆盖了 getPreferredSize(D),A Dimension 需要两个整数,对吧?我将其作为 this.image.getWidth() 和 this.image.getHeight() 传递。我还在 Frame 类中设置了 preferredSize。所以我仍然对为什么它没有滚动到底部感到困惑。
    • @juju,尝试不要将首选大小设置为JScrollPane,因为您将其添加到带有add(scrollPane) 的框架内容窗格中,这会将JScrollPane 添加到BorderLayout.CENTER。而只是在Frame() 构造函数的末尾调用pack()。试试看告诉我
    • 感谢您的帮助,this.pack() 似乎与 getPreferredSize() 完全相同,所以我认为它是另外一回事。现在有一个空指针在某处被抛出,所以测试时间。
    • 滚动条正在运行,感谢您的帮助。图像仍然只显示面板的大小。但是谢谢你的帮助:)
    猜你喜欢
    • 2012-11-08
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    相关资源
    最近更新 更多