java

 
 
 
 
 
 
package edu.jju;

import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

/**
 *
 * @author Administrator
 */
public class myFrame extends JFrame {

    public myFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 400);
        setLocation(100, 100);
        JTextArea textArea = new JTextArea("好好学习");
        textArea.setBounds(100, 100, 10, 100);
        JPanel panel = new JPanel();
        ButtonGroup buttonGroup = new ButtonGroup();
        JRadioButton radioButton1 = new JRadioButton("普通");
        JRadioButton radioButton2 = new JRadioButton("斜体");
        JRadioButton radioButton3 = new JRadioButton("加粗");
        buttonGroup.add(radioButton3);
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
         panel.add(textArea);
        panel.add(radioButton1);
        panel.add(radioButton2);
        panel.add(radioButton3);
        add(panel);
        setVisible(true);
        radioButton1.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                textArea.setFont(new Font(textArea.getText(), Font.PLAIN, 20));
            }
        });
        radioButton2.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                textArea.setFont(new Font(textArea.getText(), Font.ITALIC, 16));
            }
        });

        radioButton3.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                textArea.setFont(new Font(textArea.getText(), Font.BOLD, 14));
            }
        });
    }
    public static void main(String[] args) {
        myFrame f=new myFrame();  
    }
<span style="color:#ff0000;"><strong>}
</strong></span>

注意:一定先添加组件,然后再设置setVisible(true),不然的话运行后会看不到任何组件,

你可能感兴趣的:(java)