awt实现多选按钮和多选按钮的监听
第二个例子是用swt实现相同的功能。
awt 代码
第二个例子是用swt实现相同的功能。
awt 代码
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class JCheckBoxDemo implements ItemListener {
- JFrame f = null;
- JCheckBox c5 = null;
- JCheckBox c6 = null;
- JCheckBoxDemo() {
- f = new JFrame("JCheckBox");
- Container contentPane = f.getContentPane();
- contentPane.setLayout(new GridLayout(2, 1));
- JPanel p1 = new JPanel();
- p1.setLayout(new GridLayout(1, 3));
- //建立一个标题边界,并指定标题名称,其他为默认值。
- p1.setBorder(BorderFactory.
- createTitledBorder("您最喜欢哪一家餐厅呢?"));
- JCheckBox c1 = new JCheckBox("好伦哥");
- JCheckBox c2 = new JCheckBox("凯威啤酒屋");
- JCheckBox c3 = new JCheckBox("牛肉面大王");
- JCheckBox c4 = new JCheckBox("川王府");
- p1.add(c1);
- p1.add(c2);
- p1.add(c3);
- p1.add(c4);
- JPanel p2 = new JPanel();
- p2.setLayout(new GridLayout(2, 1));
- p2.setBorder(BorderFactory.
- createTitledBorder("您喜欢哪种编程语言,请选择:"));
- c5 = new JCheckBox("JAVA", new ImageIcon(".\\icons\\x.gif"));
- c6 = new JCheckBox("PHP", new ImageIcon(".\\icons\\x.gif"));
- c5.addItemListener(this);
- c6.addItemListener(this);
- p2.add(c5);
- p2.add(c6);
- contentPane.add(p1);
- contentPane.add(p2);
- f.pack();
- f.show();
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- }
- public void itemStateChanged(ItemEvent e) {
- if (e.getStateChange() == e.SELECTED) {
- if (e.getSource() == c5)
- c5.setIcon(new ImageIcon(".\\icons\\r.gif"));
- if (e.getSource() == c6)
- c6.setIcon(new ImageIcon(".\\icons\\r.gif"));
- } else {
- if (e.getSource() == c5)
- c5.setIcon(new ImageIcon(".\\icons\\x.gif"));
- if (e.getSource() == c6)
- c6.setIcon(new ImageIcon(".\\icons\\x.gif"));
- }
- }
- public static void main(String args[]) {
- new JCheckBoxDemo();
- }
- }
swt 代码
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.graphics.Image;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.layout.RowLayout;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Event;
- import org.eclipse.swt.widgets.Group;
- import org.eclipse.swt.widgets.Listener;
- import org.eclipse.swt.widgets.Shell;
- public class SwtCheckBoxDemo {
- SwtCheckBoxDemo() {
- Display display = new Display();
- Shell shell = new Shell(display);
- shell.setText("Using SWT");
- shell.setSize(350, 280);
- shell.setLayout(new RowLayout());
- //建立一个标题边界,并指定标题名称,其他为默认值。
- Group group1 = new Group(shell, SWT.SHADOW_IN);
- group1.setText("您最喜欢哪一家速食店呢?");
- group1.setLayout(new FillLayout(SWT.VIRTUAL));
- Button c1= new Button(group1, SWT.CHECK);
- c1.setText("好伦哥");
- Button c2= new Button(group1, SWT.CHECK);
- c2.setText("凯威啤酒屋");
- Button c3= new Button(group1, SWT.CHECK);
- c3.setText("牛肉面大王");
- Button c4= new Button(group1, SWT.CHECK);
- c4.setText("川王府");
- Group group2 = new Group(shell, SWT.SHADOW_IN);
- group2.setText("您喜欢哪种编程语言,请选择:");
- group2.setLayout(new RowLayout(SWT.VERTICAL));
- final Button c5 = new Button(group2, SWT.CHECK);
- c5.setText("JAVA");
- final Image xgif = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
- ".\\icons\\x.gif"));
- c5.setImage(xgif);
- final Button c6= new Button(group2, SWT.CHECK);
- c6.setText("PHP");
- final Image rgif = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
- ".\\icons\\r.gif"));
- c6.setImage(xgif);
- c5.addSelectionListener(new SelectionAdapter( ) {
- public void widgetSelected(SelectionEvent e) {
- // System.out.println(c5.getSelection( ));
- if(c5.getSelection()==true){
- c5.setImage(rgif);
- }else{
- c5.setImage(xgif);
- }
- }
- });
- c6.addSelectionListener(new SelectionAdapter( ) {
- public void widgetSelected(SelectionEvent e) {
- if(c6.getSelection()==true){
- c6.setImage(rgif);
- }else{
- c6.setImage(xgif);
- }
- }
- });
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch()) {
- display.sleep();
- }
- }
- display.dispose();
- }
- public static void main(String args[]) {
- new SwtCheckBoxDemo();
- }
- }