user2518777
user2518777

Reputation: 1

GUI Programming and Invoking a class from within another class

so I am trying to make a program that utilizes the JDesktopPane to hold many different internal frames. In the code below, I only have one internal frame (a Login frame). I was hoping to pass the inner frames to the outer frame and add them like that. But I am having problems doing this.... The filename is TheProgram.java Look:

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

class OuterFrame
{
JDesktopPane outframe = new JDesktopPane();
    OuterFrame()
    {

    }
    OuterFrame(JInternalFrame inframe)
    {
        outframe.add(inframe);
    }

}

class Login extends JFrame
{
    JPanel panel;
    JLabel lblname;
    JLabel lblpassword;
    JTextField txtname;
    JPasswordField txtpassword;
    JButton btlogin; 
    JInternalFrame login = new JInternalFrame();

    Login()
    {
        login.setSize(300,200);
        login.setLocation(10,2);
        login.setTitle("Member Login");
        lblname=new JLabel("User Name:");
        lblpassword=new JLabel("Password:");
        btlogin=new JButton("Login");            
        txtname=new JTextField(20);
        txtpassword=new JPasswordField(20);
        panel=new JPanel();
        panel.add(lblname);
        panel.add(txtname);
        panel.add(lblpassword);
        panel.add(txtpassword);
        panel.add(btlogin);
        //panel.add(lblmess);
        login.add(panel);
        login.setVisible(true);
    }
    public void method()
    {
        OuterFrame.OuterFrame(login);
    }

}

public class TheProgram
{
    public static void main(String[] args)
    { 
        new OuterFrame();
    } 
}

Upvotes: 0

Views: 123

Answers (1)

Java Devil
Java Devil

Reputation: 10959

Your main method is this

public static void main(String[] args)
    { 
        new OuterFrame();
    }

which constructs an outerframe, which has the constructor

OuterFrame()
    {

    }

... Does nothing

Edit: This is probably not the best way for you to be doing this, as suggested in a comment a login would normally be a Dialog. I've written some code that will work for you but might not be the best gong forward in terms of management.

Firstly the constructor of your OuterFrame needs to do something with the internal frame you passed it. Like this

OuterFrame(JInternalFrame inframe)
{
    JFrame aFrame = new JFrame();
    outframe.add(inframe);
    outframe.setVisible(true);
    aFrame.setLayout(new BorderLayout());
    aFrame.setSize(400,400);
    aFrame.add(outframe);
    aFrame.setVisible(true);
}

Then in your main method you need to create a Login frame and an instance of your outerframe using this constructor:

public static void main(String[] args)
{
    Login log = new Login();
    OuterFrame frame = new OuterFrame(log.login);
}

Upvotes: 1

Related Questions