Sei sulla pagina 1di 9

* Create the frame.

*/
public Formulario() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 752, 564);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel("Configuracion de tabla (filas y columnas)");


lblNewLabel.setBounds(50, 52, 244, 14);
contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel("filas:");


lblNewLabel_1.setBounds(50, 94, 87, 14);
contentPane.add(lblNewLabel_1);

JLabel lblNewLabel_2 = new JLabel("columnas:");


lblNewLabel_2.setBounds(50, 127, 87, 14);
contentPane.add(lblNewLabel_2);

combo1 = new JComboBox();


combo1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
repaint();
}
});
combo1.setModel(new DefaultComboBoxModel(new String[] {"2", "3", "4", "5"}));
combo1.setBounds(161, 91, 96, 20);
contentPane.add(combo1);

combo2 = new JComboBox();


combo2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
repaint();
}
});
combo2.setModel(new DefaultComboBoxModel(new String[] {"2", "3", "4", "5"}));
combo2.setBounds(161, 124, 96, 20);
contentPane.add(combo2);

JLabel lblNewLabel_3 = new JLabel("Impresion");


lblNewLabel_3.setBounds(490, 52, 103, 14);
contentPane.add(lblNewLabel_3);
radio1 = new JRadioButton("Borrador");
buttonGroup.add(radio1);
radio1.setBounds(490, 90, 109, 23);
contentPane.add(radio1);

radio2 = new JRadioButton("Texto");


buttonGroup.add(radio2);
radio2.setBounds(490, 123, 109, 23);
contentPane.add(radio2);

JLabel lblNewLabel_4 = new JLabel("Color de fondo de la hoja");


lblNewLabel_4.setBounds(490, 219, 178, 14);
contentPane.add(lblNewLabel_4);

combo3 = new JComboBox();


combo3.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
repaint();
}
});
combo3.setModel(new DefaultComboBoxModel(new String[] {"rojo", "verde",
"azul"}));
combo3.setBounds(490, 254, 109, 20);
contentPane.add(combo3);

JButton btnNewButton = new JButton("Confirmar");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String filas=combo1.getSelectedItem().toString();
String columnas=combo2.getSelectedItem().toString();
label1.setText("Filas:"+filas+" Columnas:"+columnas);
if (radio1.isSelected())
label2.setText("Borrador");
if (radio2.isSelected())
label2.setText("Texto");
label3.setText(combo3.getSelectedItem().toString());

}
});
btnNewButton.setBounds(485, 330, 89, 23);
contentPane.add(btnNewButton);

JLabel lblNewLabel_5 = new JLabel("Filas y Columnas:");


lblNewLabel_5.setBounds(418, 393, 118, 14);
contentPane.add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("Impresion:");
lblNewLabel_6.setBounds(418, 428, 118, 14);
contentPane.add(lblNewLabel_6);

JLabel lblNewLabel_7 = new JLabel("Color de fondo:");


lblNewLabel_7.setBounds(418, 465, 118, 14);
contentPane.add(lblNewLabel_7);

label1 = new JLabel("resultado");


label1.setBounds(582, 393, 144, 14);
contentPane.add(label1);

label2 = new JLabel("resultado");


label2.setBounds(579, 428, 147, 14);
contentPane.add(label2);

label3 = new JLabel("resultado");


label3.setBounds(579, 465, 147, 14);
contentPane.add(label3);
}

@Override
public void paint(Graphics arg0) {
// TODO Auto-generated method stub
super.paint(arg0);
String color=combo3.getSelectedItem().toString();
if (color.equals("rojo"))
arg0.setColor(Color.red);
if (color.equals("verde"))
arg0.setColor(Color.green);
if (color.equals("azul"))
arg0.setColor(Color.blue);
arg0.fillRect(500, 320, 30, 30);

//dibujar la tabla
arg0.setColor(Color.blue);
int filas=Integer.parseInt(combo1.getSelectedItem().toString());
int columnas=Integer.parseInt(combo2.getSelectedItem().toString());
for(int f=1;f<=filas;f++)
{
for(int c=1;c<=columnas;c++)
{
arg0.drawRect(10+c*40, 160+f*40, 40, 40);
}
}
}

}
Correcto:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class Formulario3 extends JFrame {

private JPanel contentPane;


private JTextField tf1;
private JTextField tf2;
private JComboBox combo1;
private JButton boton1;
private JButton boton2;
private JLabel label1;
int contador;
private JLabel lblrticuloMasCaro;
private JLabel label2;
private JLabel lblPrecio_1;
private JLabel label3;
private JMenuItem mntmCantidadDeproductos;
private int c;
private int c2;
private int c3;

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

/**
* Create the frame.
*/
public Formulario3() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 543, 421);

JMenuBar menuBar = new JMenuBar();


setJMenuBar(menuBar);

JMenu mnNewMenu = new JMenu("Contabilidad");


menuBar.add(mnNewMenu);

mntmCantidadDeproductos = new JMenuItem("Cantidad deproductos");


mntmCantidadDeproductos.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

setTitle("Fruta: " + c + " Verdura: " + c2 + " Carne: " + c3);

}
});
mnNewMenu.add(mntmCantidadDeproductos);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblNombreDelrticulo = new JLabel("Nombre del \u00C1rticulo:");


lblNombreDelrticulo.setBounds(25, 40, 148, 14);
contentPane.add(lblNombreDelrticulo);

JLabel lblPrecio = new JLabel("Precio:");


lblPrecio.setBounds(25, 90, 68, 14);
contentPane.add(lblPrecio);

JLabel lblRubro = new JLabel("Rubro:");


lblRubro.setBounds(25, 141, 83, 14);
contentPane.add(lblRubro);

tf1 = new JTextField();


tf1.setBounds(183, 37, 86, 20);
contentPane.add(tf1);
tf1.setColumns(10);
tf2 = new JTextField();
tf2.setBounds(183, 87, 86, 20);
contentPane.add(tf2);
tf2.setColumns(10);

combo1 = new JComboBox();


combo1.setModel(new DefaultComboBoxModel(new String[] { "Fruta",
"Verdura", "Carne" }));
combo1.setBounds(183, 138, 86, 20);
contentPane.add(combo1);

boton1 = new JButton("Procesar");


boton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

int valor = Integer.parseInt(tf2.getText());


int acumular = Integer.parseInt(label1.getText());

valor = valor + acumular;


label1.setText(String.valueOf(valor));

contador++;
repaint();

String opcion = combo1.getSelectedItem().toString();

if (opcion.equals("Fruta")) {
c++;
}

if (opcion.equals("Verdura")) {
c2++;
}

if (opcion.equals("Carne")) {
c3++;
}
String cad=label2.getText();
if (cad.equals("?")){
label2.setText(tf1.getText());
label3.setText(tf2.getText());
}
else
{
int precioMayor=Integer.parseInt(label3.getText());
int precioActual=Integer.parseInt(tf2.getText());
if (precioActual>precioMayor)
{
label2.setText(tf1.getText());
label3.setText(tf2.getText());
}
}
}
});
boton1.setBounds(25, 192, 89, 23);
contentPane.add(boton1);

boton2 = new JButton("Reiniciar");


boton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

label1.setText("0");
contador = 0;
repaint();
setTitle("");

}
});
boton2.setBounds(180, 192, 89, 23);
contentPane.add(boton2);

JLabel lblPagoTotal = new JLabel("Pago Total:");


lblPagoTotal.setBounds(25, 262, 77, 14);
contentPane.add(lblPagoTotal);

label1 = new JLabel("0");


label1.setBounds(183, 262, 86, 14);
contentPane.add(label1);

JLabel lblCantidadDerticulos = new JLabel(


"Cantidad de \u00E1rticulos ingresados:");
lblCantidadDerticulos.setBounds(25, 321, 199, 14);
contentPane.add(lblCantidadDerticulos);

lblrticuloMasCaro = new JLabel("\u00C1rticulo mas caro:");


lblrticuloMasCaro.setBounds(315, 40, 104, 14);
contentPane.add(lblrticuloMasCaro);

label2 = new JLabel("?");


label2.setBounds(447, 40, 58, 14);
contentPane.add(label2);

lblPrecio_1 = new JLabel("Precio:");


lblPrecio_1.setBounds(315, 90, 61, 14);
contentPane.add(lblPrecio_1);

label3 = new JLabel("?");


label3.setBounds(447, 90, 70, 14);
contentPane.add(label3);
}
Hu

@Override
public void paint(Graphics arg0) {
super.paint(arg0);

arg0.setColor(Color.red);
int col = 240;
for (int f = 1; f <= contador; f++) {
arg0.fillOval(col, 360, 20, 20);
col = col + 30;
}

}
}
Hu

Potrebbero piacerti anche