자바

자바 소스코드: 다이얼로그 텍스트 바꾸기(ActionListener)

드린 2016. 12. 22. 15:04
반응형
package javaapplication45;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class MyModalDialog extends JDialog{
    JTextField tf=new JTextField(10);
    JButton okButton=new JButton("OK");
    MyModalDialog(JFrame frame, String title){
        super(frame,title,true);
        this.setLayout(new FlowLayout());
        this.add(tf);
        this.add(okButton);
        this.setSize(200, 100);
        
        okButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae) {
                setVisible(false);
            }
            
        });
    }
    String getInput(){
        if(tf.getText().length()==0)    return null;
        else    return tf.getText();
    }
}

class DialogEx2 extends JFrame{
    MyModalDialog dialog;
    DialogEx2(){
        super("DialogEx2 예제 프레임");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton btn=new JButton("Show Modal Dialog");
        
        dialog = new MyModalDialog(this,"Test Modal Dialog");
        
        btn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae) {
                dialog.setVisible(true);
                String text=dialog.getInput();
                
                if(text==null)  return;
                JButton btn=(JButton)ae.getSource();
                btn.setText(text);
            }
            
        });
        
        this.add(btn);
        this.setSize(250, 200);
        this.setVisible(true);
    }
}
public class JavaApplication45 {
    public static void main(String[] args) {
        new DialogEx2();
    }
}

<결과>

2016/12/22 - [자바] - 자바 소스코드: 다이얼로그 만들기

2016/12/22 - [자바] - 자바 소스코드: 툴팁 지연시간 제어

2016/12/22 - [자바] - 자바 소스코드: ToolBar에 ToolTip만들기

#자바 #자바 소스코드 #다이얼로그 텍스트 바꾸기(ActionListener)

반응형