我想在我的JPanel上放2個JButton,其中一個居中,另一個在JPanel的右上角。 JPanel與包含JPanel的JFrame大小相同。我怎樣才能使用GridBagLayout和GridBagConstraints?如何將2個JButton放在JPanel頂部(一個居中,另一個右上角)?
public class MyPanel extends JPanel {
public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
c.weighty = 1;
c.gridx = 0;
add(btnGame1, c);
c.gridx = 1;
c.anchor = GridBagConstraints.FIRST_LINE_END;
add(btnExitFrame, c);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
MCVE爲camickr
public class MyPanel extends JPanel {
static JFrame frame = new JFrame();
public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");;
setLayout(new BorderLayout());
JPanel top = new JPanel(new FlowLayout(FlowLayout.RIGHT));
top.add(btnExitFrame);
JPanel center = new JPanel(new GridBagLayout());
center.add(btnGame1, new GridBagConstraints());
add(top, BorderLayout.PAGE_START);
add(center, BorderLayout.CENTER);
}
public static void main(String[] args) {
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
FIXED SOL'N:
public class MyPanel extends JPanel {
static JFrame frame = new JFrame();
public MyPanel() {
JButton btnGame1 = new JButton("Game 1");
JButton btnExitFrame = new JButton("Exit");;
int nOfCells = 3;
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
for(int i = 0; i < nOfCells; i++){
for(int j = 0; j < nOfCells; j++){
c.gridx = i;
c.gridy = j;
if(i == 1 && j == 0) {
c.anchor = c.PAGE_START;
add(btnGame1, c);
c.anchor = c.CENTER;
}
else if(i == 2 && j == 0) {
c.anchor = c.FIRST_LINE_END;
add(btnExitFrame, c);
c.anchor = c.CENTER;
}
else {
c.fill = c.BOTH;
add(Box.createRigidArea(new Dimension(frame.getWidth()/nOfCells, frame.getHeight()/nOfCells)), c);
c.fill = c.NONE;
}
}
}
}
public static void main(String[] args) {
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().add(new MyPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
我應該如何定義nOfCells? (我有2個按鈕放在框架中) – takanuva15
嵌套for循環在佈局上創建了一個Grid,每個方向都有nOfCells框。您選擇的數量取決於您希望Frame的外觀如何,想象一下將您的JFrame分成nOfCells^2個具有規定大小的盒子,並查看可能更美觀的數字。 – Bremsstrahlung
好吧,我刺了一個3 3,結果是時髦。您是否可以在原始響應下方粘貼一個代碼塊,以顯示如何編輯MyPanel構造函數以使「遊戲1」按鈕位於JFrame的頂部中心並且右上角的「退出」? – takanuva15