Can anyone help me with this?....
I got some sample programs to be studied but I don't really know how to remove its bug..I don't know if there really is..but I think there is...there was no error upon compiling but when I try to run it...there is...

here is the source code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class Login extends JFrame implements ActionListener
{
 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
  Login()
  {
    label1 = new JLabel();
    label1.setText("Username:");
    text1 = new JTextField(15);

    label2 = new JLabel();
    label2.setText("Password:");
      text2 = new JPasswordField(15);

    SUBMIT=new JButton("SUBMIT");

    panel=new JPanel(new GridLayout(3,1));
    panel.add(label1);
    panel.add(text1);
    panel.add(label2);
    panel.add(text2);
    panel.add(SUBMIT);
    add(panel,BorderLayout.CENTER);
    SUBMIT.addActionListener(this);
    setTitle("LOGIN FORM");
  }
   public void actionPerformed(ActionEvent ae)
  {
    String value1=text1.getText();
    String value2=text2.getText();
        if (value1.equals("roseindia") && value2.equals("roseindia")) {
    NextPage page=new NextPage();
    page.setVisible(true);
    JLabel label = new JLabel("Welcome:"+value1);
        page.getContentPane().add(label);
  }
    else{
      System.out.println("enter the valid username and password");
      JOptionPane.showMessageDialog(this,"Incorrect login or password",
            "Error",JOptionPane.ERROR_MESSAGE);
  }
}
}
 class LoginDemo
{
  public static void main(String arg[])
  {
    try
    {
    Login frame=new Login();
    frame.setSize(300,100);
    frame.setVisible(true);
    }
  catch(Exception e)
    {JOptionPane.showMessageDialog(null, e.getMessage());}
  }
}

this id for the next page:

import javax.swing.*;
import java.awt.*;

class NextPage extends JFrame
{
  NextPage()
   {
     setDefaultCloseOperation(javax.swing.
          WindowConstants.DISPOSE_ON_CLOSE);
     setTitle("Welcome");
       setSize(400, 200);
      }
 }

Recommended Answers

All 12 Replies

I should say that is punishment for you following a website that with poor/bad reputation.
I'm sorry I do not have time to go over your code, but I will share few lines of my current project

package bank;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import bank.gui.LoginFrame;

/**
 * Created by IntelliJ IDEA.
 * User: Peter
 */
public class BankGUI
{

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                LoginFrame lf = new LoginFrame();
                lf.runLoginFrame();
            }
        });
    }
}
package bank.gui.bsfPanels;

import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * Created by IntelliJ IDEA.
 * User: Peter
 */
public class ErrorPanel extends JPanel
    {
        JTextArea jta = new JTextArea();
        JScrollPane jsp = new JScrollPane(jta);

        public ErrorPanel()
        {
            super();
            setPreferredSize(new Dimension(490,100));
            add(jsp);
            setVisible(false);
        }

        public void refresh(ArrayList<String> err)
        {
            removeAll();
            jta = new JTextArea(5, 50);
            jta.setEditable(false);
            for (String str : err)
            {
                jta.append(str);
            }
            jsp = new JScrollPane(jta,
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            add(jsp);
            setVisible(true);
            validate();
            repaint();
        }

        public void hideMe()
        {
            setVisible(false);
        }
    }
package bank.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import bank.gui.logic.ResultPosition;
import bank.gui.logic.LoginLogic;

/**
 * Created by IntelliJ IDEA.
 * User: Peter
 */
public class LoginFrame extends JFrame
{
    private String errStr = "";

    private Dimension dimLF = new Dimension(250, 150);
    private Dimension dimLFErr = new Dimension(250, 175);

    private ResultPosition rp = new ResultPosition();
    private LoginLogic ll = new LoginLogic();

    public LoginFrame()
    {
        super();
    }

    public LoginFrame(String err)
    {
        super();
        errStr = err;
    }

    public void runLoginFrame()
    {
        setTitle("BMS - Login");
        LoginPanel loginJP = new LoginPanel();

        setSize(dimLF);
        getContentPane().add(loginJP);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(rp.resultPosition(dimLF));
        setResizable(false);
        setVisible(true);
    }

    public void errLoginFrame()
    {
        GridBagLayout gbag = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        LoginPanel loginJP = new LoginPanel();
        ErrorPanel errJP = new ErrorPanel();

        setTitle("BMS - Login");

        setSize(dimLFErr);
        setLayout(gbag);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbag.setConstraints(loginJP, gbc);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbag.setConstraints(errJP, gbc);
        getContentPane().add(errJP);
        getContentPane().add(loginJP);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(rp.resultPosition(dimLFErr));
        setResizable(false);
        setVisible(true);
    }

    class LoginPanel extends JPanel
    {
        final JTextField usrTF;
        final JPasswordField passPF;

        public LoginPanel()
        {
            GridBagConstraints gbc = new GridBagConstraints();

            setLayout(new GridBagLayout());

            JPanel usrPanel = new JPanel();
            JLabel usrLabel = new JLabel("Username:");
            usrTF = new JTextField(10);
            usrTF.setToolTipText("Please enter your username.");
            usrPanel.add(usrLabel);
            usrPanel.add(usrTF);

            JPanel passPanel = new JPanel();
            JLabel passLabel = new JLabel("Password:");
            passPF = new JPasswordField(10);
            passPF.setToolTipText("Please enter your password.");
            passPanel.add(passLabel);
            passPanel.add(passPF);

            JPanel btnPanel = new JPanel();
            JButton loginBtn = new JButton("Login");
            loginBtn.setPreferredSize(new Dimension(90, 30));
            loginBtn.setToolTipText("Press the button to log in to Bank Managment System.");
            loginBtn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    ll.validateEntryData(usrTF.getText(), passPF.getPassword());
                    dispose();
                }
            });
            JButton clearBtn = new JButton("Clear");
            clearBtn.setPreferredSize(new Dimension(90, 30));
            clearBtn.setToolTipText("Press the button to clear the username and password text fileds.");
            clearBtn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    usrTF.setText("");
                    passPF.setText("");
                }
            });
            btnPanel.add(loginBtn);
            btnPanel.add(clearBtn);

            gbc.gridx = 0;
            gbc.gridy = 0;
            add(usrPanel, gbc);
            gbc.gridx = 0;
            gbc.gridy = 1;
            add(passPanel, gbc);
            gbc.gridx = 0;
            gbc.gridy = 2;
            add(btnPanel, gbc);
        }
    }

    class ErrorPanel extends JPanel
    {
        public ErrorPanel()
        {
            setLayout(new FlowLayout(FlowLayout.CENTER));
            JLabel errLabel = new JLabel(errStr);
            errLabel.setForeground(Color.RED);
            add(errLabel);
        }
    }
}

That is for interface, the LoginLogic is something you should implement and is not need to just run GUI part

PS: In the future please use code tags to post your code [code=Java]YOUR CODE HERE [/code]

thanks for your code...but I really want to know first what's the problem on the original post...because I'm studying basic java and your code was not that simple to understand.....but I really appreciate your response....(^^.)

If you want to have all in one file remove this ==>

this id for the next page:
import javax.swing.*;
import java.awt.*;

Your main() function is inside LoginDemo-class
Save your project as LoginDemo.java file !
compile and run LoginDemo

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Login extends JFrame implements ActionListener {

    JButton SUBMIT;
    JPanel panel;
    JLabel label1, label2;
    final JTextField text1,  text2;

    Login() {
        label1 = new JLabel();
        label1.setText("Username:");
        text1 = new JTextField(15);
        label2 = new JLabel();
        label2.setText("Password:");
        text2 = new JPasswordField(15);
        SUBMIT = new JButton("SUBMIT");
        panel = new JPanel(new GridLayout(3, 1));
        panel.add(label1);
        panel.add(text1);
        panel.add(label2);
        panel.add(text2);
        panel.add(SUBMIT);
        add(panel, BorderLayout.CENTER);
        SUBMIT.addActionListener(this);
        setTitle("LOGIN FORM");
    }

    public void actionPerformed(ActionEvent ae) {
        String value1 = text1.getText();
        String value2 = text2.getText();
        if (value1.equals("roseindia") && value2.equals("roseindia")) {
            NextPage page = new NextPage();
            page.setVisible(true);
            JLabel label = new JLabel("Welcome:" + value1);
            page.getContentPane().add(label);
        } else {
            System.out.println("enter the valid username and password");
            JOptionPane.showMessageDialog(this, "Incorrect login or password",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}

class NextPage extends JFrame {

    NextPage() {
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Welcome");
        setSize(400, 200);
    }
}
// change to public
public class LoginDemo {

    public static void main(String arg[]) {
        try {
            Login frame = new Login();
            frame.setSize(300, 100);
            frame.setVisible(true);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }
}

change line:50 to setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

The code was still not doing fine...
there is no error upon compiling but as I run the program...
The cmd displays this: " Exception in thread “main” java.lang.Error: Do not use Login.add() use Login.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:454)
at javax.swing.JFrame.addImpl(JFrame.java:480)
at java.awt.Container.add(Container.java.490)
at Login.<init>(LoginDemo.java:26)
at LoginDemo.main(LoginDemo.java:60) ".....I really don't know why...and what to do with that...

Which code do you use?
Or better post what you currently using...

I try the one that was on my first post and the one that quuba had.....and it has the same error.

Now let us look at the error :-

" Exception in thread “main” java.lang.Error: Do not use Login.add() use Login.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:454)
at javax.swing.JFrame.addImpl(JFrame.java:480)
at java.awt.Container.add(Container.java.490)
at Login.<init>(LoginDemo.java:26)
at LoginDemo.main(LoginDemo.java:60)

And you say you do not know what to do ?????????

Sorry I do not want be rude here, but honestly if it wouldn't have been for the forum rules I would have started swearing here.

The java compiler is telling you in a straight forward manner that go line 26 in file LoginDemo.java and instead of add(..) use getContentPane().add(..) .

Also here is a tutorial on frames which I am sure you definitely need to improve your footing in swing.

ow I'm sorry stephen84s...and thanks for your suggestion...I'm not a good programmer and fool cannot even use a computer....and if I really know what to do with that line 26 do you think I will be still here?....I don't even know what are the codes in java...I'm a complete beginner here......don't even know what its content......

JFrame (together with JApplet, JWindow and JDialog) is a heavyweight component that does not inherit JComponent, however they got Component and Container from AWT. All top level containers have content pane in which components are stored. Therefore if you want to component to a frame this have to be done through frames content pane.
So you get reference to content pane by calling getContentPane() and add component with add() method. This is done for example in your original code here

NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);

Who ever wrote the original code did a big mess of simple task

PS: First code does not throw any errors it just keep open previous frame as it has poor frame handling and does not close application correctly as it is missing proper close.
Also would be nice to know what are your Java coding experience so the answer could be place on certain level
PS2: Follow the link provided by stephen, these tutorial are better then roseindia.

change line:50 to
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

Do not change this line:50 (for NextPage -JFrame. My mistake.
But use this line to properly close Login -JFrame, what was my first intencion.

public class LoginDemo {

    public static void main(String arg[]) {
        try {
            Login frame = new Login();
            frame.setSize(300, 100);
//inserted line
            frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            frame.setVisible(true);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }
}

Thank you peter_budo.......I'll keep on studying this......it's really hard having self study.......

Thank you quuba gotta try this one...and thanks for the link.....I shall read more about java.........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.