Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import java.nio.channels.InterruptibleChannel;
  2.  
  3. import org.eclipse.swt.widgets.Display;
  4. import org.eclipse.swt.widgets.Shell;
  5. import org.eclipse.swt.widgets.Text;
  6. import org.eclipse.swt.SWT;
  7. import org.eclipse.swt.widgets.Combo;
  8. import org.eclipse.swt.widgets.Button;
  9. import org.eclipse.swt.widgets.Label;
  10. import org.eclipse.swt.events.SelectionAdapter;
  11. import org.eclipse.swt.events.SelectionEvent;
  12.  
  13.  
  14. public class App0207 {
  15.  
  16.     protected Shell shlNasKalkulator;
  17.     private Text txtPrviBroj;
  18.     private Text txtDrugiBroj;
  19.  
  20.     /**
  21.      * Launch the application.
  22.      * @param args
  23.      */
  24.     public static void main(String[] args) {
  25.         try {
  26.             App0207 window = new App0207();
  27.             window.open();
  28.         } catch (Exception e) {
  29.             e.printStackTrace();
  30.         }
  31.     }
  32.  
  33.     /**
  34.      * Open the window.
  35.      */
  36.     public void open() {
  37.         Display display = Display.getDefault();
  38.         createContents();
  39.         shlNasKalkulator.open();
  40.         shlNasKalkulator.layout();
  41.         while (!shlNasKalkulator.isDisposed()) {
  42.             if (!display.readAndDispatch()) {
  43.                 display.sleep();
  44.             }
  45.         }
  46.     }
  47.  
  48.     /**
  49.      * Create contents of the window.
  50.      */
  51.     protected void createContents() {
  52.         shlNasKalkulator = new Shell();
  53.         shlNasKalkulator.setSize(222, 241);
  54.         shlNasKalkulator.setText("Nas kalkulator");
  55.        
  56.         txtPrviBroj = new Text(shlNasKalkulator, SWT.BORDER);
  57.         txtPrviBroj.setBounds(53, 37, 91, 21);
  58.  
  59.         txtDrugiBroj = new Text(shlNasKalkulator, SWT.BORDER);
  60.         txtDrugiBroj.setBounds(52, 64, 92, 21);
  61.        
  62.  
  63.         final Combo cmbOperacija = new Combo(shlNasKalkulator, SWT.READ_ONLY);
  64.         cmbOperacija.setItems(new String[] {"Sabiranje", "Oduzimanje", "Mnozenje", "Deljenje"});
  65.         cmbOperacija.setBounds(53, 91, 91, 23);
  66.         cmbOperacija.select(0);
  67.  
  68.        
  69.         Button btnIzracunaj = new Button(shlNasKalkulator, SWT.NONE);
  70.        
  71.         final Label lblRezultat = new Label(shlNasKalkulator, SWT.NONE);
  72.         lblRezultat.setBounds(53, 161, 55, 15);
  73.         lblRezultat.setText("New Label");
  74.        
  75.        
  76.         btnIzracunaj.addSelectionListener(new SelectionAdapter() {
  77.             public void widgetSelected(SelectionEvent e) {
  78.                
  79.                
  80.                
  81.                 //System.out.println(txtPrviBroj.getText());
  82.                 //System.out.println(txtDrugiBroj.getText());
  83.                
  84.                     try {
  85.                         int prviBroj = Integer.parseInt(txtPrviBroj.getText());
  86.                         int drugiBroj = Integer
  87.                                 .parseInt(txtDrugiBroj.getText());
  88.                         int rezultat = 0;
  89.                         if (cmbOperacija.getSelectionIndex() == 0) {
  90.                             rezultat = prviBroj + drugiBroj;
  91.                         } else if (cmbOperacija.getSelectionIndex() == 1) {
  92.                             rezultat = prviBroj - drugiBroj;
  93.                         } else if (cmbOperacija.getSelectionIndex() == 2) {
  94.                             rezultat = prviBroj * drugiBroj;
  95.                         } else {
  96.                             rezultat = prviBroj / drugiBroj;
  97.                         }
  98.                         //System.out.println(rezultat);
  99.                         lblRezultat.setText(Integer.toString(rezultat));
  100.                        
  101.                     } catch (NumberFormatException textGreske1) {
  102.                        
  103.                         lblRezultat.setText("Greska! Ispravite unos!");
  104.                        
  105.                     } catch (Exception textGreske) {
  106.                         lblRezultat.setText("Greska! Deljenje sa nulom!");
  107.                     }
  108.                
  109.             }
  110.         });
  111.         btnIzracunaj.setBounds(53, 120, 75, 25);
  112.         btnIzracunaj.setText("Izra\u010Dunaj");
  113.        
  114.  
  115.        
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement