15 April 2016

JSpinner in Eclipse - Product price

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;

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 lblTotalDala;
 private JTextField txtTotal;

 /**
  * 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, 246, 252);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  
  JLabel lblStock = new JLabel("Stock");
  
  JLabel lblPeecio = new JLabel("Price");
  
  txtStock = new JLabel(maxStock+" productos");
  
  txtPrecio = new JLabel("10 euros");
  
  spinner = new JSpinner();
  spinner.setModel(new SpinnerNumberModel(0, 0, maxStock, 1));
  
  btnGet = new JButton("Get");
  btnGet.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    hacerCompra();
   }
  });
  
  lblTotalDala = new JLabel("Total operator");
  
  txtTotal = new JTextField();
  txtTotal.setText("0");
  txtTotal.setColumns(10);
  GroupLayout gl_contentPane = new GroupLayout(contentPane);
  gl_contentPane.setHorizontalGroup(
   gl_contentPane.createParallelGroup(Alignment.LEADING)
    .addGroup(gl_contentPane.createSequentialGroup()
     .addContainerGap()
     .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
      .addGroup(gl_contentPane.createSequentialGroup()
       .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
        .addComponent(lblStock)
        .addComponent(lblPeecio))
       .addGap(36)
       .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
        .addComponent(txtPrecio)
        .addComponent(txtStock))
       .addGap(30))
      .addGroup(gl_contentPane.createSequentialGroup()
       .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
        .addComponent(spinner, Alignment.LEADING)
        .addComponent(lblTotalDala, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
       .addGap(54)
       .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
        .addComponent(txtTotal, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
        .addComponent(btnGet))
       .addGap(239))))
  );
  gl_contentPane.setVerticalGroup(
   gl_contentPane.createParallelGroup(Alignment.LEADING)
    .addGroup(gl_contentPane.createSequentialGroup()
     .addGap(28)
     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
      .addComponent(lblStock)
      .addComponent(txtStock))
     .addGap(18)
     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
      .addComponent(lblPeecio)
      .addComponent(txtPrecio))
     .addGap(32)
     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
      .addComponent(lblTotalDala)
      .addComponent(txtTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
     .addGap(18)
     .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
      .addComponent(spinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      .addComponent(btnGet))
     .addContainerGap(85, Short.MAX_VALUE))
  );
  contentPane.setLayout(gl_contentPane);
 }
 void hacerCompra(){
  int valueSpinner = (Integer) spinner.getValue();
  int total = valueSpinner * intPrice;
  txtTotal.setText(total+"");
 }
}
Link: https://www.youtube.com/watch?v=S62TnjdFnBs


0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang