package day0214;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* GridLayout(격자)와 컴포넌트
*/
@SuppressWarnings("serial")
public class UseGridLayout extends JFrame {
public UseGridLayout() {
super("GridLayout 연습");
//컴포넌트를 생성
JButton jbtn=new JButton("버튼 1");
JButton jbtn2=new JButton("버튼 2");
JButton jbtn3=new JButton("버튼 3");
JButton jbtn4=new JButton("버튼 4");
JButton jbtn5=new JButton("버튼 5");
JButton jbtn6=new JButton("버튼 6");
//배치관리자 설정하여 컴포넌트를 배치
setLayout(new GridLayout(2,3)); //BorderLayout => GridLayout
add ( jbtn );
add ( jbtn2 );
add ( jbtn3 );
add ( jbtn4 );
add ( jbtn5 );
add ( jbtn6 );
//윈도우 크기 설정
setSize(400,200);
//사용자에게 보여주기
setVisible(true);
//윈도우 종료 처리
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//UseGridLayout
public static void main(String[] args) {
new UseGridLayout();
}//main
}//class
컴파일 결과
package day0214;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
/**
* 흐름레이아웃과 컴포넌트 사용
*/
@SuppressWarnings("serial")
//1.윈도우 컴포넌트를 상속
public class UseFlowLayout extends JFrame {
public UseFlowLayout() {
super("FlowLayout연습");
//2.컴포넌트를 생성
//이름을 설정 : 종류+하는일의 형식으로 이름을 설정한
JLabel jlblName=new JLabel("이름");
JTextField jtfName=new JTextField(10);
JRadioButton jrbMale=new JRadioButton("남자"); //,true checked 된 상태로 제공할 때
JRadioButton jrbFemale=new JRadioButton("여자");
JButton jbtAdd=new JButton("입력");
//버튼이 버튼 그룹으로 묶여져야 둘 중에 하나만 선택된다.
//2-1. 버튼 그룹을 생성하고
ButtonGroup bg=new ButtonGroup();
//2-2. 버튼 그룹에 버튼 할당
bg.add(jrbMale);
bg.add(jrbFemale);
//3.배치관리자를 생성하고, 컴포넌트를 배치
setLayout(new FlowLayout()); //BorderLayout => FlowLayout 변경
add(jlblName);
add(jtfName);
add(jrbMale);
add(jrbFemale);
add(jbtAdd);
//4.윈도우 크기 설정
setSize(600,300);
//5.사용자에게 보여주기
setVisible(true);
//6.윈도우 종료 이벤트 처리
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//UseFlowLayout
public static void main(String[] arge) {
new UseFlowLayout();
}//main
}//class
컴파일 결과
package day0214;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
//1. Window Component 를 상속
public class UseBorderLayout extends JFrame {
public UseBorderLayout() {
super("BorderLayout 연습");//Frame 의 생성자 호출 : 타이틀 바에 들어갈 메시지
//2. 컴포넌트 생성: JLabel 2개, JButton 2개,JTextArea 1개
JLabel jlblNorth=new JLabel("북쪽");
JLabel jlblSouth=new JLabel("남쪽");
JButton jlblWest=new JButton("서쪽");
JButton jlblEast=new JButton("동쪽");
JTextArea jtaCenter=new JTextArea("가운데");
//3. 컴포넌트 배치
//배치 관리자툴 설정 (윈도우 분할하는 설정)
// BorderLayout b1=new BorderLayout();
// setLayout(b1);
setLayout(new BorderLayout());
//Constant 사용
add(jlblNorth,BorderLayout.NORTH);
add(jlblWest,BorderLayout.WEST);
add(jtaCenter,BorderLayout.CENTER);
//문자열 상수 사용
add(jlblSouth,"South");
add(jlblEast,"East");
//4.윈도우의 크기 설정 (사용자에게 보여주기 위한 크기 설정)
setSize(400,400);
//5.윈도우를 보여주기 위한 설정.
setVisible(true);
//6.윈도우 종료 처리(x 버튼을 눌렀을 때 instance 소멸)
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//UseBorderLayout
public static void main(String[] args) {
new UseBorderLayout();
}
}
컴파일 결과
package day0217;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* 글꼴 변경
*/
@SuppressWarnings("serial")
public class UseFont extends JFrame {
public UseFont() {
super("Font의 사용");
//Non=Visual Component
Font font=new Font("궁서체", Font.BOLD,20);
//Non=Visual Component
Color color=new Color(0xD3E5CB);
Color color2=new Color(0xEFCE86);
//컴포넌트 생성
JLabel jlbl=new JLabel("라벨");
JTextField jtf=new JTextField(10);
JButton jbtn=new JButton("버튼");
//폰트를 적용
jlbl.setFont(font);
jtf.setFont(font);
jbtn.setFont(font);
//전경색(글자색)
//Constant 사용
jlbl.setForeground(Color.blue);
jtf.setForeground(Color.cyan);
jbtn.setForeground(Color.DARK_GRAY);
//바닥색 변경
jlbl.setBackground(Color.GREEN); //배경이 투명하여 바닥이 보이지 않는다.
jlbl.setOpaque(true); //불투명도를 true 로 설정하면 바닥색을 보여줄 수 있다.
jtf.setBackground(Color.GREEN);
jbtn.setBackground(Color.MAGENTA); //바닥색이 보이는 이유는 불투명도가 설정되어있어서
// jbtn.setContentAreaFilled(false);//배경이 투명 (배경색이 설정되지 않는다.)
JTextArea jta=new JTextArea(5,30);
jta.setFont(new Font("휴먼편지체",Font.BOLD|Font.ITALIC,25));
//객체사용
jta.setForeground(color); //전경색
jta.setBackground(color2);//배경색
JScrollPane jsp=new JScrollPane(jta);
//배치관리자 설정
setLayout(new FlowLayout() );
//컴포넌트 배치
add(jlbl);
add(jtf);
add(jbtn);
add(jsp);
//윈도우 크기 설정
setBounds(100,100,500,300);
//사용자에게 보여주기
setVisible(true);
//윈도우 종료 설정
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//UseFont
public static void main(String[] args) {
new UseFont();
}//main
}//class
컴파일 결과
'Java개발' 카테고리의 다른 글
[Java] 예외 처리 Exception (0) | 2025.03.30 |
---|---|
[Java]Dialog,JOptionPane,actionFerformed,UseCombobox&List&Icon&Table (0) | 2025.03.24 |
[Java] ArrayList,Scanner (0) | 2025.03.19 |
[Java] StringBuilder 등 유용한 클래스들 import하기 (0) | 2025.03.17 |
[Java] Array,Variable Array (0) | 2025.03.16 |