同樣在你前面的問題說,一個辦法做到這一點是嵌入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