JScrollPane滚动条不可滚动 [英] JScrollPane scrollbars not scrollable

查看:497
本文介绍了JScrollPane滚动条不可滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类绘制一些非常简单的图形,如线条,圆形和矩形。线条是动态可扩展的,有时当它们超出分辨率时,如果没有滚动条,则无法看到。因此,我已经将JScrollPane添加到我的JFrame中,但遗憾的是,尽管已经调用了布局管理器,滚动条仍然无法滚动。

I have a class that draw some very simple graphics like lines, circles and rectangles. The lines are dynamically expandable and sometimes when they expand beyond the resolution, it is impossible to see without a scroll bar. Therefore, I've added JScrollPane to my JFrame but unfortunately, the scroll bar is not scrollable despite calling the Layout Manager already.

这就是我所拥有的:
- 绘制组件(线,矩形,圆圈)的类
- 设置JFrame / JScrollPane的类

Here's what I have: - A class that draws components (lines, rectangles, circles) - A class that sets up the JFrame/JScrollPane

这是我的GUI类的摘录代码:

Here's an excerpt code of my GUI class:

    JFrame frame = new JFrame("GUIFrame");
    frame.setLayout(new BorderLayout()); // Layout already set
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DrawComponent comp = new DrawComponent(); // Reference to class that draw components
    JScrollPane sp = new JScrollPane(comp, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    sp.setPreferredSize(new Dimension(1000, 1000));

    frame.add(sp, BorderLayout.CENTER);
    frame.setSize(500,500);
    frame.setVisible(true);

使用上面的代码,我有Java给我看一个带有包含我的jcomponents的滚动条的JFrame。我已将滚动条设置为始终如上所示,但它们不可滚动,灰色显示。

With the code above, I've got Java to show me a JFrame with scrollpane containing my jcomponents. I have set the scrollbars to always appear as shown above but they are not scrollable, gray-ed out.

正如安德鲁所建议的,我花了一些时间来创建一个SSCCE反映我正在尝试做的事情:

As suggested by Andrew, I took sometime to create a SSCCE to reflect what I'm trying to do:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;


public class DrawTest {
    public static void main(String[] args){
        JFrame frame = new JFrame("SSCCE");
        frame.setLayout(new BorderLayout());
        frame.setSize(1000, 1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

        DrawComp d = new DrawComp();
        JScrollPane sp = new JScrollPane(d, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        frame.add(sp);      
        frame.setVisible(true);
    }
}

class DrawComp extends JComponent {

    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D)g;

        Random ran = new Random();
        int ranNum = ran.nextInt(10);
        System.out.println(ranNum);
        double length = 100 * ranNum;
        g2.draw(new Line2D.Double(10, 10, length, length));
    }
}

上面的代码基于随机抽取对角线输入。我打算做的是,当线条长度超出帧大小时,我希望我能够滚动并查看整行。我再次将行组件添加到JScrollPane但它不能滚动。

The code above draws a diagonal line based on a random input. What I intend to do is that when the line gets so long that it goes out of the frame size, I hope that I'll be able to scroll and have a view of the full line. Again I have added the line component to JScrollPane but it's not scroll-able.

推荐答案

我道歉。您正在以正确的方式使用JScrollPane。我运行了你的代码,我想我得到了JScrollPane不工作的原因。想象一下,当你将jframe的背景设置为全红色时(通过在任何地方涂上一个红点),jscrollpane是否可以滚动?不,因为绘制背景颜色是在背景中。实际的VIEW没有变化,它不会比显示尺寸大,所以滚动窗格看不到滚动点。你的paint组件方法正在做类似的事情。它只是在后台绘制一些东西。实际的VIEW没有改变,所以scrollpane不会工作。

My apologies. You are using JScrollPane the right way. I ran your code and I think I got the reason why JScrollPane is not working. Picture this, when your set the jframe's background to all red (by paiting a red dot everywhere) should the jscrollpane be scrollable? No, because painting the background color is in the background. The actual VIEW isnt changing and it is not bigger then the display size so the scrollpane does not see the point of scrolling. Your paint component method is doing something similar. It is just drawing something in the background. The actual VIEW didnt change so scrollpane wont work.

public class DrawTest {
    public static void main(String[] args){
        JFrame frame = new JFrame("SSCCE");
        frame.setLayout(new BorderLayout());
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

        final DrawComp d = new DrawComp();
        final JScrollBar hbar,vbar;
        hbar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 500);
        vbar = new JScrollBar(JScrollBar.VERTICAL, 0, 1, 0, 500);

        frame.setLayout(null);
        frame.add(d);      
        frame.add(hbar);      
        frame.add(vbar);      
        d.setBounds(0, 0, 300, 300);
        vbar.setBounds(460, 0, 20, 480);
        frame.setVisible(true);

        vbar.addAdjustmentListener(new AdjustmentListener() 
        {
            public void adjustmentValueChanged(AdjustmentEvent e) 
            {
                d.setLocation(d.getX(), -vbar.getValue());
            }
        });
    }
}

以下是垂直滑动组件的代码。我对您现有的代码进行了一些更改。 DrawComp仍然是相同的

Here is the code for sliding a component vertically. I made some changes to your existing code. The DrawComp is still the same

这篇关于JScrollPane滚动条不可滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆