【问题标题】:Rearranging buttons and jlist in a panel在面板中重新排列按钮和 jlist
【发布时间】:2015-10-10 21:33:14
【问题描述】:

我创建了按钮和 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);

这就是我的窗口现在的样子

这就是我想要的样子

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

【问题讨论】:

  • 你可以使用 GridBagLayout
  • 谢谢,这真的很有帮助。我什至不知道 GridBagLayout

标签: java swing layout layout-manager panels


【解决方案1】:

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

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

阅读更多关于秋千布局的信息here

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2015-06-28
    • 2010-11-01
    • 1970-01-01
    相关资源
    最近更新 更多