-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetSampleNameFromUser.java
More file actions
89 lines (73 loc) · 2.61 KB
/
Copy pathGetSampleNameFromUser.java
File metadata and controls
89 lines (73 loc) · 2.61 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
2025/07/25 pls Initial authoring
2025/07/31 pls initial implementation
2025/08/01 pls @0033 comment change this works: Pi version fails
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GetSampleNameFromUser
implements ActionListener
{
private static JFrame applFrame = new JFrame("Test Name Entry Panel");
private static GetSampleNameFromUser gsn;
private static boolean tracer = true;
private static JTextField newName = new JTextField(15); // 15 chars long
private Container applFrameContent;
private boolean inputEntered = false;
private static final Font fontLarge = new Font("Serif",Font.BOLD,16);
public static void main(String[] args)
{// System.out.println("GetSampleNameFromUser (GSN) main executed");
gsn = new GetSampleNameFromUser();
String newName;
gsn.init();
newName = gsn.getNameFromUser();
System.out.println("new name is: ["+newName+"]");
newName = gsn.getNameFromUser();
System.out.println("new name is: ["+newName+"]");
System.exit(0);
}
public void start()
{return;
}
public void init()
{if (tracer) System.out.println("GSN.init()");
applFrameContent = applFrame.getContentPane();
applFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applFrame.setLocationRelativeTo(null); // center window
JTextArea instructions = new JTextArea
("Enter your name for this test in the box below. Names work best when"
+" they DO NOT include imbeddded spaces. Use underscores, hyphens,"
+" or periods to replace spaces in your name.\n\n"
+" Press the [ENTER] key after entering your name for this test."
,5,35 // rows, columns
);
instructions.setLineWrap(true);
instructions.setWrapStyleWord(true);
instructions.setFont(fontLarge);
newName.addActionListener(this);
newName.setBorder(BorderFactory.createLineBorder(Color.black,2));
newName.setText("");
newName.setFont(fontLarge);
applFrameContent.add(instructions,BorderLayout.NORTH);
applFrameContent.add(newName,BorderLayout.SOUTH);
applFrame.pack();
}
public String getNameFromUser()
{if (tracer) System.out.println("gsn.getNameFromUser()");
inputEntered = false;
newName.setText("");
applFrame.setVisible(true);
newName.requestFocusInWindow(); // put cursor here
while (!inputEntered)
{try {Thread.sleep(100);}
catch (InterruptedException e) {}
}
applFrame.setVisible(false);
return newName.getText();
}
public void actionPerformed(ActionEvent e)
{if (tracer) System.out.println("GSN.actionPerformed()");
inputEntered = true;
}
}