1
package com.kh.gui.part01_container.view;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class JFrameTest1 extends JFrame {
//프레임을 생성하는 방법
//javax.swing.JFrame 클래스를 상속받아 생성자에서 프레임에 대한 설정 후
//다른 클래스에서 호출하는 방식
public JFrameTest1() {
//프레임에 크기 와 위치 지정
// this.setLocation(300, 200);
// this.setSize(800, 500);
this.setBounds(300, 200, 800, 500);
//프레임 상단에 이름 설정하기
this.setTitle("MyFrame");
//프레임 상단에 이미지 변경하기
try {
this.setIconImage(ImageIO.read(new File("images/icon.PNG")));
} catch (IOException e) {
e.printStackTrace();
}
//프레임 사이즈 고정하기
this.setResizable(false);
//닫기(x)버튼을 눌러도 프로세스는 계속 실행중이다.
//아래 메소드를 실행시켜야 프레임을 종료할 때 프로세스도 정상 종료된다.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//프레임이 보여지게 하는 메소드
this.setVisible(true);
}
}
package com.kh.gui.part01_container.view;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class JFrameTest2 {
//프레임을 생성하는 방법2
//상속을 받지 않고 직접 객체를 생성하는 방법
public void showMainFrame() {
JFrame mainFrame = new JFrame("MyFrame2");
//mainFrame.setBounds(300, 200, 800, 500);
//Rectangle 객체를 활용한 방법
Rectangle r = new Rectangle(300, 200, 800, 500);
mainFrame.setBounds(r);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
run
package com.kh.gui.part01_container.run;
import com.kh.gui.part01_container.view.JFrameTest2;
public class Run {
public static void main(String[] args) {
//new JFrameTest1();
new JFrameTest2().showMainFrame();
}
}
2
package com.kh.gui.part02_layout.view;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class A_BorderLayout extends JFrame {
public A_BorderLayout() {
super("BorderLayout");
this.setBounds(300, 200, 800, 500);
//레이아웃 설정
//JFrame의 레이아웃 기본값이다.
//this.setLayout(new BorderLayout());
//버튼 생성
JButton north = new JButton("북");
JButton south = new JButton("남");
JButton east = new JButton("동");
JButton west = new JButton("서");
JButton center = new JButton("가운데");
//레이아웃의 위치별로 컴포넌트 배치
this.add(north, "North");
this.add(south, "South");
this.add(east, "East");
this.add(west, "West");
this.add(center, "Center");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package com.kh.gui.part02_layout.view;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class B_FlowLayout extends JFrame {
public B_FlowLayout() {
super("FlowLayout");
this.setBounds(300, 200, 800, 500);
//FlowLayout설정
//컴포넌트들을 가로나 세로 방향의 줄 단위로 배치하는 방식의 레이아웃이다.
//만약 컨테이너보다 배치할 구성 요소가 더 많거나 크게 되면 자동으로 다음 줄을 넘기며 배치된다.
//this.setLayout(new FlowLayout());
//FlowLayout은 정렬 방식에 대한 alignment 값을 가지고 있으며 기본값은 center 이다.
//this.setLayout(new FlowLayout(FlowLayout.CENTER));
//this.setLayout(new FlowLayout(FlowLayout.LEFT));
//this.setLayout(new FlowLayout(FlowLayout.RIGHT));
//this.setLayout(new FlowLayout(FlowLayout.LEADING)); //왼쪽 정렬
this.setLayout(new FlowLayout(FlowLayout.TRAILING)); //오른쪽 정렬
this.add(new JButton("1번"));
this.add(new JButton("2번"));
this.add(new JButton("3번"));
this.add(new JButton("4번"));
this.add(new JButton("5번"));
this.add(new JButton("6번"));
this.add(new JButton("7번"));
this.add(new JButton("8번"));
this.add(new JButton("9번"));
this.add(new JButton("10번"));
this.add(new JButton("11번"));
this.add(new JButton("12번"));
this.add(new JButton("13번"));
this.add(new JButton("14번"));
this.add(new JButton("15번"));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
package com.kh.gui.part02_layout.view;
import java.awt.GridLayout;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
public class C_GridLayout extends JFrame {
public C_GridLayout() {
super("GridLayout");
this.setBounds(300, 200, 800, 500);
//GridLayout
//컴포넌트들을 가로, 세로의 일정 수 만큼 배치하고 싶을 때 사용한다.
//윗 줄부터 시작해서 왼쪽에서 오른쪽으로 움직이며 각 줄을 이동하며 컴포넌트를 배치하는 방식이다.
this.setLayout(new GridLayout(5, 5/*, 10, 20*/));
/*for(int i = 1; i < 26; i++) {
String str = new Integer(i).toString();
this.add(new JButton(str));
}*/
//중복 제거하여 랜덤으로 빙고판 만들기
Set set = new LinkedHashSet();
while(set.size() < 25) {
set.add(new Random().nextInt(25) + 1);
}
Object[] obj = set.toArray();
for(int i = 0; i < obj.length; i++) {
String str = new Integer((int) obj[i]).toString();
this.add(new JButton(str));
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
package com.kh.gui.part02_layout.view;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class D_CardLayout extends JFrame {
public D_CardLayout() {
super("CardLayout");
this.setBounds(300, 200, 800, 500);
//CardLayout
//panel을 이용해서 카드 판을 만들고 겹쳐서 배치를 함
//이벤트를 이용해서 다른 판이 보여지게 설정해야 한다.
CardLayout card = new CardLayout();
this.setLayout(card);
//패널 만들기
//패널은 컴포넌트이지만 다른 컴포넌트를 포함할 수 있는 컨테이너 성격을 가지기도 한다.
JPanel card1 = new JPanel();
JPanel card2 = new JPanel();
JPanel card3 = new JPanel();
//패널에 배경색 지정
card1.setBackground(Color.GRAY);
card2.setBackground(Color.YELLOW);
card3.setBackground(new Color(50, 100, 50));
//패널에 라벨 추가
card1.add(new JLabel("Card1"));
card2.add(new JLabel("Card2"));
card3.add(new JLabel("Card3"));
//메인프레임에 패널을 추가
this.add(card1);
this.add(card2);
this.add(card3);
//패널에 이벤트 추가
card1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 1) {
card.next(card1.getParent());
}
if(e.getButton() == 3) {
card.previous(card1.getParent());
}
}
});
card2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 1) {
card.next(card2.getParent());
}
if(e.getButton() == 3) {
card.previous(card2.getParent());
}
}
});
card3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 1) {
card.next(card3.getParent());
}
if(e.getButton() == 3) {
card.previous(card3.getParent());
}
}
});
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package com.kh.gui.part02_layout.view;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class F_NullLayout extends JFrame {
//Layout 지정 없이 위치 지정하면서 배치하는 방식이다.
public F_NullLayout() {
super("NullLayout");
this.setBounds(200, 200, 500, 500);
this.setLayout(null);
JLabel lb = new JLabel("이름 : ");
lb.setLocation(50, 100);
lb.setSize(150, 50);
JTextField tf = new JTextField();
tf.setLocation(110, 100);
tf.setSize(200, 50);
JButton btn = new JButton("추가");
btn.setLocation(350, 100);
btn.setSize(100, 50);
this.add(lb);
this.add(tf);
this.add(btn);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package com.kh.gui.part02_layout.view;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class G_PanelLayout extends JFrame {
public G_PanelLayout() {
super("JPanelLayoutTest");
this.setBounds(200, 200, 500, 500);
this.setLayout(null);
JLabel lb = new JLabel("이름 : ");
lb.setLocation(50, 100);
lb.setSize(150, 50);
JTextField tf = new JTextField();
tf.setLocation(110, 100);
tf.setSize(200, 50);
JButton btn = new JButton("추가");
btn.setLocation(350, 100);
btn.setSize(100, 50);
//패널 한 개 더 생성
JPanel panel = new JPanel();
panel.setSize(500, 500);
panel.setLayout(null);
panel.add(lb);
panel.add(tf);
panel.add(btn);
this.add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
run
package com.kh.gui.part02_layout.run;
import com.kh.gui.part02_layout.view.A_BorderLayout;
import com.kh.gui.part02_layout.view.B_FlowLayout;
import com.kh.gui.part02_layout.view.C_GridLayout;
import com.kh.gui.part02_layout.view.D_CardLayout;
import com.kh.gui.part02_layout.view.F_NullLayout;
import com.kh.gui.part02_layout.view.G_PanelLayout;
public class Run {
public static void main(String[] args) {
//new A_BorderLayout();
//new B_FlowLayout();
//new C_GridLayout();
//new D_CardLayout();
//new F_NullLayout();
new G_PanelLayout();
}
}
반응형
'Programming > Java' 카테고리의 다른 글
[Java] Thread (0) | 2022.03.07 |
---|---|
[Java] IO(입출력) (0) | 2022.03.07 |
[Java] Exception(예외처리) (0) | 2022.03.07 |
[Java] 기본 API (0) | 2022.03.07 |
[Java] 다형성 (0) | 2022.03.07 |
댓글