简介

checkbox

code

/*
 * @Author: your name
 * @Date: 2020-11-04 09:44:08
 * @LastEditTime: 2020-11-04 09:53:07
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: /java/calcu/CheckBoxTest.java
 */
package calcu;

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


public class CheckBoxTest extends JFrame {
    private JLabel label;
    private JCheckBox bold;
    private JCheckBox italic;
    private static final int FONTSIZE = 24;
    public static void main(String[] args){
        CheckBoxTest t = new CheckBoxTest();
        t.setTitle("ImageTest");
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setVisible(true);
    }
    public CheckBoxTest() {
        // add the sample text label

        label = new JLabel("The quick brown fox jumps over the lazy dog.");
        label.setFont(new Font("Serif", Font.BOLD, FONTSIZE));
        add(label, BorderLayout.CENTER);

        // this listener sets the font attribute of
        // the label to the checkbox state

        ActionListener listener = event -> {
            int mode = 0;
            if (bold.isSelected())
                mode += Font.BOLD;
            if (italic.isSelected())
                mode += Font.ITALIC;
            label.setFont(new Font("Serif", mode, FONTSIZE));
        };
        // add the check boxes
        JPanel buttonPanel = new JPanel();

        bold = new JCheckBox("Bold");
        bold.addActionListener(listener);
        bold.setSelected(true);
        buttonPanel.add(bold);

        italic = new JCheckBox("Italic");
        italic.addActionListener(listener);

        buttonPanel.add(italic);

        add(buttonPanel, BorderLayout.SOUTH);
        pack();
    }
}

Q&A

简单

posted on 2020-11-04 09:55  HDU李少帅  阅读(301)  评论(0编辑  收藏  举报