1. 기초
package swing;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
//SWING => AWT에서 발전된 GUI
//JFrame => JPanel => JObject
//닫기 따로 안만들어도 닫기 됨
public class swing1 {
public static void main(String[] args) {
new swing1_box();
}
}
class swing1_box extends JFrame{
private JPanel contentPane;
private JTextField mid;
public swing1_box() {
setBounds(100, 100, 450, 300); //해당 JFrame 위치 및 크기
this.contentPane = new JPanel(); //오브젝트를 생성할 수 있도록 클래스를 로드
//화면의 위치에 따른 외곽선 부분
this.contentPane.setBorder(new EmptyBorder(5,5,5,5)); //EmptyBorder(상,하,좌,우) : 여백
this.contentPane.setBounds(5,5,200,200);
// this.contentPane.setBackground(Color.red);
JButton btn = new JButton("클릭");
btn.setFont(new Font("돋움체",Font.PLAIN,12));
btn.setBackground(Color.black);
btn.setForeground(Color.white);
btn.setBounds(10,10,100,30);
setContentPane(this.contentPane);
this.contentPane.setLayout(null); //직접 만들겠다
setVisible(true); //JFrame 보여줌 (true) / false : 안보이게함
setTitle("SWING 연습1"); //JFrame 타이틀 제목
this.contentPane.add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
- JPanel : 프레임
- JButton : 버튼
- JTextField : 텍스트 필드
- setTitle("제목"); : 제목