[Java] JFrame 창 구현하기

JTI·2022년 12월 6일
0

📌 Code list

목록 보기
34/55

다음 조건을 만족하는 창을 구현하시오.

  1. 사이즈: 500 * 400
  2. 위치: 200, 200
  3. 타이틀: MyFrame1
  4. 종료버튼 클릭시: 현재창만 종료
  5. 배경색: 노랑

✏️ main에다 직접 넣기


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JFrame;

public class MyFrame1 {
	public static void main(String[] args) {
		JFrame f = new JFrame();

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(new Dimension(500, 400));
		f.setLocation(new Point(200, 200));
		f.setTitle("MyFrame1");
		f.getContentPane().setBackground(Color.YELLOW);

		f.setVisible(true);
	}
}

이렇게 쓸 수도 있다.

Rectangle bounds = new Rectangle(new Point(200, 200), new Dimension(500, 400));
f.setBounds(200, 200, 500, 400);
//f.setSize(new Dimension(500, 400));
//f.setLocation(new Point(200, 200));

✏️ Jframe 상속받아서 활용하기


import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JFrame;

public class FrameEx2 extends JFrame {
	public FrameEx2() {
		super("MyFrame1");
		
		setBounds(new Rectangle(200, 200, 500, 400));
		getContentPane().setBackground(Color.YELLOW);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new FrameEx2();
	}
}
profile
Fill in my own colorful colors🎨

0개의 댓글