天天看點

Panel in the Frame,手動居中

import java.awt.*;

public class TestCenterPanel {

	public static void main(String[] args) {
		new SelfFrame("FrameWithPanel", 50,50,400,400);

	}

}

class SelfFrame extends Frame{
	private Panel pn;
	SelfFrame(String str, int x, int y, int width, int height){
		super(str);
		super.setBackground(Color.blue);
		super.setVisible(true);
		super.setBounds(x, y, width, height);
		super.setLayout(null);
		
		pn = new Panel(null);
		pn.setBackground(Color.yellow);
		pn.setBounds((width-width/2)/2, (height-height/2)/2, width/2, height/2);
		
		super.add(pn);
	}
}