Hi, I would like to know how I can arrange the buttons horizontally for the next window.
This is my code:
import javax.swing.table.AbstractTableModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.text.DecimalFormat;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class MatrizGrafica extends JPanel {
private JTable tabla;
public MatrizGrafica() {
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
tabla = new JTable(new MiTablaModelo());
tabla.setPreferredScrollableViewportSize(new Dimension(900, 200));
tabla.setFillsViewportHeight(true);
add(new JScrollPane(tabla));
}
static class MiTablaModelo extends AbstractTableModel {
//private String [] dias = {"Nombres","Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo"};
private String[] dias = {"Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo"};
private String[] nombres = {"Pedro Hernández", "Juan Cardona", "Ana Magaña", "Carlos Soriano", "Roberto Durán", "María López", "Luisa Tobar", "Jorge Escalante", "Roxana Flores", "Rosa Cea"};
//Object [][] datos = {
//{"Pedro Hernández", 123.65, 113.23, 143.23, 131.34, 153.32, 195.34, 125.98},
//{"Juan Cardona",139.56, 139.39, 131.98, 183.47, 162.27, 150.06, 191.84},
//{"Ana Magaña",169.61, 191.91, 119.87, 132.71, 125.21, 107.61, 119.43},
//{"Carlos Soriano", 169.16, 129.29, 151.88, 133.77, 192.27, 160.76, 181.34},
//{"Roberto Durán",159.66, 179.89, 131.98, 198.47, 162.74, 110.55, 153.65},
//{"María López",132.89, 139.76, 183.84, 136.31, 140.46, 171.62, 118.23},
//{"Luisa Tobar",134.46, 145.23, 175.38, 153.73, 184.28, 179.42, 153.07},
//{"Jorge Escalante",172.32, 149.72, 157.61, 128.61, 167.72, 137.71, 174.52},
//{"Roxana Flores", 129.34, 148.63, 193.38, 138.28, 120.52, 154.16, 162.18},
//{"Rosa Cea",137.84, 174.58, 173.73, 117.37, 193.25, 137.15, 127.41}
//};
Object[][] datos = {
{123.65, 113.23, 143.23, 131.34, 153.32, 195.34, 125.98},
{139.56, 139.39, 131.98, 183.47, 162.27, 150.06, 191.84},
{169.61, 191.91, 119.87, 132.71, 125.21, 107.61, 119.43},
{169.16, 129.29, 151.88, 133.77, 192.27, 160.76, 181.34},
{159.66, 179.89, 131.98, 198.47, 162.74, 110.55, 153.65},
{132.89, 139.76, 183.84, 136.31, 140.46, 171.62, 118.23},
{134.46, 145.23, 175.38, 153.73, 184.28, 179.42, 153.07},
{172.32, 149.72, 157.61, 128.61, 167.72, 137.71, 174.52},
{129.34, 148.63, 193.38, 138.28, 120.52, 154.16, 162.18},
{137.84, 174.58, 173.73, 117.37, 193.25, 137.15, 127.41}
};
//tomo el numero de columnas
public int getColumnCount() {
return dias.length;
}
// tomo el numero de filas
public int getRowCount() {
return nombres.length;
}
// nombre de las columnas
public String getColumnName(int col) {
return dias[col];
}
//nombre de las filas
public String getRowName(int fil) {
return nombres[fil];
}
public Object getValueAt(int fil, int col) {
return datos[fil][col];
}
}
static class Action1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
MiTablaModelo modelo = new MiTablaModelo();
double total = 0.0, prom = 0.0;
String[] dias = modelo.dias;
String[] vendedores = modelo.nombres;
double vMenor = (double) modelo.getValueAt(0, 0);
int fila = 0, columna = 0, i, j;
for (i = 0; i < modelo.getRowCount(); i++) {
for (j = 0; j < modelo.getColumnCount(); j++) {
if (vMenor > (double) modelo.getValueAt(i, j)) {
vMenor = (double) modelo.getValueAt(i, j);
fila = i;
columna = j;
}
}
}
for (i = 0; i < modelo.getColumnCount(); i++) {
total += (double) modelo.getValueAt(i, columna);
}
prom = total / dias.length;
DecimalFormat formato = new DecimalFormat("#,##0.00");
String mensaje = "Venta menor = " + vMenor + "\nDía de la venta = " + dias[columna] + "\nVendedor = "
+ vendedores[fila] + "\nPromedio de ventas = " + formato.format(prom);
JOptionPane.showMessageDialog(null, mensaje, "Datos de la venta menor",
JOptionPane.INFORMATION_MESSAGE);
}
}
static class Action2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
MiTablaModelo modelo = new MiTablaModelo();
double total = 0.0, prom = 0.0;
String[] dias = modelo.dias;
String[] vendedores = modelo.nombres;
double vMayor = (double) modelo.getValueAt(0, 0);
int fila = 0, columna = 0, i, j;
for (i = 0; i < modelo.getRowCount(); i++) {
for (j = 0; j < modelo.getColumnCount(); j++) {
if (vMayor < (double) modelo.getValueAt(i, j)) {
vMayor = (double) modelo.getValueAt(i, j);
fila = i;
columna = j;
}
}
}
for (i = 0; i < modelo.getColumnCount(); i++) {
total += (double) modelo.getValueAt(i, columna);
}
prom = total / dias.length;
DecimalFormat formato = new DecimalFormat("#,##0.00");
String mensaje = "Venta mayor = " + vMayor + "\nDía de la venta = " + dias[columna] + "\nVendedor = "
+ vendedores[fila] + "\nPromedio de ventas = " + formato.format(prom);
JOptionPane.showMessageDialog(null, mensaje, "Datos de la venta mayor",
JOptionPane.INFORMATION_MESSAGE);
}
}
static class Action3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
MiTablaModelo modelo = new MiTablaModelo();
double total = 0.0, prom = 0.0;
int i, j;
for (i = 0; i < modelo.getRowCount(); i++) {
for (j = 0; j < modelo.getColumnCount(); j++) {
total += (double) modelo.getValueAt(i, j);
}
}
prom = total / (modelo.getRowCount() * modelo.getColumnCount());
DecimalFormat df = new DecimalFormat("#,##0.00");
String mensaje = "Promedio general = " + df.format(prom);
JOptionPane.showMessageDialog(null, mensaje, "Promedio general de ventas",
JOptionPane.INFORMATION_MESSAGE);
}
}
static class Action4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
MiTablaModelo modelo = new MiTablaModelo();
double total = 0.0, prom = 0.0;
String[] dias = modelo.dias;
String[] vendedores = modelo.nombres;
double ventasV = (double) modelo.getValueAt(0, 0);
int fila = 0, columna = 0, i, j;
for (i = 0; i < modelo.getRowCount(); i++) {
for (j = 0; j < modelo.getColumnCount(); j++) {
//fila = i ;
//columna = j ;
//}
}
}
for (i = 0; i++ < modelo.getColumnCount(); i++) {
}
for (i = 0; i++ < modelo.getColumnCount(); i++) {
total += (double) modelo.getValueAt(i, columna);
prom = total;
DecimalFormat formato = new DecimalFormat("#,##0.00");
String mensaje = "\nVendedor = " + vendedores[fila] + " Ventas Por Semana = " + formato.format(prom);
JOptionPane.showMessageDialog(null, mensaje, "Datos de Venta por Vendedor",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
static class Action5 implements ActionListener {
public void actionPerformed(ActionEvent e) {
MiTablaModelo modelo = new MiTablaModelo();
double total = 0.0, prom = 0.0;
String[] dias = modelo.dias;
String[] vendedores = modelo.nombres;
double pMenor = (double) modelo.getValueAt(0, 0);
int fila = 0, columna = 0, i, j;
for (i = 0; i < modelo.getRowCount(); i++) {
for (j = 0; j < modelo.getColumnCount(); j++) {
if (pMenor > (double) modelo.getValueAt(i, j)) {
pMenor = (double) modelo.getValueAt(i, j);
fila = i;
columna = j;
}
}
}
for (i = 0; i < modelo.getColumnCount(); i++) {
total += (double) modelo.getValueAt(i, columna);
}
prom = total / dias.length;
DecimalFormat formato = new DecimalFormat("#,##0.00");
String mensaje = "\nPromedio de ventas = " + formato.format(prom) + "\nDía de la venta = " + dias[columna] + "\nVendedor = "
+ vendedores[fila];
JOptionPane.showMessageDialog(null, mensaje, "Datos de Promedio menor",
JOptionPane.INFORMATION_MESSAGE);
}
}
static class Action6 implements ActionListener {
public void actionPerformed(ActionEvent e) {
MiTablaModelo modelo = new MiTablaModelo();
double total = 0.0, prom = 0.0;
String[] dias = modelo.dias;
String[] vendedores = modelo.nombres;
double pMayor = (double) modelo.getValueAt(0, 0);
int fila = 0, columna = 0, i, j;
for (i = 0; i < modelo.getRowCount(); i++) {
for (j = 0; j < modelo.getColumnCount(); j++) {
if (pMayor > (double) modelo.getValueAt(i, j)) {
pMayor = (double) modelo.getValueAt(i, j);
fila = i;
columna = j;
}
}
}
for (i = 0; i < modelo.getColumnCount(); i++) {
total += (double) modelo.getValueAt(i, columna);
}
prom = total / dias.length;
DecimalFormat formato = new DecimalFormat("#,##0.00");
String mensaje = "\nPromedio de ventas = " + formato.format(prom) + "\nDía de la venta = " + dias[columna] + "\nVendedor = "
+ vendedores[fila];
JOptionPane.showMessageDialog(null, mensaje, "Datos de Promedio Mayor",
JOptionPane.INFORMATION_MESSAGE);
}
}
static class Action7 implements ActionListener {
public void actionPerformed(ActionEvent e) {
MiTablaModelo modelo = new MiTablaModelo();
double total = 0.0, prom = 0.0;
String[] dias = modelo.dias;
String[] vendedores = modelo.nombres;
double promedioD = (double) modelo.getValueAt(0, 0);
int fila = 0, columna = 0, i, j;
for (i = 0; i < modelo.getRowCount(); i++) {
for (j = 0; j < modelo.getColumnCount(); j++) {
}
}
for (i = 0; i++ < modelo.getColumnCount(); i++) {
}
for (i = 0; i++ < modelo.getColumnCount(); i++) {
total += (double) modelo.getValueAt(i, columna);
prom = total;
DecimalFormat formato = new DecimalFormat("#,##0.00");
String mensaje = "\nDia = " + dias[fila] + " Promedio Diario = " + formato.format(prom);
JOptionPane.showMessageDialog(null, mensaje, "Promedio por Dias",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
private static void crearYMostrarGUI() {
JFrame marco = new JFrame("Tabla semanal de ventas");
marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MatrizGrafica panel = new MatrizGrafica();
marco.setContentPane(panel);
JButton mostrarVMenor = new JButton("Venta menor");
marco.add(mostrarVMenor);
mostrarVMenor.addActionListener(new Action1());
JButton mostrarVMayor = new JButton("Venta mayor");
marco.add(mostrarVMayor);
mostrarVMayor.addActionListener(new Action2());
// Botón para mostrar el promedio general de ventas
JButton mostrarProm = new JButton("Promedio total");
marco.add(mostrarProm);
mostrarProm.addActionListener(new Action3());
JButton mostrarventasV = new JButton("Ventas X Vendedor");
marco.add(mostrarventasV);
mostrarventasV.addActionListener(new Action4());
JButton mostrarpMenor = new JButton("Promedio Menor");
marco.add(mostrarpMenor);
mostrarpMenor.addActionListener(new Action5());
JButton mostrarpMayor = new JButton("Promedio Mayor");
marco.add(mostrarpMayor);
mostrarpMayor.addActionListener(new Action6());
JButton mostrarpromedioD = new JButton("Promedio por Dias");
marco.add(mostrarpromedioD);
mostrarpromedioD.addActionListener(new Action7());
marco.pack();
marco.setLocationRelativeTo(null);
marco.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
crearYMostrarGUI();
}
});
}
}