在面板中重新排列按钮和 jlist

Rearranging buttons and jlist in a panel

我创建了按钮和一个 jlist。我所有的按钮都在另一个名为 buttonPanels 的面板中,我想将该面板和 jList 添加到另一个名为 jPanelSouth 的面板中。我尝试使用 ______.setLocation(x,y) 定位我的按钮,但按钮似乎没有在面板内的任何位置移动(与 jlist 相同)。

 //southPanel and stuff
  jPanelSouth = new JPanel();
  jPanelSouth.setBorder(BorderFactory.createLineBorder(Color.black, 5));

  //buttons
  buttonPanel = new JPanel(new GridLayout(3,1));
  drawCardBtn = new JButton("Draw Card");
  moveBtn = new JButton("Move");
  playCardBtn = new JButton("Play Card");


  buttonPanel.add(drawCardBtn);
  buttonPanel.add(moveBtn);
  buttonPanel.add(playCardBtn);

  roomList = new JList(roomNumbers);
  roomList.setVisibleRowCount(4);
  roomList.setFixedCellWidth(100);
  roomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


  jPanelSouth.add(buttonPanel);
  jPanelSouth.add(roomList);

这就是我的 window 现在的样子

这就是我想要的样子

我不太确定在这种情况下应该使用哪种布局

尝试在您的按钮中使用 setBounds( x, y, height, width)。

buttonPanel = newJPanel();
buttonPanel.setBounds(x, y, height, width);

您应该更改 jPanelSouth 中的布局。例如,尝试将此行添加到您的代码中。

jPanelSouth.setLayout(new BoxLayout(jPanelSouth, BoxLayout.Y_AXIS));

详细了解秋千布局 here