Advertisement
Nick-O-Rama

firstGUI

Feb 20th, 2015
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3.  
  4. public class FirstWindow extends JFrame{
  5.     private JLabel lblQuantity;
  6.     private JLabel lblPrice;
  7.     private JTextField txtQuantity;
  8.     private JTextField txtPrice;
  9.     private JTextField txtResult;
  10.     private JButton btnCalculate;
  11.     private JPanel panel;
  12.    
  13.     public FirstWindow()
  14.     {
  15.         super("First Application!!!");
  16.         setSize(250,300);
  17.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         setVisible(true);
  19.         buildPanel();
  20.         add(panel);
  21.         //pack();
  22.     }
  23.    
  24.     public void buildPanel()
  25.     {
  26.         panel = new JPanel();
  27.         lblQuantity = new JLabel("Enter a quantity");
  28.         lblPrice = new JLabel("Enter a price");
  29.         txtQuantity = new JTextField("test", 15);
  30.         txtPrice = new JTextField("test", 15);
  31.         btnCalculate = new JButton("Calculate!");
  32.         txtResult = new JTextField(10);
  33.         panel.add(lblQuantity);
  34.         panel.add(txtQuantity);
  35.         panel.add(lblPrice);
  36.         panel.add(txtPrice);
  37.         panel.add(txtResult);
  38.         panel.add(btnCalculate);
  39.         btnCalculate.addActionListener(new CalcButtonListener());
  40.     }
  41.    
  42.     private class CalcButtonListener implements ActionListener
  43.     {
  44.         public void actionPerformed(ActionEvent e)
  45.         {
  46.             String strQuantity = txtQuantity.getText();
  47.             String strPrice = txtPrice.getText();
  48.             double result = Double.parseDouble(strQuantity) * Double.parseDouble(strPrice);
  49.             txtResult.setText(result + "");
  50.            
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement