Advertisement
aznGiLL

Innlevering2 GUI

Mar 11th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package Innlevering2;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.GridLayout;
  6. import javax.swing.*;
  7.  
  8. public class Oppgave2 extends JFrame implements ActionListener {
  9.    
  10.     private static final long serialVersionUID = 1L;
  11.     JFrame frame = new JFrame();
  12.     private JLabel tall, resultat, blank;
  13.     private JTextField textfield;
  14.     private JButton kvadrat, kvadratrot;
  15.    
  16.     public Oppgave2(){
  17.         super("Minikalkulator");
  18.         setSize(300, 100);
  19.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20.         setLayout(new GridLayout(3,2));
  21.         setVisible(true);
  22.        
  23.         tall = new JLabel();
  24.         textfield = new JTextField();
  25.         resultat = new JLabel();
  26.         blank = new JLabel();
  27.         kvadrat = new JButton();
  28.         kvadratrot = new JButton();
  29.        
  30.         tall.setText("Tall");
  31.         resultat.setText("Resultat");
  32.         kvadrat.setText("Kvadrat");
  33.         kvadratrot.setText("Kvadratrot");
  34.        
  35.         kvadrat.addActionListener(this);
  36.         kvadratrot.addActionListener(this);
  37.        
  38.         add(tall);
  39.         add(textfield);
  40.         add(resultat);
  41.         add(blank);
  42.         add(kvadrat);
  43.         add(kvadratrot);
  44.     }
  45.    
  46.     public void actionPerformed(ActionEvent e) {
  47.         if (e.getSource() == kvadrat) {
  48.             JOptionPane.showMessageDialog(null, "Kvadrat ble klikket");
  49.         } else if(e.getSource() == kvadratrot) {
  50.             JOptionPane.showMessageDialog(null, "Kvadratrot ble klikket");
  51.         }
  52.     }
  53.    
  54. /*   public void actionPerformed(ActionEvent a) {
  55.           double sum = 0.0;
  56.            String data = a.getActionCommand();
  57.            String num = txtInput.getText();
  58.            double tall = Double.parseDouble(num); //gjør teksten om til tall
  59.            String res = ""; //res = resultat
  60.            
  61.            if(data.equals("Kvadrat")) { //Hvis knappen "Kvadrat" blir trykket - > ganger seg med seg selv.
  62.             sum = tall * tall;  
  63.            }
  64.            if(data.equals("Kvadratrot")) { //Hvis knappen "Kvadratrot" blir trykket -> finner kvadratroten.
  65.             sum = Math.sqrt(tall);
  66.            }
  67.      }
  68. */
  69.    
  70.  
  71.    
  72.     public static void main(String[] args){
  73.         new Oppgave2();
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement