Java Reference
In-Depth Information
79 setNewFont();
80 }
81 });
82 jchkItalic.addActionListener( new ActionListener() {
83 @Override
84 public void actionPerformed(ActionEvent e) {
85 setNewFont();
86 }
87 });
88
89 // Register listeners for radio buttons
90 jrbRed.addActionListener( new ActionListener() {
91 @Override
92 public void actionPerformed(ActionEvent e) {
93 jlblMessage.setForeground(Color.red);
94 }
95 });
96 jrbGreen.addActionListener( new ActionListener() {
97 @Override
98 public void actionPerformed(ActionEvent e) {
99 jlblMessage.setForeground(Color.green);
100 }
101 });
102 jrbBlue.addActionListener( new ActionListener() {
103 @Override
104 public void actionPerformed(ActionEvent e) {
105 jlblMessage.setForeground(Color.blue);
106 }
107 });
108
109 // Register listener for text field
110 jtfMessage.addActionListener( new ActionListener() {
111 @Override
112 public void actionPerformed(ActionEvent e) {
113 jlblMessage.setText(jtfMessage.getText());
114 jtfMessage.requestFocusInWindow();
115 }
116 });
117 }
118
119
register listener
register listener
register listener
register listener
register listener
private void setNewFont() {
120
// Determine a font style
121
int fontStyle = Font.PLAIN;
122
fontStyle += (jchkBold.isSelected() ? Font.BOLD : Font.PLAIN);
123
fontStyle += (jchkItalic.isSelected() ? Font.ITALIC : Font.PLAIN);
124
125 // Set font for the message
126 Font font = jlblMessage.getFont();
127 jlblMessage.setFont(
128
set a new font
new Font(font.getName(), fontStyle, font.getSize()));
129 }
130 }
The program creates a label, check boxes, radio buttons, and a text field (lines 7-19). It
places a label in the center of the frame (lines 31-32), check boxes in the east (lines 35-39),
radio buttons in the west (lines 42-47), and a text field in the north (lines 60-66).
The program also sets mnemonics for check boxes and radio buttons (lines 69-73). You
can use a mouse click or a shortcut key to select a check box or a radio button.
mnemonic keys
 
Search WWH ::




Custom Search