VasilM

AWTHomework4

Apr 9th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package awtHomework4;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class AWTHomework4 extends Frame implements ActionListener {
  6.  
  7.     CheckboxGroup grp1 = new CheckboxGroup();
  8.     CheckboxGroup grp2 = new CheckboxGroup();
  9.     Checkbox ascending = new Checkbox("Ascending", grp1, true);
  10.     Checkbox descending = new Checkbox("Descending", grp1, false);
  11.     Checkbox s1 = new Checkbox("Step 1", grp2, true);
  12.     Checkbox s5 = new Checkbox("Step 5", grp2, false);
  13.     Checkbox s10 = new Checkbox("Step 10", grp2, false);
  14.     int res = 0;
  15.     Label result = new Label( ""+res, Label.RIGHT);
  16.     Button clickMe = new Button("ClickMe!");
  17.     Button clear = new Button("Clear!");
  18.     Panel west = new Panel(new GridLayout(0,1));
  19.     Panel center = new Panel();
  20.     Panel east = new Panel(new GridLayout(0,1));
  21.        
  22.     public AWTHomework4() {    
  23.         west.add( ascending);
  24.         west.add( descending);
  25.         west.add(new Label());
  26.         west.add( s1);
  27.         west.add( s5);
  28.         west.add( s10);
  29.        
  30.         clickMe.addActionListener(this);
  31.         clear.addActionListener(this);
  32.         east.add( clickMe);
  33.         east.add( result);
  34.         east.add( clear);
  35.        
  36.         this.add( west, BorderLayout.WEST);
  37.         this.add( center, BorderLayout.CENTER);
  38.         this.add( east, BorderLayout.EAST);
  39.         this.setTitle("ButtonCounterDemo");
  40.         this.setSize(300, 150);
  41.         this.setVisible(true);
  42.         this.addWindowListener(new ExitProgram());
  43.     }
  44.    
  45.     public void actionPerformed(ActionEvent e) {
  46.         Button x = (Button)e.getSource();
  47.         if(x.equals(clickMe)) {
  48.             if(s1.getState()) {
  49.                 if(ascending.getState()) res += 1;
  50.                 else res -= 1;
  51.                 result.setText( ""+res );
  52.             }
  53.             if(s5.getState()) {
  54.                 if(ascending.getState()) res += 5;
  55.                 else res -= 5;
  56.                 result.setText( ""+res );
  57.             }
  58.             if(s10.getState()) {
  59.                 if(ascending.getState()) res += 10;
  60.                 else res -= 10;
  61.                 result.setText( ""+res );
  62.             }
  63.         }
  64.         if(x.equals(clear)) {
  65.             res = 0;
  66.             result.setText( ""+res);
  67.         }
  68.     }
  69.    
  70.     public static void main(String [] args) {
  71.         new AWTHomework4();
  72.     }
  73. }
  74.  
  75. class ExitProgram extends WindowAdapter {
  76.     public void windowClosing( WindowEvent e ) {
  77.         System.exit(0);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment