正如 brano88 所说,实现这一目标的最简单方法是使用BorderLayout。以下是您希望使用此布局的简短示例。与BorderLayout
JFrame 的默认布局管理器一样,您无需显式设置它,只需使用开箱即用的字段即可。
public class MyPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
public static void main(String[] args){
JFrame frame = new JFrame("My frame");
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//Button Panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton("Click me!"));
//Drawing panel
MyPanel panel = new MyPanel();
frame.add(panel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.NORTH);
frame.setVisible(true);
}
}