Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: mrsanty en 14 de Diciembre 2016, 03:30
-
Hola a todos\as estoy haciendo un pequeño programa en java con entorno grafico para un amigo que tiene un local de reparación de pc´s. este programa cuenta con registro de usuario, etc.
Este programa se conecta a una base de datos sqlite, la conexión anda perfecto pero tengo un problema. cuando creo un botón y ago. que con ese botón me abra un jframe secundario y sierre el jframe principal pero no me funciona.
Este es el código del botón:
JButton btnMenu_1 = new JButton("menu");
btnMenu_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
alta frame = new alta();
frame.show();
dispose();
-
Buenos días.
Haría falta que publicaras más parte del código para ver donde podría estar el problema.
En el siguiente enlace te indica como publicar en el foro.
https://www.aprenderaprogramar.com/foros/index.php?topic=1460.0
Un saludo
-
hola, acá esta el código completo de una de las ventanas.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Registro
extends JFrame
{
private JPanel contentPane;
private JTextField txtusu;
private JTextField txtcorreo;
private JPasswordField txtpass;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Registro frame = new Registro();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
private void cargaDriver()
{
try
{
Class.forName("org.sqlite.JDBC");
}
catch (Exception ex)
{
setTitle(ex.toString());
}
}
public Registro()
{
setDefaultCloseOperation(3);
setBounds(100, 100, 512, 372);
this.contentPane = new JPanel();
this.contentPane.setBackground(Color.GRAY);
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(this.contentPane);
this.contentPane.setLayout(null);
JLabel lblNombre = new JLabel("Nombre:");
lblNombre.setForeground(Color.ORANGE);
lblNombre.setFont(new Font("Tw Cen MT", 3, 18));
lblNombre.setBounds(20, 73, 74, 37);
this.contentPane.add(lblNombre);
JLabel lblContrasea = new JLabel("Contrase�a:");
lblContrasea.setForeground(Color.ORANGE);
lblContrasea.setFont(new Font("Tw Cen MT", 3, 18));
lblContrasea.setBounds(20, 148, 102, 37);
this.contentPane.add(lblContrasea);
JLabel lblCorreo = new JLabel("Correo:");
lblCorreo.setForeground(Color.ORANGE);
lblCorreo.setFont(new Font("Tw Cen MT", 3, 18));
lblCorreo.setBounds(20, 196, 102, 50);
this.contentPane.add(lblCorreo);
this.txtusu = new JTextField();
this.txtusu.setBounds(141, 80, 270, 26);
this.contentPane.add(this.txtusu);
this.txtusu.setColumns(10);
this.txtcorreo = new JTextField();
this.txtcorreo.setBounds(141, 210, 270, 26);
this.contentPane.add(this.txtcorreo);
this.txtcorreo.setColumns(10);
final JLabel lblResultado = new JLabel("");
lblResultado.setForeground(Color.BLUE);
lblResultado.setBounds(141, 19, 313, 50);
this.contentPane.add(lblResultado);
JButton btnAceptar = new JButton("Aceptar");
btnAceptar.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
lblResultado.setText("");
try
{
Connection conexion = DB.conectarA("C:/local/DB.db");
Statement comando = conexion.createStatement();
comando.executeUpdate("insert into usua(usu,pass) values ('" + Registro.this.txtusu.getText() + "','" + Registro.this.txtpass.getText() + "')");
lblResultado.setText("Los datos han sido registrados");
Registro.this.txtusu.setText("");
Registro.this.txtpass.setText("");
JOptionPane.showMessageDialog(null, "la cuenta fue creada exitosamente");
}
catch (SQLException ex)
{
Registro.this.setTitle(ex.toString());
}
}
});
btnAceptar.setForeground(Color.RED);
btnAceptar.setFont(new Font("Tw Cen MT", 3, 17));
btnAceptar.setBounds(322, 278, 111, 23);
this.contentPane.add(btnAceptar);
JButton btnNuevoUsuario = new JButton("Atras");
btnNuevoUsuario.setForeground(Color.RED);
btnNuevoUsuario.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0){
lio frame = new lio ();
frame.show();
dispose();
}
});
btnNuevoUsuario.setFont(new Font("Tw Cen MT", 3, 17));
btnNuevoUsuario.setBounds(141, 278, 102, 23);
this.contentPane.add(btnNuevoUsuario);
this.txtpass = new JPasswordField();
this.txtpass.setBounds(141, 158, 270, 20);
this.contentPane.add(this.txtpass);
}
}
-
Buenas este ejemplo quizás te puede servir:
Clase de una ventana
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class StartupWindow extends JFrame implements ActionListener
{
private JButton btn;
public StartupWindow()
{
super("Simple GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn = new JButton("Open the other JFrame!");
btn.addActionListener(this);
btn.setActionCommand("Open");
add(btn);
pack();
}
@Override
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals("Open"))
{
dispose();
new AnotherJFrame();
}
}
Clase de la otra ventana
import javax.swing.JFrame;
import javax.swing.JLabel;
public class AnotherJFrame extends JFrame
{
public AnotherJFrame()
{
super("Another GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel("Empty JFrame"));
pack();
setVisible(true);
}
}
En este hilo quizás el ejemplo de código te pueda servir de ayuda: https://www.aprenderaprogramar.com/foros/index.php?topic=4249.0
O este: https://www.aprenderaprogramar.com/foros/index.php?topic=4193.0
Salu2