java窗口按钮位置设置

时间:2021-07-18 09:08:30

java窗口按钮位置设置

代码如下:

package Day08;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Homework2 extends JFrame {
public Homework2() {
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout(5, 5));
p1.add(new JButton("JButton1-1"), BorderLayout.SOUTH);
p1.add(new JButton("JButton1-2"), BorderLayout.NORTH);
p1.add(new JButton("JButton1-3"), BorderLayout.EAST);
JPanel p2 = new JPanel();
p2 .setLayout(new BorderLayout(5, 5));
p2.add(new JButton("JButton2-1"), BorderLayout.SOUTH);
p2.add(new JButton("JButton2-2"), BorderLayout.NORTH);
p2.add(new JButton("JButton2-3"), BorderLayout.EAST);
setLayout(new BorderLayout(10, 10));
add(p1, BorderLayout.SOUTH);
add(p2, BorderLayout.NORTH);
}

public static void main(String[] args) {
Homework2 frame = new Homework2();
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}