[Java] JOptionPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//외부클래스
public class JOptionPaneTest extends JFrame {
Container con;
//외부클래스 생성자 구현
public JOptionPaneTest(){
setTitle("JOptionPane 실습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = getContentPane();
setBounds(500, 400, 500, 300);
con.add(new myPanel(),BorderLayout.NORTH);
setVisible(true);
}
//내부클래스 구현
class myPanel extends JPanel{
JButton inputbtn = new JButton("Input Name");
JButton confirmbtn = new JButton("confirm");
JButton messagebtn = new JButton("message");
JTextField tf = new JTextField(10);
//내부클래스의 생성자 구현
public myPanel(){
setBackground(Color.LIGHT_GRAY);
add(inputbtn);
add(confirmbtn);
add(messagebtn);
add(tf);
//이벤트 연결 및 이벤트 핸들러 처리를 동시에
inputbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog("이름을 입력하세요");
tf.setText(name);
}
});
confirmbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(null, "계속하겠습니까?",
"confirm 다이얼로그창",JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION)
tf.setText("Yes");
else
tf.setText("No");
}
});
messagebtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "message 다이얼로그 태스트","Message",JOptionPane.ERROR_MESSAGE);
tf.setText("메세지 보여줌");
}
});
}
}
public static void main(String args[]){
new JOptionPaneTest();
}
}