编程语言
首页 > 编程语言> > Java二级-Swing实现复选按钮

Java二级-Swing实现复选按钮

作者:互联网

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

public class Hello extends JFrame{
	
	private JLabel lab;
	private JCheckBox box1,box2;
	
	public  Hello(){
		super("复选按钮");
		setSize(300,300);
		setLocation(300,300);
		setLayout(new FlowLayout());
		Container ban=getContentPane();
		lab=new JLabel("you do not choose");
		box1=new JCheckBox("zhu",false);
		box2=new JCheckBox("laz",false);
		ban.add(lab);
		ban.add(box1);
		ban.add(box2);
		box1.addItemListener(new itemevent());
		box2.addItemListener(new itemevent());
		setVisible(true);
	}
	
	public static void main(String args[])
	{
		Hello tt=new Hello();
		tt.addWindowListener( new win());
		
	}
	
	class itemevent implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource()==box1) {
				if(e.getStateChange()==ItemEvent.SELECTED)
					lab.setText("you choose zhu");
				else {
					lab.setText("you do not choose zhu");
				}
			}
			if(e.getSource()==box2) {    
			    if(e.getStateChange()==ItemEvent.SELECTED) 
			    	lab.setText("you choose laz");
			    else {
			    	lab.setText("you do not choose zhu");
			    }
			
			}
		}		
	}
	
	static class win extends WindowAdapter{
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
	
}

 

标签:Java,复选,lab,choose,Swing,new,box1,public,box2
来源: https://www.cnblogs.com/zhulmz/p/11514873.html