Java swing, awt 기본적인 GUI
package project1010;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class P2 {
public static void main(String[] args) {
new JFrameTest();
}
}
class JFrameTest extends JFrame implements ActionListener{
JButton btn = null;
JButton btn2 = null;
JTextField Sname =null;
JTextField Snum =null;
public JFrameTest() {
super("다이얼 로그");
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(200,200,200,300);
this.setLayout(new FlowLayout());
btn = new JButton("확인");
btn2 = new JButton("취소");
// EventHandler handler = new EventHandler();
// btn.addActionListener(handler);
// btn2.addActionListener(handler);
btn.addActionListener(this);
btn2.addActionListener(this);
//라벨, 텍스트 박스 생성
JLabel L_Sname = new JLabel("학생이름",SwingConstants.RIGHT);
Sname = new JTextField(10);
JLabel L_Snum = new JLabel("학생번호",SwingConstants.RIGHT);
Snum = new JTextField(10);
this.add(L_Sname);
this.add(Sname);
this.add(L_Snum);
this.add(Snum);
this.add(btn);
this.add(btn2);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(btn == e.getSource()){
clickedBtn();
}else if(btn2 == e.getSource()){
clickedBtn2();
}
}
public void clickedBtn(){
// String str = btn.getText();
// JOptionPane.showMessageDialog(null, str+" 버튼을 눌렀구나!");
String name = Sname.getText();
String num = Snum.getText();
JOptionPane.showMessageDialog(null, "학생의 이름은 "+ name +" 입니다. \n"+"학생의 번호는 "+ num +" 입니다. \n");
}
public void clickedBtn2(){
// String str = btn2.getText();
// String str2 = Sname.getText();
// Sname.setText(null);
// JOptionPane.showMessageDialog(null, str+" 버튼을 눌렀구나!\n"+str2+"를 입력했구나!");
dispose();
}
}
class EventHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = ((JButton)e.getSource());
String str = btn.getText();
JOptionPane.showMessageDialog(null, str+" 버튼을 눌렀구나!");
}
}
결과

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class P2 {
public static void main(String[] args) {
new JFrameTest();
}
}
class JFrameTest extends JFrame implements ActionListener{
JButton btn = null;
JButton btn2 = null;
JTextField Sname =null;
JTextField Snum =null;
public JFrameTest() {
super("다이얼 로그");
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(200,200,200,300);
this.setLayout(new FlowLayout());
btn = new JButton("확인");
btn2 = new JButton("취소");
// EventHandler handler = new EventHandler();
// btn.addActionListener(handler);
// btn2.addActionListener(handler);
btn.addActionListener(this);
btn2.addActionListener(this);
//라벨, 텍스트 박스 생성
JLabel L_Sname = new JLabel("학생이름",SwingConstants.RIGHT);
Sname = new JTextField(10);
JLabel L_Snum = new JLabel("학생번호",SwingConstants.RIGHT);
Snum = new JTextField(10);
this.add(L_Sname);
this.add(Sname);
this.add(L_Snum);
this.add(Snum);
this.add(btn);
this.add(btn2);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(btn == e.getSource()){
clickedBtn();
}else if(btn2 == e.getSource()){
clickedBtn2();
}
}
public void clickedBtn(){
// String str = btn.getText();
// JOptionPane.showMessageDialog(null, str+" 버튼을 눌렀구나!");
String name = Sname.getText();
String num = Snum.getText();
JOptionPane.showMessageDialog(null, "학생의 이름은 "+ name +" 입니다. \n"+"학생의 번호는 "+ num +" 입니다. \n");
}
public void clickedBtn2(){
// String str = btn2.getText();
// String str2 = Sname.getText();
// Sname.setText(null);
// JOptionPane.showMessageDialog(null, str+" 버튼을 눌렀구나!\n"+str2+"를 입력했구나!");
dispose();
}
}
class EventHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = ((JButton)e.getSource());
String str = btn.getText();
JOptionPane.showMessageDialog(null, str+" 버튼을 눌렀구나!");
}
}
결과


댓글
댓글 쓰기