java图形用户之复选框与单选框、字体修改

package TextGUI;

//TestCheckRadio.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestCheckRadio extends JFrame implements ItemListener

{
              JCheckBox italic = new JCheckBox("斜体");
              JCheckBox bold = new JCheckBox("粗体");
              JRadioButton large = new JRadioButton("大字号");
              JRadioButton middle = new JRadioButton("中字号");
              JRadioButton small = new JRadioButton("小字号"); 
              JTextArea area = new JTextArea();
              TestCheckRadio()

              {
                       ButtonGroup group = new ButtonGroup();
                       group.add(large);
                       group.add(middle);
                       group.add(small);

                       setLayout(new GridLayout(1, 2, 10, 0));

                       JPanel left = new JPanel();
                       left.setLayout(new GridLayout(5, 1, 0, 10));
                       left.add(italic);
                       left.add(bold);
                       left.add(large);
                       left.add(middle);
                       left.add(small);
                       add(left);

                       add(area);

                       Font font = new Font("", 0, 25);
                       area.setFont(font);
                       area.setText("我喜欢C++,\n但更喜欢Java");

                       italic.addItemListener(this);
                       bold.addItemListener(this);
                       large.addItemListener(this);
                       middle.addItemListener(this);
                       small.addItemListener(this);

                       setSize(500, 200);
                       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                       setVisible(true);
 }
 public void itemStateChanged(ItemEvent e)

 {
              int size = 25;
              if (small.isSelected())
                      size = 15;
              if (large.isSelected())
                      size = 35;
              int type = 0;
              if (italic.isSelected())
                        type = type + Font.ITALIC;
              if (bold.isSelected())
                        type = type + Font.BOLD;
              Font font = new Font("", type, size);
              area.setFont(font);
      }           
      public static void main(String[] args)

      {
               new TestCheckRadio();
      }
}


单选框创建:JRadioButton large = new JRadioButton(“大字号”);
单选框创建以后,需要创建一个选框组,将单选框添加到这个组中,意味着在同一时刻,只能选中这个组中的一个选框;

ButtonGroup group = new ButtonGroup();
group.add(large);
group.add(middle);
group.add(small);

复选框创建:JCheckBox italic = new JCheckBox(“斜体”);
复选框创建之后,在同一时刻可以选中复选框的多个

改变字体大小以及格式

if (small.isSelected())
                      size = 15;
              if (large.isSelected())
                      size = 35;
              int type = 0;
              if (italic.isSelected())
                        type = type + Font.ITALIC;
              if (bold.isSelected())
                        type = type + Font.BOLD;
              Font font = new Font("", type, size);
              area.setFont(font);

字体大小可以直接设置,
字体形式可以直接用加的形式来改变;

判断选框是否被选中:
if (bold.isSelected())

注意
在修改字体之后 需要重新设置文本框中的子体,否则修改不生效
Font font = new Font("", type, size);
area.setFont(font);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值