-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJRadioButton
77 lines (64 loc) · 2.61 KB
/
JRadioButton
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
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
/**
*
* @author onezer0
*/
public class Tela extends JFrame {
private final JTextField txtTexto = new JTextField("Exemplo Java", 31);
private final Font fntNormal = new Font("TimesRoman", Font.PLAIN, 14);
private final Font fntNegrito = new Font("TimesRoman", Font.BOLD, 14);
private final Font fntItalico = new Font("TimesRoman", Font.ITALIC, 14);
private final Font fntItalicoNegrito = new Font("TimesRoman", Font.BOLD + Font.ITALIC, 14);
private final JRadioButton rdNormal = new JRadioButton("Normal", true);
private final JRadioButton rdNegrito = new JRadioButton("Negrito", false);
private final JRadioButton rdItalico = new JRadioButton("Italico", false);
private final JRadioButton rdNegritoItalico = new JRadioButton("Negrito e Italico", false);
public Tela() {
super("Exemplo de fonte");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(400, 100);
setLocation(500, 300);
Container painelConteudo = getContentPane();
painelConteudo.setLayout(new FlowLayout());
ButtonGroup rdTipoFonte = new ButtonGroup();
rdTipoFonte.add(rdNormal);
rdTipoFonte.add(rdNegrito);
rdTipoFonte.add(rdItalico);
rdTipoFonte.add(rdNegritoItalico);
ActionListener fontChangeListener = e -> {
if (rdNormal.isSelected()) {
txtTexto.setFont(fntNormal);
} else if (rdNegrito.isSelected()) {
txtTexto.setFont(fntNegrito);
} else if (rdItalico.isSelected()) {
txtTexto.setFont(fntItalico);
} else if (rdNegritoItalico.isSelected()) {
txtTexto.setFont(fntItalicoNegrito);
}
};
rdNormal.addActionListener(fontChangeListener);
rdNegrito.addActionListener(fontChangeListener);
rdItalico.addActionListener(fontChangeListener);
rdNegritoItalico.addActionListener(fontChangeListener);
painelConteudo.add(txtTexto);
painelConteudo.add(rdNormal);
painelConteudo.add(rdNegrito);
painelConteudo.add(rdItalico);
painelConteudo.add(rdNegritoItalico);
txtTexto.setFont(fntNormal);
pack();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
Tela tela = new Tela();
tela.setVisible(true);
});
}
}