-
Notifications
You must be signed in to change notification settings - Fork 44
/
DefaultText.java
67 lines (58 loc) · 1.89 KB
/
DefaultText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.webRequest;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
// 默认显示文本
public class DefaultText implements FocusListener {
private String defaultText;
private JTextField defaultField;
public DefaultText(String Text, JTextField textField){
this.defaultText = Text;
this.defaultField = textField;
textField.setText(Text);
textField.setForeground(Color.GRAY);
}
@Override
public void focusGained(FocusEvent e) {
String temp = defaultField.getText();
if (temp.equals(defaultText)){
defaultField.setText("");
defaultField.setForeground(Color.BLACK);
}
}
@Override
public void focusLost(FocusEvent e) {
String temp = defaultField.getText();
if (temp.equals("")){
defaultField.setForeground(Color.GRAY);
defaultField.setText(defaultText);
}
}
}
class DefaultTextarea implements FocusListener{
private String defaultText;
private JTextArea defaultTextarea;
public DefaultTextarea(String Text, JTextArea TextArea){
this.defaultText = Text;
this.defaultTextarea = TextArea;
defaultTextarea.setText(Text);
defaultTextarea.setForeground(Color.GRAY);
}
@Override
public void focusGained(FocusEvent e) {
String temp = defaultTextarea.getText();
if (temp.equals(defaultText)){
defaultTextarea.setText("");
defaultTextarea.setForeground(Color.BLACK);
}
}
@Override
public void focusLost(FocusEvent e) {
String temp = defaultTextarea.getText();
if (temp.equals("")){
defaultTextarea.setForeground(Color.GRAY);
defaultTextarea.setText(defaultText);
}
}
}