将组件放入较小的组件内

时间:2017-06-03 17:29:01

标签: java swing layout jframe

我想要使用DrawCanvas(960x960)获取Frame(640x640),如下所示: enter image description here

因此必须隐藏DrawCanvas的部分并以帧为中心。我应该选择什么样的布局?现在我将DrawCanvas放在框架中:

    class extends JFrame {
        canvas = new DrawCanvas();
        canvas.setPreferredSize(new Dimension(960, 960));
        Container cp = getContentPane();
        cp.add(canvas);
        setPreferredSize(new Dimension(640, 640));
    }

1 个答案:

答案 0 :(得分:4)

再次如上一个问题中所述,一种方法是将内部组件嵌入JScrollPane中,但除去滚动条的滚动窗格。

例如,假设您有一个名为DrawPanel的类,它扩展了JPanel并保持了12 x 12网格的单元格,但您只想显示8 x 8个单元格。你可以让这个类实现Scrollable接口,让它的getPreferredScrollableViewportSize方法返回你想要显示的尺寸,而它的getPreferredSize方法将返回组件的实际尺寸:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Scrollable;

@SuppressWarnings("serial")
public class DrawPanel extends JPanel implements Scrollable {
    public static final int VISIBLE_ROW_COUNT = 8;
    private static final int ROW_COUNT = 12;
    private static final Color DARK_COLOR = Color.DARK_GRAY;
    private static final Color LIGHT_COLOR = Color.WHITE;
    private int cellWidth;

    public DrawPanel(int cellWidth) {
        super(new GridLayout(ROW_COUNT, ROW_COUNT));
        this.cellWidth = cellWidth;
        Dimension prefSize = new Dimension(cellWidth, cellWidth);
        for (int i = 0; i < ROW_COUNT; i++) {
            for (int j = 0; j < ROW_COUNT; j++) {
                JPanel cell = new JPanel();
                cell.setPreferredSize(prefSize);
                Color c = i % 2 == j % 2 ? DARK_COLOR : LIGHT_COLOR;
                cell.setBackground(c);
                add(cell);

                // TODO: delete the code below. For demo/debug purposes only
                String text = String.format("[%d, %d]", j, i);
                JLabel label = new JLabel(text);
                c = c == DARK_COLOR ? LIGHT_COLOR : DARK_COLOR;
                label.setForeground(c);
                cell.add(label);
                // TODO: end delete block
            }
        }
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        Dimension viewportSize = new Dimension(VISIBLE_ROW_COUNT * cellWidth, VISIBLE_ROW_COUNT * cellWidth);
        return viewportSize;
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation,
            int direction) {
        return cellWidth;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
            int direction) {
        return 1;
    }

    // note that getPreferredSize already returns the size of the 12 x 12 grid
    // since it is composed of a grid sized JPanels

}

然后,您可以将其放在JScrollPane中,

private static final int CELL_WIDTH = 80;
private DrawPanel drawPanel = new DrawPanel(CELL_WIDTH);
private JScrollPane drawPanelScrollPane = new JScrollPane(drawPanel); 

摆脱滚动条,

    drawPanelScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    drawPanelScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

并将其滚动到您想要左上角点的位置:

    int x = 2 * CELL_WIDTH;
    int y = x;
    Point pt = new Point(x, y);
    drawPanelScrollPane.getViewport().setViewPosition(pt);

示例GUI:

import java.awt.BorderLayout;
import java.awt.Point;

import javax.swing.*;

@SuppressWarnings("serial")
public class FrameAGrid extends JPanel {
    private static final int CELL_WIDTH = 80;
    private DrawPanel drawPanel = new DrawPanel(CELL_WIDTH);
    private JScrollPane drawPanelScrollPane = new JScrollPane(drawPanel); 

    public FrameAGrid() {
        drawPanelScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        drawPanelScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        setLayout(new BorderLayout());
        add(drawPanelScrollPane);

        int x = 2 * CELL_WIDTH;
        int y = x;
        Point pt = new Point(x, y);
        drawPanelScrollPane.getViewport().setViewPosition(pt);
    }


    private static void createAndShowGui() {
        FrameAGrid mainPanel = new FrameAGrid();

        JFrame frame = new JFrame("FrameAGrid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

显示为: enter image description here

请注意,左上角是2,2,而不是0,0