同样在你前面的问题说,一个办法做到这一点是嵌入JScrollPane中,内内组成部分,但摆脱滚动条的滚动窗格。
例如,假设您有一个名为DrawPanel的类,它扩展了JPanel并拥有一个12 x 12的网格单元格,但您只想显示8 x 8个单元格。可以使用这个类实现滚动界面,有其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);
,并滚动到SPO
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());
}
}
这将显示为:
注意,左上角是2,2不是你想要的左上点t在0,0
我认为你可以通过使用[Boxlayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html)来完成,用网格区域填充空格。 – Articuno