당신이 널 (null) 레이아웃을 시도하지 않은 경우, 다음
public static void main(String[] args) {
SwingUtilities.invokeLater(NullLayout::new);
}
NullLayout() {
JFrame frame = new JFrame("Basket Game");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
for (int i = 0; i < 4; i++) {
JPanel strip = new JPanel();
strip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 50));
strip.setBorder(BorderFactory.createTitledBorder("Strip " + i));
strip.add(new JLabel("Strip " + i));
mainPanel.add(strip);
}
JPanel gamearea = new JPanel();
gamearea.setLayout(null);
mainPanel.add(gamearea);
for (int i = 0; i < 5; i++) {
int x = i * 100, y = i * 100;
JPanel basket = new JPanel();
basket.setSize(200, 50);
basket.setLocation(x, y);
basket.setBackground(Color.YELLOW);
basket.add(new JLabel("x = " + x + ", y = " + y));
gamearea.add(basket);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.pack();
frame.setResizable(false);
frame.setSize(600, 600);
frame.setVisible(true);
}
} ([박스 레이아웃에있는 구성 요소의 설정 사용자 지정 위치]의
가능한 중복 HTTPS 도움이 될 수 있습니다,이 코드를 체크 아웃 : //stackoverflow.com/questions/36227097/set-custom-location-for-a-component-in-box-layout) – ParkerHalo
[레이아웃 관리자 사용 방법] (https://docs.oracle.com/)으로 시작하십시오. javase/tutorial/uiswing/layout/layoutlist.html) - 픽셀 완벽한 (또는 null) 레이아웃이 더 쉽고, 장황하지 않다는 생각에 사로 잡히지 마십시오. 레이아웃 관리자와 자신을 저장 머리가 아프다. – MadProgrammer