이미 답변을 받았지만 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을 사용하면 길고 비밀스러운 찾는 방법 서명을 해독하는 데 도움이됩니다.
이 독점 보드 예제에는 세 가지 주요 부분이 있습니다. 레이아웃 설정, JPanels로 Large Middle Piece 추가, JPanels로 외부 사각형 추가 등이 있습니다.
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();
}
}
}
여기에서 패널을 고정하는 구조를 추가 한 다음 버튼과 원하는 패널을 각각 추가 할 수 있습니다. 버튼이 패널 대신 작동합니다. 이것은 올바른 수입과 함께 컴파일해야하므로 컴파일하고 시험해보십시오.
투표 해 주셔서 감사합니다. 희망을 도왔습니다. – jjnguy