我在业余时间一直在构建 Java 版本的 Monopoly,但在理解 Swing 的布局等方面遇到了一些麻烦。
我在板上的每个空间本质上都是一种习惯JButton
,我一直试图将它们放置在框架的边缘(就像垄断板本身一样)。我似乎找不到有关布局系统如何工作的有用解释,因此在这样做时遇到了麻烦。
有人可以给我一个例子,说明他们如何将按钮放在框架边缘?我应该使用不同的布局吗?
我在业余时间一直在构建 Java 版本的 Monopoly,但在理解 Swing 的布局等方面遇到了一些麻烦。
我在板上的每个空间本质上都是一种习惯JButton
,我一直试图将它们放置在框架的边缘(就像垄断板本身一样)。我似乎找不到有关布局系统如何工作的有用解释,因此在这样做时遇到了麻烦。
有人可以给我一个例子,说明他们如何将按钮放在框架边缘?我应该使用不同的布局吗?
这似乎最适合与BorderLayout
. 我建议创建JPanel
包含所有s 的 4 JButton
s。
然后JPanels
将BorderLayout.North
, South
,East
和West
.
在我看来,这可能是布局按钮的最简单方法。
这是开始使用BorderLayout
.
我只是把一些代码放在一起,可以帮助你开始布局。 它没有被编译。
int boardWidth;
int boardHeight;
int boardSquareHeight;
int boardSqusreWidth;
JPanel north = new JPanel();
JPanel south = new JPanel();
Dimension northSouthD = new Dimension(boardWidth, boardSquareHeight);
north.setPreferedSize(northSouthD);
south.setPreferedSize(northSouthD);
JPanel east = new JPanel();
JPanel west = new JPanel();
Dimension eastWestD = new Dimension(boardSquareHeight, boardHeight - 2 * boardSquaareWidth);
east.setPreferedSize(easWestD);
west.setPreferedSize(easWestD);
// add all of the buttons to the appropriate JPanel
parentPanel.setLayoutManager(new BorderLayout());
parentPanel.add(north, BorderLayour.NORTH);
...
我知道这已经得到了回答,但我觉得你应该看看 GridLayout 的工作原理。首先http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html和http://www.cs.ubc.ca/local/computing/software/jdk-1.5.0/ docs/api/java/awt/GridBagConstraints.html有助于破译长而神秘的方法签名。
这个垄断板示例包含三个主要部分。有布局的设置,将大中间块添加为 JPanel,将外部方块添加为 JPanel。
public class GridBagLayoutExample extends JFrame {
public static void main(String[] args) {
new GridBagLayoutExample().setVisible(true);
}
public GridBagLayoutExample() {
try {
//Setup the Layout
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
GridBagLayout thisLayout = new GridBagLayout();
thisLayout.rowWeights = new double[] { 0.2, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2 };
thisLayout.columnWeights = new double[] { 0.2, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2 };
getContentPane().setLayout(thisLayout);
//Default Grid values
int gridX = 0;
int gridY = 0;
//Add Panels for Each of the four sides
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 13; i++) {
JPanel tempPanel = new JPanel();
switch(j)
{
case 0://Top Spaces
gridX = i;
gridY = 0;
break;
case 1://Left Spaces
gridX = 0;
gridY = i;
break;
case 2://Right Spaces
gridX = 12;
gridY = i;
break;
case 3://Bottom Spaces
gridX = i;
gridY = 12;
break;
}
getContentPane().add(tempPanel,
new GridBagConstraints(gridX,// XGridSpot
gridY,// YGridSpot
1,// XGridSpaces
1,// YGridSpaces
0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH,//Fill
new Insets(0, 0, 0, 0), 0, 0));
tempPanel.setBorder(BorderFactory
.createLineBorder(Color.BLACK));
}
}
{// Main Inner Area Notice Starts at (1,1) and takes up 11x11
JPanel innerPanel = new JPanel();
getContentPane().add(
innerPanel,
new GridBagConstraints(1,
1,
11,
11,
0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
pack();
setSize(260, 260);
} catch (Exception e) {
e.printStackTrace();
}
}
}
从这里开始,如果您添加一个结构来容纳面板,然后您可以向每个面板添加按钮和任何您想要的东西。按钮也可以代替面板工作。这应该使用正确的导入进行编译,因此请编译并尝试一下。
您可以想象在 netbeans 之类的东西中设计布局,然后使用自动生成的代码(它从您构建的设计中生成间距)来布置您自己的电路板,只需复制自动生成的代码并替换为您的自定义按钮而不是其他任何东西您用作占位符的元素。
它将允许您根据相对间距将事物准确地放置在您想要的位置,而不是试图让 swing 弄清楚。
GridLayout
如果您将“内部”单元格留空,则标准可能会起作用。