-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFrame.java
More file actions
130 lines (108 loc) · 4.11 KB
/
TextFrame.java
File metadata and controls
130 lines (108 loc) · 4.11 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextFrame extends JFrame {
private JTextArea textArea;
private JTextField textField;
private JButton submitButton;
private JTextField usernameField;
private JButton usernameSubmitButton;
private JPanel mainPanel;
private JPanel usernamePanel;
private String username;
// Constructor
public TextFrame() {
// Set up the JFrame
setTitle("Text Display Frame");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new CardLayout());
// Initialize panels
mainPanel = createMainPanel();
usernamePanel = createUsernamePanel();
// Add panels to the frame
add(usernamePanel, "UsernamePanel");
add(mainPanel, "MainPanel");
// Show the username input screen initially
showUsernameInput();
// Make the frame visible
setVisible(true);
}
// Method to create the username input panel
private JPanel createUsernamePanel() {
JPanel panel = new JPanel(new BorderLayout());
JLabel usernameLabel = new JLabel("Enter Username:");
usernameField = new JTextField();
usernameSubmitButton = new JButton("Submit");
// Action listener for the username submission
usernameSubmitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputUsername = usernameField.getText().trim();
if (!inputUsername.isEmpty()) {
username = inputUsername;
showMainPanel();
}
}
});
panel.add(usernameLabel, BorderLayout.NORTH);
panel.add(usernameField, BorderLayout.CENTER);
panel.add(usernameSubmitButton, BorderLayout.SOUTH);
return panel;
}
// Method to create the main text display panel
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
// Initialize JTextArea
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("SansSerif", Font.PLAIN, 14));
JScrollPane scrollPane = new JScrollPane(textArea);
// Initialize text field and submit button
textField = new JTextField();
submitButton = new JButton("Submit");
// Panel for input components
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(textField, BorderLayout.CENTER);
inputPanel.add(submitButton, BorderLayout.EAST);
// Action listener for the submit button
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputText = getInputText();
if (!inputText.isEmpty()) {
appendText(username + ": " + inputText);
textField.setText(""); // Clear the input field after submission
}
}
});
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(inputPanel, BorderLayout.SOUTH);
return panel;
}
// Method to show the username input panel
private void showUsernameInput() {
CardLayout layout = (CardLayout) getContentPane().getLayout();
layout.show(getContentPane(), "UsernamePanel");
}
// Method to show the main panel
private void showMainPanel() {
CardLayout layout = (CardLayout) getContentPane().getLayout();
layout.show(getContentPane(), "MainPanel");
appendText("Welcome, " + username + "!");
}
// Method to update the text area
public void appendText(String text) {
textArea.append(text + "\n");
}
// Method to get text from the input field
public String getInputText() {
return textField.getText();
}
// Main method to test the class
public static void main(String[] args) {
new TextFrame();
}
}