2024-05-24 15:00:49 +08:00
|
|
|
package hae.utils;
|
2024-05-23 12:00:13 +08:00
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.FocusEvent;
|
|
|
|
|
import java.awt.event.FocusListener;
|
|
|
|
|
|
|
|
|
|
public class UIEnhancer {
|
|
|
|
|
public static void setTextFieldPlaceholder(JTextField textField, String placeholderText) {
|
|
|
|
|
textField.setForeground(Color.GRAY);
|
|
|
|
|
textField.setText(placeholderText);
|
|
|
|
|
textField.addFocusListener(new FocusListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void focusGained(FocusEvent e) {
|
|
|
|
|
if (textField.getText().equals(placeholderText)) {
|
|
|
|
|
textField.setText("");
|
|
|
|
|
textField.setForeground(Color.BLACK);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void focusLost(FocusEvent e) {
|
|
|
|
|
if (textField.getText().isEmpty()) {
|
|
|
|
|
textField.setForeground(Color.GRAY);
|
|
|
|
|
textField.setText(placeholderText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|