Java二级-Swing实现复选按钮
Posted zhulmz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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二级-Swing实现复选按钮的主要内容,如果未能解决你的问题,请参考以下文章