-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditor.java
More file actions
122 lines (103 loc) · 3.99 KB
/
TextEditor.java
File metadata and controls
122 lines (103 loc) · 3.99 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
import java.awt.*;
import java.awt.event.*;
public class TextEditor extends Frame {
TextField textField;
Label outputLabel;
Checkbox boldCheckbox, italicCheckbox;
Choice fontSizeChoice, fontFamilyChoice;
public TextEditor() {
// Set up the Frame
setTitle("Simple Text Editor");
setSize(500, 250);
setLayout(new FlowLayout());
// Create TextField for the user to input text
textField = new TextField(30);
add(new Label("Enter Text:"));
add(textField);
// Create a Label to display the output text
outputLabel = new Label("Output will appear here");
outputLabel.setFont(new Font("Arial", Font.PLAIN, 14)); // Default font
add(outputLabel);
// Create a Choice (dropdown) for font family
fontFamilyChoice = new Choice();
fontFamilyChoice.add("Arial");
fontFamilyChoice.add("Courier New");
fontFamilyChoice.add("Times New Roman");
fontFamilyChoice.add("Verdana");
fontFamilyChoice.select("Arial"); // Default font
add(new Label("Font Family:"));
add(fontFamilyChoice);
// Create a Choice (dropdown) for font size
fontSizeChoice = new Choice();
fontSizeChoice.add("12");
fontSizeChoice.add("14");
fontSizeChoice.add("16");
fontSizeChoice.add("18");
fontSizeChoice.add("20");
fontSizeChoice.add("24");
fontSizeChoice.add("28");
fontSizeChoice.select("14"); // Default font size
add(new Label("Font Size:"));
add(fontSizeChoice);
// Create checkboxes for bold and italic
boldCheckbox = new Checkbox("Bold");
italicCheckbox = new Checkbox("Italic");
add(boldCheckbox);
add(italicCheckbox);
// Event listeners to apply changes
textField.addTextListener(new TextListener() {
public void textValueChanged(TextEvent e) {
updateLabelText();
}
});
fontSizeChoice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateLabelText();
}
});
fontFamilyChoice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateLabelText();
}
});
boldCheckbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateLabelText();
}
});
italicCheckbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateLabelText();
}
});
// Window listener to handle closing the frame
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
// Method to update the label text based on user selections
private void updateLabelText() {
String text = textField.getText(); // Get the text entered in the TextField
// Get selected font size and font family
String selectedFamily = fontFamilyChoice.getSelectedItem();
int fontSize = Integer.parseInt(fontSizeChoice.getSelectedItem());
// Determine the style based on the checkboxes
int style = Font.PLAIN;
if (boldCheckbox.getState()) {
style = Font.BOLD;
}
if (italicCheckbox.getState()) {
style |= Font.ITALIC;
}
// Set the font of the output label
outputLabel.setFont(new Font(selectedFamily, style, fontSize));
// Update the output label with the text entered in the TextField
outputLabel.setText(text);
}
public static void main(String[] args) {
TextEditor editor = new TextEditor();
editor.setVisible(true);
}
}