我正在嘗試調整我的中心面板的大小,BorderLayout但大小沒有改變。它不斷填充可用的框架的其余部分。我嘗試設定首選大小,但沒有效果。我想要框架的大小,但只需要中心的一部分實際上是一個面板供以后使用。
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class Gui extends JFrame {
Border blackline = BorderFactory.createLineBorder(Color.black);
private JPanel board;
private JPanel buttons;
private JButton setMissing, by4, by8;
public Gui(){
setUpGui();
}
public void setUpGui(){
this.setSize(1000,1000);
this.setTitle("Comp361 Assignment One");
addButtons();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board = new JPanel();
board.setPreferredSize(new Dimension(400,400));
this.add(board, BorderLayout.CENTER);
//Sboard.setBackground(Color.Gray);
board.setBorder(blackline);
this.setVisible(true);
}
public void addButtons(){
buttons = new JPanel(new FlowLayout());
setMissing = new JButton("Set X");
by4 = new JButton("4 by 4");
by8 = new JButton("8 by 8");
buttons.add(setMissing);
buttons.add(by4);
buttons.add(by8);
this.add(buttons,BorderLayout.PAGE_START);
}
public static void main (String[] args){
new Gui();
}
}
uj5u.com熱心網友回復:
對于中央面板周圍的額外填充,您可以將它放在一個面板GridBagLayout(沒有約束)以使其居中,然后將 GBL 面板添加CENTER到BorderLayout.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/443878.html
