java窗口按钮设置五个方向
代码如下:
package Day08; import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class ShowBorderLayout extends JFrame{
public ShowBorderLayout() {
// 设置BorderLayout的水平间距为5,垂直间距为10
setLayout(new BorderLayout(5, 10));
add(new JButton("East"), BorderLayout.EAST);
add(new JButton("South"), BorderLayout.SOUTH);
add(new JButton("West"), BorderLayout.WEST);
add(new JButton("North"), BorderLayout.NORTH);
add(new JButton("Center"), BorderLayout.CENTER);
}
public static void main(String[] args){
ShowBorderLayout frame = new ShowBorderLayout();
frame.setSize(400, 300);//set the frame size
frame.setLocationRelativeTo(null);// center a frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// center a frame
frame.setVisible(true);//display the frame
}
}