所以我正在尝试制作一个面板,它可以跨越我的整个程序,并且具有大约 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) {
}