ejercicio3
unknown
java
2 years ago
3.5 kB
7
Indexable
package Ejercicios;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.DefaultComboBoxModel;
public class Ejercicio3 extends JFrame implements ActionListener{
private JPanel contentPane;
private JTextField txtCantidad;
private JButton btnProcesar;
private JTextArea txtResultado;
private JButton btnBorrar;
private JComboBox cboProducto;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Ejercicio3 frame = new Ejercicio3();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Ejercicio3() {
setTitle("Tienda");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Producto:");
lblNewLabel.setBounds(27, 26, 66, 14);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Cantidad:");
lblNewLabel_1.setBounds(27, 64, 66, 14);
contentPane.add(lblNewLabel_1);
cboProducto = new JComboBox();
cboProducto.setModel(new DefaultComboBoxModel(new String[] {"P0", "P1", "P2"}));
cboProducto.setBounds(103, 22, 86, 22);
contentPane.add(cboProducto);
txtCantidad = new JTextField();
txtCantidad.setBounds(103, 61, 86, 20);
contentPane.add(txtCantidad);
txtCantidad.setColumns(10);
btnProcesar = new JButton("Procesar");
btnProcesar.setBounds(298, 22, 89, 23);
btnProcesar.addActionListener(this);
contentPane.add(btnProcesar);
btnBorrar = new JButton("Borrar");
btnBorrar.setBounds(298, 60, 89, 23);
btnBorrar.addActionListener(this);
contentPane.add(btnBorrar);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 106, 414, 144);
contentPane.add(scrollPane);
txtResultado = new JTextArea();
scrollPane.setViewportView(txtResultado);
}
@Override
public void actionPerformed(ActionEvent e) {
// Botón Procesar
if (e.getSource() == btnProcesar) {
//variables
int producto, cantidad;
//entradas
producto = obtenerProducto();
cantidad = obtenerCantidad();
//proceso
//salida
}
// Botón borrar
if (e.getSource() == btnBorrar) {
limpiar();
}
}
public void limpiar() {
txtCantidad.setText("");
cboProducto.setSelectedIndex(0);
txtResultado.setText("");
txtCantidad.requestFocus();
}
public int obtenerProducto() {
return cboProducto.getSelectedIndex();
}
public int obtenerCantidad() {
return Integer.parseInt(txtCantidad.getText());
}
public double obtenerPrecio(int producto) {
double precio = 0;
switch (producto) {
case 0: precio = 15; break;
case 1: precio = 17.5; break;
case 2: precio = 20; break;
}
return precio;
}
}
Editor is loading...
Leave a Comment