이벤트를 모아둔 클래스 java.awt.event
public class JFrameTest {
public static void main(String[] args) {
JFrame f = new JFrame("창만들기 연습");
f.setLayout(new FlowLayout());
JButton btn = new JButton("확인");
JButton btn2 = new JButton("취소");
f.add(btn);
f.add(btn2);
f.setSize(400, 300);
f.setVisible(true);
}
}
class AddFrame extends JFrame implements ActionListener{
JTextField jtf01;
JTextField jtf02;
JTextField jtf03;
JButton btnAdd;
JButton btnClear;
public AddFrame() {
jtf01 = new JTextField(10);
jtf02 = new JTextField(10);
jtf03 = new JTextField(10);
btnAdd = new JButton("Add");
btnClear = new JButton("Clear");
setLayout(new FlowLayout());
add(new JLabel("첫번째 정수:"));
add(jtf01);
add(new JLabel("두번째 정수:"));
add(jtf02);
add(new JLabel("연산의 결과:"));
add(jtf03);
add(btnAdd);
add(btnClear);
setSize(250, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//창을 닫았을때 프로그램을 종료
btnAdd.addActionListener(this);
btnClear.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
System.out.println(cmd);
System.out.println("버튼이 눌러짐!");
if(cmd.equals("Add")) {
int n1 = Integer.parseInt(jtf01.getText());
int n2 = Integer.parseInt(jtf02.getText());
int r = n1+ n2;
jtf03.setText(r+"");
}else {
jtf01.setText("");
jtf02.setText("");
jtf03.setText("");
}
}
}
public class AddTest {
public static void main(String[] args) {
new AddFrame();
}
}
AddFrame() : 자기 자신의 생성자를 생성해서 불러온다
getActionCommand(); : 눌러진 버튼의 글자를 보여줌
getText() : 텍스트필드에 적힌 글자 가져오기
setText : 텍스트필드에 값 설정
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); : 창을 닫을 떄 프로그램 종료
this는 자기자신
btnAdd.addActionListener(this)
//Add버튼이 눌러졌을때 이벤트 처리 담당 클래스
class AddEvent implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Add가 눌러짐");
}
}
//Clear버튼이 눌러졌을때 이벤트 처리 담당 클래스
class ClearEvent implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Clear가 눌러짐");
}
}
class AddFrame extends JFrame{
JTextField jtf01;
JTextField jtf02;
JTextField jtf03;
JButton btnAdd;
JButton btnClear;
public AddFrame() {
jtf01 = new JTextField(10);
jtf02 = new JTextField(10);
jtf03 = new JTextField(10);
btnAdd = new JButton("Add");
btnClear = new JButton("Clear");
setLayout(new FlowLayout());
add(new JLabel("첫 번째 정수"));
add(jtf01);
add(new JLabel("두 번째 정수"));
add(jtf02);
add(new JLabel("세 번째 정수"));
add(jtf03);
add(btnAdd);
add(btnClear);
setSize(250, 200);
setVisible(true);
// 창을 닫을 떄 프로그램 종료
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this는 자기자신
// btnAdd.addActionListener(this)
// - 이 버튼을 눌렀을 떄 실행할 이벤트 객체
// this 자리는 이벤트 처리 담당 객체 -- 인터페이스 ActionListener를 구현한 객체
btnAdd.addActionListener(new AddEvent());
btnClear.addActionListener(new ClearEvent());
}
}
public class AddTest {
public static void main(String[] agrs) {
new AddFrame();
}
}
class AddFrame extends JFrame{
JTextField jtf01;
JTextField jtf02;
JTextField jtf03;
JButton btnAdd;
JButton btnClear;
public AddFrame() {
jtf01 = new JTextField(10);
jtf02 = new JTextField(10);
jtf03 = new JTextField(10);
btnAdd = new JButton("Add");
btnClear = new JButton("Clear");
setLayout(new FlowLayout());
add(new JLabel("첫 번째 정수"));
add(jtf01);
add(new JLabel("두 번째 정수"));
add(jtf02);
add(new JLabel("세 번째 정수"));
add(jtf03);
add(btnAdd);
add(btnClear);
setSize(250, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 이름없는 클래스 객체 생성
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int sum = Integer.parseInt(jtf01.getText()) + Integer.parseInt(jtf02.getText());
jtf03.setText(String.valueOf(sum));
}
});
btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtf01.setText("");
jtf02.setText("");
jtf03.setText("");
}
});
}
}
public class AddTest {
public static void main(String[] agrs) {
new AddFrame();
}
}
이름없는 클래스 객체 생성
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Add가 눌러짐");
}
});
btnClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Clear가 눌러짐");
}
});
inner클래스
class MyFrame extends JFrame {
public MyFrame() {
setLayout(new FlowLayout());
for (int i = 0; i < 15; i++) {
add(new JButton("버튼" + (i + 1)));
}
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class FlowLayoutTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
class MyFrame2 extends JFrame {
public MyFrame2() {
setLayout(new GridLayout(3, 5));
for (int i = 0; i < 15; i++) {
add(new JButton("버튼" + (i + 1)));
}
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class GridLayoutTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame2();
}
}
class MyFrame extends JFrame {
public MyFrame() {
setLayout(new BorderLayout());
add(new JButton("위쪽"), BorderLayout.NORTH);
add(new JButton("가운데"), BorderLayout.CENTER);
add(new JButton("오른쪽"), BorderLayout.EAST);
add(new JButton("왼쪽"), BorderLayout.WEST);
add(new JButton("아래"), BorderLayout.SOUTH);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class BorderLayoutTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
복수의 레이아웃 사용
class MyFrame extends JFrame {
JTextArea jta_kor;
JTextArea jta_eng;
HashMap<String, String> map;
public MyFrame() {
map = new HashMap<String, String>();
map.put("텍스트", "Test");
map.put("영어", "English");
jta_kor = new JTextArea(10, 30);
jta_eng = new JTextArea(10, 30);
JButton btnOk = new JButton("변화");
JButton btnCancle = new JButton("최소");
JPanel p_center = new JPanel();
JPanel p_south = new JPanel();
// GridLayout(가로, 세로, 가로 여백, 세로여백)
p_center.setLayout(new GridLayout(1, 2, 10, 10));
p_south.setLayout(new FlowLayout());
p_center.add(jta_kor);
p_center.add(jta_eng);
p_south.add(btnOk);
p_south.add(btnCancle);
btnOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String kor = jta_kor.getText();
String eng = kor;
// map으로 부터 key를 모두 갖고온다
Iterator iter = map.keySet().iterator();
// Iterator에 데이터가 있는 만큼 반복실행
while (iter.hasNext()) {
String key = (String)iter.next();
String value = map.get(key);
// key를 value로 바꾼다
eng = eng.replace(key, value);
}
jta_eng.setText(eng);
}
});
btnCancle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jta_kor.setText("");
jta_eng.setText("");
}
});
setLayout(new BorderLayout());
add(p_center, BorderLayout.CENTER);
add(p_south, BorderLayout.SOUTH);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class KorToEngTest {
public static void main(String[] args) {
new MyFrame();
}
}
JPanel를 통해여 원하는 레이아웃을 설정할 수 있다.