Advertisement
Paarzivall

Untitled

May 8th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. package kolos4_A;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.Toolkit;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. import javax.swing.BorderFactory;
  10. import javax.swing.JButton;
  11. import javax.swing.JCheckBox;
  12. import javax.swing.JComboBox;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.JTextField;
  17.  
  18. public class MainFrame extends JFrame implements Runnable{
  19.     private JPanel panel;
  20.     private JTextField liczba_1, liczba_2;
  21.     private JComboBox<String> akcja = new JComboBox(new String[] {"Plus", "Minus", "Razy"});
  22.            
  23.     private JLabel wynik;
  24.     private JButton wykonaj;
  25.    
  26.     public MainFrame(String Tytul) {
  27.         super(Tytul);
  28.         Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  29.         setSize(new Dimension(dim.width/2, dim.height/2));
  30.        
  31.         panel = new JPanel();
  32.         panel.setBorder(BorderFactory.createRaisedBevelBorder());
  33.         add(panel, BorderLayout.NORTH);
  34.        
  35.         liczba_1 = new JTextField(10);
  36.         liczba_2 = new JTextField(10);
  37.         panel.add(liczba_1);
  38.         panel.add(liczba_2);
  39.        
  40.         panel.add(akcja);
  41.        
  42.         wykonaj = new JButton("Wykonaj");
  43.         panel.add(wykonaj);
  44.        
  45.         wynik = new JLabel();
  46.         panel.add(wynik);
  47.         wykonaj.addActionListener(new ActionListener() {
  48.            
  49.             @Override
  50.             public void actionPerformed(ActionEvent e) {
  51.                 Double a = Double.parseDouble(get_A());
  52.                 Double b = Double.parseDouble(get_B());
  53.                
  54.                 if(get_akcja() == "Plus") {
  55.                     Double w = a + b;
  56.                     wynik.setText("Wynik: " + w);
  57.                 }
  58.                 else if(get_akcja() == "Minus") {
  59.                     Double w = a - b;
  60.                     wynik.setText("Wynik: " + w);
  61.                 }
  62.                 else {
  63.                     Double w = a * b;
  64.                     wynik.setText("Wynik: " + w);
  65.                 }
  66.             }
  67.         });
  68.        
  69.  
  70.        
  71.     }
  72.  
  73.     @Override
  74.     public void run() {
  75.         setVisible(true);
  76.     }
  77.    
  78.     public String get_A() {
  79.         return liczba_1.getText();
  80.     }
  81.    
  82.     public String get_B() {
  83.         return liczba_2.getText();
  84.     }
  85.    
  86.     public String get_akcja() {
  87.         return akcja.getSelectedItem().toString();
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement