多选按钮和监听按钮

awt实现多选按钮和多选按钮的监听
第二个例子是用swt实现相同的功能。

awt 代码
 
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4.   
  5. public class JCheckBoxDemo implements ItemListener {  
  6.     JFrame f = null;  
  7.     JCheckBox c5 = null;  
  8.     JCheckBox c6 = null;  
  9.   
  10.     JCheckBoxDemo() {  
  11.         f = new JFrame("JCheckBox");  
  12.         Container contentPane = f.getContentPane();  
  13.         contentPane.setLayout(new GridLayout(21));  
  14.         JPanel p1 = new JPanel();  
  15.         p1.setLayout(new GridLayout(13));  
  16.         //建立一个标题边界,并指定标题名称,其他为默认值。  
  17.         p1.setBorder(BorderFactory.  
  18.                 createTitledBorder("您最喜欢哪一家餐厅呢?"));  
  19.         JCheckBox c1 = new JCheckBox("好伦哥");  
  20.         JCheckBox c2 = new JCheckBox("凯威啤酒屋");  
  21.         JCheckBox c3 = new JCheckBox("牛肉面大王");  
  22.         JCheckBox c4 = new JCheckBox("川王府");  
  23.         p1.add(c1);  
  24.         p1.add(c2);  
  25.         p1.add(c3);  
  26.         p1.add(c4);  
  27.         JPanel p2 = new JPanel();  
  28.         p2.setLayout(new GridLayout(21));  
  29.         p2.setBorder(BorderFactory.  
  30.                 createTitledBorder("您喜欢哪种编程语言,请选择:"));  
  31.         c5 = new JCheckBox("JAVA"new ImageIcon(".\\icons\\x.gif"));  
  32.         c6 = new JCheckBox("PHP"new ImageIcon(".\\icons\\x.gif"));  
  33.   
  34.         c5.addItemListener(this);  
  35.         c6.addItemListener(this);  
  36.         p2.add(c5);  
  37.         p2.add(c6);  
  38.         contentPane.add(p1);  
  39.         contentPane.add(p2);  
  40.         f.pack();  
  41.         f.show();  
  42.         f.addWindowListener(new WindowAdapter() {  
  43.             public void windowClosing(WindowEvent e) {  
  44.                 System.exit(0);  
  45.             }  
  46.         });  
  47.     }  
  48.   
  49.     public void itemStateChanged(ItemEvent e) {  
  50.         if (e.getStateChange() == e.SELECTED) {  
  51.             if (e.getSource() == c5)  
  52.                 c5.setIcon(new ImageIcon(".\\icons\\r.gif"));  
  53.             if (e.getSource() == c6)  
  54.                 c6.setIcon(new ImageIcon(".\\icons\\r.gif"));  
  55.   
  56.         } else {  
  57.             if (e.getSource() == c5)  
  58.                 c5.setIcon(new ImageIcon(".\\icons\\x.gif"));  
  59.             if (e.getSource() == c6)  
  60.                 c6.setIcon(new ImageIcon(".\\icons\\x.gif"));  
  61.         }  
  62.     }  
  63.   
  64.     public static void main(String args[]) {  
  65.         new JCheckBoxDemo();  
  66.     }  
  67. }  


swt 代码
 
  1. import org.eclipse.swt.SWT;  
  2. import org.eclipse.swt.events.SelectionAdapter;  
  3. import org.eclipse.swt.events.SelectionEvent;  
  4. import org.eclipse.swt.graphics.Image;  
  5. import org.eclipse.swt.layout.FillLayout;  
  6. import org.eclipse.swt.layout.RowLayout;  
  7. import org.eclipse.swt.widgets.Button;  
  8. import org.eclipse.swt.widgets.Display;  
  9. import org.eclipse.swt.widgets.Event;  
  10. import org.eclipse.swt.widgets.Group;  
  11. import org.eclipse.swt.widgets.Listener;  
  12. import org.eclipse.swt.widgets.Shell;  
  13.   
  14. public class SwtCheckBoxDemo {  
  15.   
  16.   
  17.   
  18.   
  19.   
  20.     SwtCheckBoxDemo() {  
  21.         Display display = new Display();  
  22.         Shell shell = new Shell(display);  
  23.         shell.setText("Using SWT");  
  24.         shell.setSize(350280);  
  25.         shell.setLayout(new RowLayout());  
  26.   
  27.   
  28.         //建立一个标题边界,并指定标题名称,其他为默认值。  
  29.         Group group1 = new Group(shell, SWT.SHADOW_IN);  
  30.         group1.setText("您最喜欢哪一家速食店呢?");  
  31.         group1.setLayout(new FillLayout(SWT.VIRTUAL));  
  32.         Button  c1=  new Button(group1, SWT.CHECK);  
  33.                 c1.setText("好伦哥");  
  34.         Button  c2=  new Button(group1, SWT.CHECK);  
  35.                 c2.setText("凯威啤酒屋");  
  36.         Button  c3=  new Button(group1, SWT.CHECK);  
  37.                 c3.setText("牛肉面大王");  
  38.                 Button  c4=  new Button(group1, SWT.CHECK);  
  39.                 c4.setText("川王府");  
  40.   
  41.         Group group2 = new Group(shell, SWT.SHADOW_IN);  
  42.                 group2.setText("您喜欢哪种编程语言,请选择:");  
  43.                group2.setLayout(new RowLayout(SWT.VERTICAL));  
  44.   
  45.          final Button c5 = new Button(group2, SWT.CHECK);  
  46.   
  47.          c5.setText("JAVA");  
  48.          final Image xgif = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(  
  49.          ".\\icons\\x.gif"));  
  50.          c5.setImage(xgif);  
  51.          final Button  c6=  new Button(group2, SWT.CHECK);  
  52.          c6.setText("PHP");  
  53.          final Image rgif = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(  
  54.          ".\\icons\\r.gif"));  
  55.          c6.setImage(xgif);  
  56.   
  57.   
  58.          c5.addSelectionListener(new SelectionAdapter( ) {  
  59.                 public void widgetSelected(SelectionEvent e) {  
  60. //                  System.out.println(c5.getSelection( ));  
  61.                     if(c5.getSelection()==true){  
  62.                         c5.setImage(rgif);  
  63.                     }else{  
  64.                         c5.setImage(xgif);  
  65.                     }  
  66.                 }  
  67.             });  
  68.             c6.addSelectionListener(new SelectionAdapter( ) {  
  69.                 public void widgetSelected(SelectionEvent e) {  
  70.                     if(c6.getSelection()==true){  
  71.                         c6.setImage(rgif);  
  72.                     }else{  
  73.                         c6.setImage(xgif);  
  74.                     }  
  75.                 }  
  76.             });  
  77.   
  78.   
  79.                 shell.open();  
  80.                 while (!shell.isDisposed()) {  
  81.                   if (!display.readAndDispatch()) {  
  82.                     display.sleep();  
  83.                   }  
  84.                 }  
  85.                 display.dispose();  
  86.               }  
  87.   
  88.     public static void main(String args[]) {  
  89.         new SwtCheckBoxDemo();  
  90.     }  
  91. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值