1、按钮组件
常见组件:JButton、JCheckBox、JRadioButton
JCheckBox
复选框组件
常用构造方法
组件基本用法
代码如下
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CheckBoxTest {
static JPanel jp;
static JLabel jl;
static JCheckBox jc1,jc2;
public static void initCheckbox() {
JFrame jf=new JFrame(“checkbox”);
jf.setLayout(new BorderLayout());
jf.setSize(300, 400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jl=new JLabel("Hello World!",JLabel.CENTER);
jl.setFont(new Font("宋体",Font.PLAIN,20));
jp=new JPanel();
jc1=new JCheckBox("ITALIC",true);
jc2=new JCheckBox("BOLD");
ActionListener ac=new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int m=0;
if(jc1.isSelected())
{
m+=Font.BOLD;
}
if(jc2.isSelected())
{
m+=Font.ITALIC;
}
jl.setFont(new Font("宋体",m,20));
}};
jc1.addActionListener(ac);
jc2.addActionListener(ac);