Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import javax.swing.JRadioButton;
- import javax.swing.ButtonGroup;
- class RadioButtons {
- public static void main(String args[ ]) {
- Gui gui = new Gui();
- gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- gui.setSize(320, 100);
- gui.setVisible(true);
- }
- }
- class Gui extends JFrame {
- private JTextField text;
- private JRadioButton plain, bold, italic, boldItalic;
- private Font plainFont, boldFont, italicFont, boldItalicFont;
- Gui() {
- super("My radio buttons");
- setLayout(new FlowLayout());
- plainFont = new Font("Serif", Font.PLAIN, 14);
- boldFont = new Font("Serif", Font.BOLD, 14);
- italicFont = new Font("Serif", Font.ITALIC, 14);
- boldItalicFont = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
- text = new JTextField("This my super awesome sentence.", 23);
- text.setFont(plainFont);
- plain = new JRadioButton("Plain", true);
- bold = new JRadioButton("Bold", false);
- italic = new JRadioButton("Italic", false);
- boldItalic = new JRadioButton("Bold and italic", false);
- add(text);
- add(plain);
- add(bold);
- add(italic);
- add(boldItalic);
- ButtonGroup gr = new ButtonGroup();
- gr.add(plain);
- gr.add(bold);
- gr.add(italic);
- gr.add(boldItalic);
- plain.addItemListener(new handler(plainFont));
- bold.addItemListener(new handler(boldFont));
- italic.addItemListener(new handler(italicFont));
- boldItalic.addItemListener(new handler(boldItalicFont));
- }
- private class handler implements ItemListener {
- Font font;
- handler(Font f) {
- font = f;
- }
- public void itemStateChanged(ItemEvent event) {
- text.setFont(font);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement