-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextStyler.java
More file actions
73 lines (61 loc) · 2.33 KB
/
TextStyler.java
File metadata and controls
73 lines (61 loc) · 2.33 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextStyler extends JFrame implements ActionListener {
private JTextField inputTextField;
private JLabel displayLabel;
private JComboBox<String> fontComboBox;
private JComboBox<Integer> sizeComboBox;
private JCheckBox boldCheckBox, italicCheckBox;
private static final String[] FONTS = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
private static final Integer[] SIZES = {8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36};
public TextStyler() {
setTitle("Text Styler");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
inputTextField = new JTextField(20);
displayLabel = new JLabel("Your styled text will appear here");
displayLabel.setFont(new Font("Arial", Font.PLAIN, 14));
fontComboBox = new JComboBox<>(FONTS);
sizeComboBox = new JComboBox<>(SIZES);
boldCheckBox = new JCheckBox("Bold");
italicCheckBox = new JCheckBox("Italic");
JButton applyButton = new JButton("Apply");
applyButton.addActionListener(this);
add(new JLabel("Enter Text:"));
add(inputTextField);
add(new JLabel("Font:"));
add(fontComboBox);
add(new JLabel("Size:"));
add(sizeComboBox);
add(boldCheckBox);
add(italicCheckBox);
add(applyButton);
add(displayLabel);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = inputTextField.getText();
String selectedFont = (String) fontComboBox.getSelectedItem();
int selectedSize = (Integer) sizeComboBox.getSelectedItem();
int style = Font.PLAIN;
if (boldCheckBox.isSelected()) {
style |= Font.BOLD;
}
if (italicCheckBox.isSelected()) {
style |= Font.ITALIC;
}
Font font = new Font(selectedFont, style, selectedSize);
displayLabel.setText(text);
displayLabel.setFont(font);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TextStyler frame = new TextStyler();
frame.setVisible(true);
});
}
}