【问题标题】:JPanel Not Stretching To Fill Screen [closed]JPanel 未拉伸以填充屏幕 [关闭]
【发布时间】:2013-11-07 05:23:36
【问题描述】:

所以我正在尝试制作一个面板,它可以跨越我的整个程序,并且具有大约 20 像素的高度。由于某种原因,只有一个小盒子是面板。谁能告诉我为什么它没有延伸到整个宽度?我对 Java 还很陌生,如果代码有点混乱/错误,请见谅。

GridPane 类

public GridPane() {
  gbc = new GridBagConstraints();
  gbc.anchor = GridBagConstraints.NORTH;
  gbc.weighty = 1.0;
  horizontalGrid = new JButton();
  horizontalGrid.setText("Horizontal Grid");
  options = new JPanel(new BorderLayout());
  options.setBackground(Color.black);
  options.add(horizontalGrid);
  add(options, gbc);
}

游戏类

public class Game extends JPanel implements ActionListener{
   //DECLARATIONS
private static final long serialVersionUID = 1L;
    public GridPane grid = new GridPane();
//DECLARATIONS END
//CONSTRUCTOR
public Game() {
    this.add(grid);     
}
//CONSTRUCTOR ENDS
//MAIN METHOD
public static void main(String[] args) {
    JFrame frame = new JFrame();
    Game game = new Game();
    frame.setResizable(true);
    frame.setAlwaysOnTop(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(game);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
//MAIN METHOD ENDS
//ACTION METHOD
public void actionPerformed(ActionEvent e) {
}

【问题讨论】:

  • 您设置了weighty,但您似乎缺少了一些东西。 ... weight? 也许?
  • 好的,直截了当——你在哪里设置weightx
  • 我是在weighty之后直接做了,但没有修复太多。
  • @DennisMills:在哪里——我没看到。

标签: java swing jframe jpanel layout-manager


【解决方案1】:

我修好了!!事实证明,我们需要用以下代码替换 GridPane 构造函数:

setBackground(Color.black);

add(horizontalGrid);

我不完全确定为什么,但它确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    相关资源
    最近更新 更多