Swing:创建一个显示其组件居中的JScrollPane? [英] Swing: creating a JScrollPane that displays its component centered?

查看:88
本文介绍了Swing:创建一个显示其组件居中的JScrollPane?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果创建一个视口大于JScrollPane组件的JScrollPane,它会在左上角显示该组件。

If you create a JScrollPane that has a viewport larger than the JScrollPane's component, it displays that component in the upper left.

有没有办法改变这种行为所以它显示组件居中?

Is there any way to change this behavior so it displays the component centered?

下面的示例程序。

澄清

我的组件有(宽度,高度)=(cw,ch)。

I have a component which has (width, height) = (cw,ch).

我有一个带有视口的JScrollPane,其宽度,高度=(vw,vh)。

I have a JScrollPane with a viewport which has (width, height) = (vw, vh).

组件可能会变大或变小。我想要一种方法来使用滚动条相对于视口的中心点定位组件的中心点,因此如果组件的一个或两个维度小于视口,则该组件将显示在视口的中心。

The component may become larger or smaller. I would like a way to use the scrollbars to position the component's centerpoint relative to the viewport's center point, so if the one or both of the component's dimensions are smaller than the viewport, the component shows up in the center of the viewport.

默认的滚动窗格行为将组件的左上角相对于视口的左上角定位。

The default scrollpane behavior positions the component's upper left corner relative to the viewport's upper left corner.

我要问的是如何更改参考点。我不确定这对于默认的JScrollPane有多容易,所以如果这不容易,那么我将学习我能做什么并考虑不同的方法。

All I'm asking is how to change the reference point. I am not sure how easy this is to do with the default JScrollPane, so if it's not easy, then I'll learn what I can and think of a different approach.

package com.example.test.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SimpleScroller extends JFrame {
    static class Thingy extends JPanel
    {
        private double size = 20.0;

        @Override public Dimension getPreferredSize() {
            int isize = (int) this.size;
            return new Dimension(isize, isize);
        }
        @Override protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            int[] x = {0, 100, 100, 0, 0, 75, 75, 25, 25, 50};
            int[] y = {0, 0, 100, 100, 25, 25, 75, 75, 50, 50};

            Graphics2D g2d = (Graphics2D) g;
            AffineTransform at0 = g2d.getTransform();
            g2d.scale(size/100, size/100);
            g.drawPolyline(x, y, x.length);
            g2d.setTransform(at0);
        }
        public void setThingySize(double size) { 
            this.size = size;
            revalidate();
            repaint();
        }
        public double getThingySize() { return this.size; }
    }

    public SimpleScroller(String title) { 
        super(title);
        final Thingy thingy = new Thingy();
        setLayout(new BorderLayout());      
        add(new JScrollPane(thingy), BorderLayout.CENTER);

        final SpinnerNumberModel spmodel = 
            new SpinnerNumberModel(thingy.getThingySize(),
                10.0, 2000.0, 10.0);
        spmodel.addChangeListener(new ChangeListener() {
            @Override public void stateChanged(ChangeEvent e) {
                thingy.setThingySize((Double) spmodel.getNumber());
            }           
        });
        add(new JSpinner(spmodel), BorderLayout.NORTH);
    }
    public static void main(String[] args) {
        new SimpleScroller("simple scroller").start();
    }
    private void start() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}


推荐答案


  1. JPanel 放入滚动窗格

  2. 将面板的布局设置为 GridBagLayout

  3. 将一个组件放入面板中,没有约束。它将居中。

  1. Put a JPanel into the scroll-pane
  2. Set the layout of the panel to a GridBagLayout.
  3. Put one component into the panel with no constraint. It will be centered.

这是嵌套布局示例,它将红色/橙色图像放置在父级的中心。

This is the technique used in the Nested Layout Example, which places the red/orange image into the center of the parent.

这篇关于Swing:创建一个显示其组件居中的JScrollPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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