728x90
반응형
package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class S05_JTextField extends JFrame {
// 직접 입력이 가능한 텍스트칸 컴포넌트
public S05_JTextField() {
JTextField tf = new JTextField("Hello, text!");
tf.setBounds(50, 50, 300, 30);
tf.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("현재 입력되어있는 것: " + tf.getText());
// # ActionEvent의 메서드들
// getActionCommand() : 해당 이벤트 액션과 관련된 값을 가져온다.
System.out.println(e.getActionCommand());
// getSource() : 이벤트가 발생한 해당 컴포넌트를 반환한다.
JTextField field = (JTextField)(e.getSource());
System.out.println(field.getText());
// getWhen() : 이벤트 발생 시각을 알려준다.
System.out.println(new Date(e.getWhen()));
}
});
add(tf);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setLocation(100, 100);
setVisible(true);
}
public static void main(String[] args) {
new S05_JTextField();
}
}
728x90
반응형
'JAVA > Swing' 카테고리의 다른 글
[JAVA] JPasswordField (0) | 2023.05.02 |
---|---|
[JAVA] JTextArea (0) | 2023.05.02 |
[JAVA] JLabel (0) | 2023.04.27 |
[JAVA] JButton (0) | 2023.04.27 |
[JAVA] Border Layout (0) | 2023.04.27 |