-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path59_TextField
More file actions
53 lines (41 loc) · 1.47 KB
/
59_TextField
File metadata and controls
53 lines (41 loc) · 1.47 KB
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
// JTextField = A GUI textbox component that can be used to add, set, or get text
import javax.swing.*;
<Main.java>
public class Main{
public static void main(String[] args){
new myFrame(); //***REMEMBER DIS to ensure program functionable
}
}
<myFrame.java>
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class myFrame extends JFrame implements ActionListener {
JButton button;
JTextField textField;
myFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
button = new JButton("Submit");
button.addActionListener(this);
textField = new JTextField();
textField.setPreferredSize(new Dimension(250,40));//width,height
textField.setFont(new Font("Consolas", Font.PLAIN,35));
textField.setForeground(Color.black);
textField.setCaretColor(Color.white);
textField.setText("username");
this.add(button);
this.add(textField);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button){ //within constructor: local; outside constructor: global
System.out.println(("Welcome " +textField.getText()));
button.setEnabled(false); //unable to click button twice (only once)
textField.setEditable(false); //unable to edit
}
}
}