JButton 按钮,JRadioJButton单选按钮,JChectBox复选框

按钮JButton

//导入Java类
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo extends JFrame {
        public Demo(){
        setBounds(100,100,400,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
         Container c=getContentPane();
         c.setLayout(new GridLayout(2,3,5,5));
         JButton btn[]=new JButton[6];
         for(int i=0;i<btn.length;i++){
             btn[i]=new JButton();//创建btn[i]的可实例化对象
             c.add(btn[i]);
}
        btn[0].setText("不可用");
        btn[0].setEnabled(false);//用布尔类型设置组件不可用
       
        btn[1].setText("背景颜色");
        btn[1].setBackground(Color.BLUE);
   
        btn[2].setText("无边框");
        btn[2].setBorderPainted(false);//用布尔类型设置图片不可用
 

        btn[3].setText("有边框");
        //用线框工厂创建组件边框
        btn[3].setBorder(BorderFactory.creatLineBorder(Color.blue));

        Icon icon=new ImageIcon("src/5.PNG");//获取图片文件
        btn[4].setIcon(icon);
 
        btn[5].setText("可点击");
        btn[5].addActionListener(new ActionListener()//添加监听            
    public void actionPerformed(ActionEvent e) {//监听触发的方法代码
JOptionPane.showMessageDialog(Demo.this,"点击按钮")//可弹出小对话框
}
});
setVisible(true);
//可弹出小对话框 } }); setVisible(true); }
public static void main(String[] args) { new Demo();
}
}有错
}

猜你喜欢

转载自www.cnblogs.com/mld1040871703/p/10099679.html