Hey, I've been working on an applet quite some time now.
I'm making a little text based rpg with some images, in a JApplet.
I made panels for register, login, main, character creation, but the JPanel resizes when I press a button.
Basicly when I click register (gets inserted into a database) then a Jlabel returns, Account created. And then, when you press back to go back to the first panel, it crashes. (when you move your mouse over the frame, the buttons of the main panel will apear, so basicly you see 2 panels, but you can't do anything anymore
this is the code:
The actionlistener for the Register panel
public class actRegister implements ActionListener {
JPanel myPanel;
Container con;
public actRegister(JPanel panel, Container container)
{
this.myPanel = panel;
this.con = container;
}
public void actionPerformed(ActionEvent e) {
con.add(FameApplet.register);
con.remove(myPanel);
packContainer.pack(con);
Registerpan.txtName.requestFocus();
}
}
Register layout
public class Registerpan extends JPanel implements MouseListener, FocusListener {
static JTextField txtName;
static JTextField email;
static JPasswordField password;
static JPasswordField repassword;
static JTextArea ERror;
Border whiteline;
JLabel lblname;
JLabel lblpass;
JLabel lblemail;
JLabel lblrepass;
JButton btnSignup;
JButton btnClearForm;
JButton btnBackToMain;
public Registerpan(Color color, Container con)
{
whiteline = BorderFactory.createLineBorder(Color.WHITE);
setLayout(null);
setBackground(color);
setPreferredSize(new Dimension(498,498));
setBounds(0,0,500,500);
setForeground(Color.WHITE);
// ... Buttons and textfields
}
}
Here happens the crash/freeze
public class actBackMain implements ActionListener {
JPanel myPanel;
Container con;
public actBackMain(JPanel panel, Container container)
{
this.myPanel = panel;
this.con = container;
}
public void actionPerformed(ActionEvent arg0) {
con.remove(myPanel);
con.add(FameApplet.mainpan);
packContainer.pack(con);
}
}
This happens after pressing this button once (sign up button)
public class actBackMain implements ActionListener {
JPanel myPanel;
Container con;
public actBackMain(JPanel panel, Container container)
{
this.myPanel = panel;
this.con = container;
}
public void actionPerformed(ActionEvent arg0) {
con.remove(myPanel);
con.add(FameApplet.mainpan);
packContainer.pack(con);
}
}
public class actSignup implements ActionListener
{
JPanel myPanel;
Container con;
public actSignup(JPanel panel, Container container) { this.myPanel = panel; this.con = container; }
public void actionPerformed(ActionEvent arg0) {
String errorMess = "";
Connection conn = null;
try
// Register process
// ...................................................
try {
conn = RecordManagement.getConnection();
String query = "INSERT INTO app_users(name, password, email) VALUES (?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, rec_name);
pstmt.setString(2, rec_password);
pstmt.setString(3, rec_email);
pstmt.executeUpdate();
errorMess += "Account created!";
ERror.setText(errorMess);
con.remove(myPanel);
con.add(FameApplet.mainpan);
packContainer.pack(con);
}
catch (Exception ex){System.out.print(ex);}
}
}
}
This is the pack function
public class packContainer {
public static void pack(Container container)
{
Container cp = container;
Dimension d = cp.getLayout().preferredLayoutSize(cp);
cp.setSize((int)d.getWidth(),(int)d.getHeight());
}
}
Help would be appreciated. Thanks.
Edited by: nitroniouz on Sep 29, 2008 8:57 AM