본문 바로가기

JAVA/UI

layout(버튼과 라벨 기본셋팅)

예시

main

package awtSample03;

public class mainClass {
	public static void main(String[] args) {
		
		new WindowTest();
	}
}

windowTest


public class WindowTest extends Frame implements WindowListener {

	public WindowTest() {
		super("Layout");
		//setLayout(new FlowLayout());// 맨 위에 일렬로 배치
		//setLayout(new GridLayout(3,1));//3행 1열
		setLayout(null);//사용 안하고 Label위치를 잡아줌
		
		//Label
		Label label1 = new Label("label 1");
		label1.setBackground(new Color(255, 0, 0));//빨간색
		label1.setBounds(150, 30, 50, 30);//위치 1,2,폭,넓이-> 각 객체의 위치설정
		add(label1);
		
		
		//Label
		Label label2 = new Label("label 2");
		label2.setBackground(Color.yellow);//고정된 색상사용
		label2.setBounds(150, 80, 70, 30);//위치 1,2,폭,넓이 //바 위치 포함 0,0
		add(label2);
		
		//Button
		Button button = new Button();
		button.setLabel("Button");
		button.setBounds(150, 150, 100, 30);//위치 1,2,폭,넓이

		add(button);
		
		setBounds(0, 0, 640, 480);
		setVisible(true);
		
		addWindowListener(this);
	}
	
	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		System.exit(0);
	}
	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
	}
}