JAVA-GUI用单选按钮控制标签的字体名称

仅供学习
仅供学习
仅供学习

用单选按钮控制标签的字体名称
JAVA-GUI用单选按钮控制标签的字体名称_第1张图片

package Java作业;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class dame02 {
    public static class cannian  extends JFrame{
        JLabel jLabel1,jLabel2;
        JRadioButton jRadioButton1,jRadioButton2,jRadioButton3;
        ButtonGroup buttonGroup1;
        public cannian(){
            super("单选按钮应用");
            jLabel1=new JLabel("字体名称:");
            jLabel2=new JLabel("这是一个单选按钮示例");
            jRadioButton1=new JRadioButton("黑体");
            jRadioButton2=new JRadioButton("楷体");
            jRadioButton3=new JRadioButton("隶属");
            buttonGroup1=new ButtonGroup();
            buttonGroup1.add(jRadioButton1);
            buttonGroup1.add(jRadioButton2);
            buttonGroup1.add(jRadioButton3);
            setLayout(null);
            jLabel1.setBounds(30,10,70,30);
            jRadioButton1.setBounds(90,10,70,30);
            jRadioButton2.setBounds(180,10,70,30);
            jRadioButton3.setBounds(270,10,70,30);
            jLabel2.setBounds(90,150,300,100);
            jLabel2.setFont(new Font("华文彩云", Font.PLAIN, 20));
            add(jLabel1);
            add(jRadioButton1);
            add(jRadioButton2);
            add(jRadioButton3);
            add(jLabel2);
            setSize(400,300);
            ButtonListener a1 = new ButtonListener();
            jRadioButton1.addActionListener(a1);
            jRadioButton2.addActionListener(a1);
            jRadioButton3.addActionListener(a1);
            setVisible(true);
            this.setLocationRelativeTo(null);
            this.setResizable(false);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public class ButtonListener implements ActionListener {
            //重写ActionListener接口中的事件处理方法actionPerformed()
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if(source==jRadioButton1){
                    jLabel2.setFont(new Font("黑体", Font.PLAIN, 20));
                }
                if(source==jRadioButton2){
                    jLabel2.setFont(new Font("楷体", Font.PLAIN, 20));
                }
                if(source==jRadioButton3){
                    jLabel2.setFont(new Font("隶属", Font.PLAIN, 20));
                }
            }}
    }

    public static void main(String[] args) {
        new cannian();
    }
}

效果图:
JAVA-GUI用单选按钮控制标签的字体名称_第2张图片

你可能感兴趣的:(日常作业)