15 April 2016

JSpinner in Eclipse - Product price 2

Example: JSpinner in Eclipse - Product price 
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Font;

public class prod extends JFrame {

 private JPanel contentPane;
 private JSpinner spinner;
 private JLabel txtPrecio;
 private JLabel txtStock;
 private JButton btnGet;
 private int maxStock=50;
 private int intPrice=10;
 private JLabel lblTotal;
 private JTextField txtTotal;
 private JTextField txtTotales;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     prod frame = new prod();
     frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the frame.
  */
 public prod() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 310, 280);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  
  JLabel lblStock = new JLabel("Stock");
  lblStock.setBounds(15, 33, 52, 14);
  
  JLabel lblPeecio = new JLabel("Price");
  lblPeecio.setBounds(15, 65, 52, 14);
  
  txtStock = new JLabel("");
  txtStock.setFont(new Font("Tahoma", Font.BOLD, 11));
  txtStock.setBounds(77, 33, 90, 14);
  
  txtPrecio = new JLabel(intPrice+" euros");
  txtPrecio.setFont(new Font("Tahoma", Font.BOLD, 11));
  txtPrecio.setBounds(77, 65, 79, 14);
  
  spinner = new JSpinner();
  spinner.setBounds(121, 111, 135, 20);
  spinner.setModel(new SpinnerNumberModel(0, 0, maxStock, 1));  

  btnGet = new JButton("Get");
  btnGet.setBounds(164, 33, 92, 46);
  btnGet.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    hacerCompra();
   }
  });
  
  lblTotal = new JLabel("Total ");
  lblTotal.setBounds(15, 163, 69, 14);
  
  txtTotal = new JTextField();
  txtTotal.setBounds(121, 158, 135, 25);
  txtTotal.setText("0");
  txtTotal.setColumns(10);
  
  JList list = new JList();
  list.setBounds(93, 190, 1, 1);
  
  JList list_1 = new JList();
  list_1.setBounds(376, 60, 1, 1);
  
  JList list_2 = new JList();
  list_2.setBounds(298, 209, 1, 1);
  
  JScrollPane scrollPane = new JScrollPane();
  scrollPane.setBounds(266, 65, 2, 2);
  contentPane.setLayout(null);
  contentPane.add(list);
  contentPane.add(list_1);
  contentPane.add(list_2);
  contentPane.add(scrollPane);
  contentPane.add(spinner);
  contentPane.add(lblStock);
  contentPane.add(lblPeecio);
  contentPane.add(txtPrecio);
  contentPane.add(txtStock);
  contentPane.add(btnGet);
  contentPane.add(lblTotal);
  contentPane.add(txtTotal);
  
  JLabel lblStock_1 = new JLabel("Stock");
  lblStock_1.setBounds(15, 114, 69, 14);
  contentPane.add(lblStock_1);
  
  txtTotales = new JTextField();
  txtTotales.setText("0");
  txtTotales.setBounds(121, 207, 135, 20);
  contentPane.add(txtTotales);
  txtTotales.setColumns(10);
  
  JLabel lblTotales = new JLabel("Totales");
  lblTotales.setBounds(15, 210, 69, 14);
  contentPane.add(lblTotales);
 }
 void hacerCompra(){
  int valueSpinner = (Integer) spinner.getValue();
  int total = valueSpinner * intPrice;
  txtTotal.setText(total+"");
  maxStock = 50 - valueSpinner;
  txtStock.setText(Integer.toString(maxStock)+" Product");
 

  int totales = Integer.parseInt(txtTotales.getText().toString());
  totales = total+totales;
  txtTotales.setText(Integer.toString(totales));
 }

}


0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang