Swing - 不使用滚动条滚动JFrame内容

时间:2012-02-29 11:14:16

标签: java swing scroll jframe

我有以下问题。有没有办法滚动Jframe内容而不使用滚动条,只是在代码中以编程方式进行。我内置了Japplet,我找不到滚动内容而不显示滚动的方法。整个滚动操作不应该在用户操作上执行,而是在我的线程想要执行时。等待帮助,谢谢。

我找不到任何办法。我试图将我的组件(Applet)添加到Jscrollpane,然后添加到jframe,但是当只显示白屏时,它会导致情况。

JFrame类:

public class SimulationFrame extends JFrame {

    private SimulationWindow simulationWindow;

    public SimulationFrame() throws HeadlessException {
        super(PropertiesHelper.getWindowTitle());

        simulationWindow = new SimulationWindow();
        JScrollPane scrollPane = new JScrollPane(simulationWindow);
        this.getContentPane().add(scrollPane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }
}

的JComponent:

    public SimulationWindow() {

    setLayout(new BorderLayout());

    graph = GraphHelper.provideGraphInstance();

    Dimension layoutSize = new Dimension(PropertiesHelper.getGraphHolderWidth(),
                                         PropertiesHelper.getGraphHolderHeight());
    graphLayout = new StaticLayout<Checkpoint, Route>(graph, new CheckpointPositionTransformer());
    graphLayout.setSize(layoutSize);

    visualizationViewer = new VisualizationViewer<Checkpoint, Route>(graphLayout, new Dimension(
            PropertiesHelper.getWindowWidth(), PropertiesHelper.getWindowHeight()));

    visualizationViewer.getRenderContext().setVertexLabelTransformer(new CheckpointLabelTransformer());
    visualizationViewer.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
    visualizationViewer.getRenderContext().setVertexFillPaintTransformer(new CheckpointColorTransformer());
    visualizationViewer.getRenderContext().setEdgeDrawPaintTransformer(new RouteColorTransformer());
    visualizationViewer.getRenderContext().setEdgeLabelTransformer(new RouteLabelTransformer());

    final ImageIcon mapBackground = createMapBackground();

    if (mapBackground != null) {
        mapBackgroundImagePaintable = new BackgroundImagePaintable(visualizationViewer, mapBackground);
        visualizationViewer.addPreRenderPaintable(mapBackgroundImagePaintable);
    }
    add(visualizationViewer);
    scrollRectToVisible(new Rectangle(1000,100));

}

VisualizationViewer是一个扩展JPanel的类。在此构造函数中放置scrollRectToVisible不起作用。

任何提示?也许这个实现是错误的,其中Jcomponent包含Jpanel本身?

1 个答案:

答案 0 :(得分:3)

使用方法

public void scrollRectToVisible(Rectangle aRect)
JScrollPane中添加的JComponent的

相关问题