-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserInputForm.java
55 lines (46 loc) · 1.6 KB
/
UserInputForm.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserInputForm extends JFrame {
private JTextField userInputField;
private JLabel displayLabel;
public UserInputForm() {
// Set up the JFrame
setTitle("User Input Form");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create components
userInputField = new JTextField(20);
JButton displayButton = new JButton("Display Input");
displayLabel = new JLabel("Your input will be displayed here.");
// Set up layout
setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("Enter Text:"));
inputPanel.add(userInputField);
inputPanel.add(displayButton);
// Add components to the frame
add(inputPanel, BorderLayout.NORTH);
add(displayLabel, BorderLayout.CENTER);
// Add action listener to the button
displayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayUserInput();
}
});
}
private void displayUserInput() {
String userInput = userInputField.getText();
displayLabel.setText("Your input: " + userInput);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserInputForm().setVisible(true);
}
});
}
}