Will
Posts: 20
Nickname: lofty
Registered: Mar, 2005
Applet please help!
Posted: Mar 25, 2005 3:35 AM
Advertisement
Hi, I'm desperate for some help. The applet below runs fine in Applet Viewer, but the only browser it runs in is Safari. All the others say "Applet not initialized". I've checked that the browsers can run swing (and they can), so I can't see the problem at all. Safari is pretty forgiving, so rather than blaming the browsers, I think I need to look at my code. Any help would be really appreciated - I've been stuck on this for a week. import java.awt.*; import java.awt.event.*; import javax.swing.*; /* * Created on Mar 15, 2005 * */ /** * @author will * */ public class SpendYourMoney extends JApplet implements ActionListener { boolean pMatch = false; boolean nMatch = false; // instantiate arrays String usernames[] = {"Will", "Mary", "Tom", "Dick", "Harry"}; String passwords[] = {"author", "tester", "thumb", "dastardly", "hill"}; //instantiate components Container container = new Container(); JPanel topPanel = new JPanel(); JLabel lUpper = new JLabel(); JLabel lLower = new JLabel(); JLabel lStatus = new JLabel(""); JPanel centerPanel = new JPanel(); JTextField tName = new JTextField(12); JTextField tChangePassName = new JTextField(); JPasswordField tPassword = new JPasswordField(12); JPanel centerPanel1 = new JPanel(); JPasswordField tChangePass = new JPasswordField(12); JPasswordField tNewPass = new JPasswordField(12); JPasswordField tCheckNewPass = new JPasswordField(12); JPanel btnPanel = new JPanel(); JPanel changePassPanel = new JPanel(); JButton submitBtn = new JButton("Submit"); JButton resetBtn = new JButton("Reset"); JButton changePassBtn = new JButton("Change Password"); JButton okBtn = new JButton("OK"); JButton logOutBtn = new JButton("Log Out"); JPanel bottomPanel = new JPanel(); JPanel successPanel = new JPanel(); JPanel logOutBtnPanel = new JPanel(); JPanel loggedPanel = new JPanel(); public void init() { try{ container = getContentPane(); setSize(520, 455); // start adding components //start with Top Panel topPanel.setLayout(new BorderLayout()); topPanel.setPreferredSize(new Dimension(80, 130)); lUpper.setFont(new java.awt.Font("Dialog", 1, 18)); lUpper.setForeground(Color.blue); lUpper.setText("Welcome to"); lUpper.setHorizontalAlignment(SwingConstants.CENTER); lLower.setFont(new Font("Dialog", 1, 28)); lLower.setForeground(Color.blue); lLower.setText("SpendYourMoneyHere.com"); lLower.setHorizontalAlignment(SwingConstants.CENTER); topPanel.add(lUpper, BorderLayout.NORTH); topPanel.add(lLower, BorderLayout.CENTER); //center Panel centerPanel.setLayout(new GridLayout(5,3,0,5)); centerPanel.setPreferredSize(new Dimension(100,130)); centerPanel.add(new JLabel("Name: ", SwingConstants.RIGHT)); tName.requestFocus(); centerPanel.add(tName); centerPanel.add(new JLabel("")); centerPanel.add(new JLabel("Password: ", SwingConstants.RIGHT)); centerPanel.add(tPassword); centerPanel.add(new JLabel("")); centerPanel.add(new JLabel("")); btnPanel.add(submitBtn); btnPanel.add(resetBtn); centerPanel.add(btnPanel); centerPanel.add(new JLabel("")); centerPanel.add(new JLabel("")); changePassPanel.add(changePassBtn); centerPanel.add(changePassPanel); //replacement centerPanel centerPanel1.setLayout(new GridLayout(5,3,0,5)); centerPanel1.setPreferredSize(new Dimension(100,130)); centerPanel1.add(new JLabel("Name: ", SwingConstants.RIGHT)); centerPanel1.add(tChangePassName); tChangePassName.requestFocus(); centerPanel1.add(new JLabel ("")); centerPanel1.add(new JLabel("Password: ", SwingConstants.RIGHT)); centerPanel1.add(tChangePass); centerPanel1.add(new JLabel("")); centerPanel1.add(new JLabel("New Password: ", SwingConstants.RIGHT)); centerPanel1.add(tNewPass); centerPanel1.add(new JLabel("")); centerPanel1.add(new JLabel("Re-enter New Password:", SwingConstants.RIGHT)); centerPanel1.add(tCheckNewPass); centerPanel1.add(new JLabel("")); centerPanel1.add(new JLabel("")); JPanel okBtnPanel = new JPanel(); okBtnPanel.add(okBtn); centerPanel1.add(okBtnPanel); centerPanel1.add(new JLabel("")); //bottom panel bottomPanel.setPreferredSize(new Dimension(300, 150)); lStatus.setFont(new java.awt.Font("Dialog", 2, 12)); lStatus.setForeground(Color.RED); lStatus.setHorizontalAlignment(SwingConstants.CENTER); bottomPanel.add(lStatus, BorderLayout.CENTER); //create successful login panel successPanel.setLayout(new BorderLayout()); successPanel.setBackground(Color.BLACK); JLabel lSuccess = new JLabel(); lSuccess.setFont(new java.awt.Font("Dialog", 2, 24)); lSuccess.setForeground(Color.WHITE); lSuccess.setText("Welcome to Will's World!"); lSuccess.setHorizontalAlignment(SwingConstants.CENTER); JLabel lSpend = new JLabel(); lSpend.setFont(new java.awt.Font("Dialog", 2, 18)); lSpend.setForeground(Color.WHITE); lSpend.setText("Now get your Wallet out."); lSpend.setHorizontalAlignment(SwingConstants.CENTER); logOutBtn.setHorizontalAlignment(SwingConstants.RIGHT); successPanel.add(lSuccess, BorderLayout.NORTH); successPanel.add(lSpend, BorderLayout.CENTER); loggedPanel.setLayout(new BorderLayout()); loggedPanel.setBackground(Color.BLACK); logOutBtnPanel.add(logOutBtn); logOutBtn.setBackground(Color.BLACK); logOutBtnPanel.setBackground(Color.BLACK); loggedPanel.add(logOutBtnPanel, BorderLayout.EAST); successPanel.add(loggedPanel, BorderLayout.SOUTH); //add Action Listener to buttons okBtn.addActionListener(this); submitBtn.addActionListener(this); resetBtn.addActionListener(this); changePassBtn.addActionListener(this); logOutBtn.addActionListener(this); tName.requestFocus(); container.add(topPanel, BorderLayout.NORTH); container.add(centerPanel, BorderLayout.CENTER); container.add(bottomPanel, BorderLayout.SOUTH); validate(); } catch (Exception e) { e.printStackTrace(); } } // Action Performed public void actionPerformed(ActionEvent e) { if(e.getSource()==okBtn){ for(int i=0; i<usernames.length; i++) if(usernames.equals(tChangePassName.getText().trim())) { int cUName = i; nMatch = true; for(int a=0; a<passwords.length; a++) if(passwords[a].equals(tChangePass.getText().trim())) { int cUPass = a; pMatch = true; if(cUName == cUPass) { String changedPass=tNewPass.getText().trim(); String checkPass = tCheckNewPass.getText().trim(); if((changedPass).equals(checkPass)) { passwords[a]=(tNewPass.getText().trim()); tChangePassName.setText(""); tChangePass.setText(""); tCheckNewPass.setText(""); tNewPass.setText(""); remove(centerPanel1); container.add(centerPanel); tName.requestFocus(); lStatus.setText("Password changed successfully - please login with your new password"); repaint(); validate(); } else if(changedPass!=checkPass) { showStatus("Password alteration Error"); lStatus.setText("New Passwords do not match"); tNewPass.setText(""); tCheckNewPass.setText(""); tChangePassName.requestFocus(); validate(); } } } } if (nMatch=false || pMatch!=true) { showStatus("Password alteration Error"); lStatus.setText("Incorrect username or password"); tChangePassName.setText(""); tChangePass.setText(""); tNewPass.setText(""); tCheckNewPass.setText(""); tChangePassName.requestFocus(); repaint(); validate(); } } if(e.getSource()==changePassBtn) { remove(centerPanel); container.add(centerPanel1, BorderLayout.CENTER); tChangePassName.requestFocus(); lStatus.setText(""); validate(); } if(e.getSource()==resetBtn) { tName.setText(""); tPassword.setText(""); lStatus.setText(""); tName.requestFocus(); validate(); } if(e.getSource()==submitBtn) { for(int i=0; i<usernames.length; i++) if(usernames.equals(tName.getText().trim())) { int uName = i; nMatch = true; for(int a=0; a<passwords.length; a++) if(passwords[a].equals(tPassword.getText().trim())) { int pWord = a; pMatch = true; if(uName==pWord) { String loggedID=tName.getText().trim(); showStatus("Login Successful. You are logged in as " + loggedID); remove(topPanel); remove(centerPanel); remove(bottomPanel); container.add(successPanel); validate(); } if(uName!=pWord) { lStatus.setText("Incorrect Username or Password, please try again."); showStatus("Password Error"); tName.setText(""); tPassword.setText(""); tName.requestFocus(); repaint(); validate(); } } } if(nMatch=false || pMatch!=true) { lStatus.setText("Incorrect Username or Password, please try again."); showStatus("Password Error"); tName.setText(""); tPassword.setText(""); tName.requestFocus(); repaint(); validate(); } } else if(e.getSource()==logOutBtn) { remove(successPanel); container.add(topPanel); container.add(centerPanel); container.add(bottomPanel); tName.setText(""); tPassword.setText(""); tName.requestFocus(); showStatus("Please Login"); validate(); } } } One thing that just occurred to me is that I'm using the getText() method which is deprecated for JPassword. I don't think that would cause the applet not to initialize, but at this point I'm willing to look at anything.