Jump to content

Need help with extended JFrame

- - - - -

  • Please log in to reply
11 replies to this topic

#1
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
Hello i am new to java and i am trying to put my code into not only one file but many different files and i just created one file with the main project and one file with the login screen that i whant for my program and when i run my program the Jframe loginscreen i make is not visible in the screen.

I will put both codes here..

This is the login screen
package files;


public class LoginFrame extends javax.swing.JFrame {

    

    javax.swing.JButton login;

    javax.swing.JLabel username;

    javax.swing.JLabel password;

    javax.swing.JTextField uname;

    javax.swing.JTextField pword;



    public void loginscreen (){

        


        login = new javax.swing.JButton("Login");

        username = new javax.swing.JLabel("Username");

        password = new javax.swing.JLabel("Password");

        uname = new javax.swing.JTextField(15);

        pword = new javax.swing.JTextField(15);


        setSize(400, 300);

        add(login);

        add(username);

        add(password);

        add(uname);

        add(pword);

        setVisible(true);

        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);




    }

}


And this is the file i whant to show the Jframe in
import javax.swing.*;

import java.awt.*;



public class Meditor {

     

    public static void main(String[] args) {

        files.LoginFrame loginscreen = new files.LoginFrame();

        

    }

}

Please help me :) thanks!

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You didn't make a constructor for LoginFrame.
So, you can either put the loginscreen() 's code in the constructor
It should be named
public LoginFrame(){...}
or change your main to this:


import javax.swing.*;

import java.awt.*;



public class Meditor {

     

    public static void main(String[] args) {

        files.LoginFrame loginscreen = new files.LoginFrame();

        loginscreen.loginscreen();

    }

}

But that looks a bit weird.

#3
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
I'm little new to java please explain whata constructor are :S


I know i am stupid :P

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
It's a method which gets executed when the object gets created. So when you do "new ClassName", it will run its constructor.
A constructor is always like "public ClassName(){...}";

It can also take parameters:

public class Person{

  private String name;


  public Person(String n){

    name = n;

  }

}

And running this would be:

public static void main(String[] args){

  Person me = new Person("Wim DC");

}


If the constructor is not "public" you can't do it. The fact that you could run your code is because the Java compiler will automatically add a no-arg constructor if it doesn't exist.
(no-arg means it doesn't take parameters), so your constructor would've looked like:
public LoginFrame(){

}


#5
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
Hmm you mean i put the "Public LoginFrame(){} in my main file ? or hmm sorry i maybe not understanding :S

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
No, in LoginFrame:

package files;


public class LoginFrame extends javax.swing.JFrame {

    

    javax.swing.JButton login;

    javax.swing.JLabel username;

    javax.swing.JLabel password;

    javax.swing.JTextField uname;

    javax.swing.JTextField pword;


[B]    public LoginFrame(){

        init();

    }[/B]


    public void [B]init[/B](){       


        login = new javax.swing.JButton("Login");

        username = new javax.swing.JLabel("Username");

        password = new javax.swing.JLabel("Password");

        uname = new javax.swing.JTextField(15);

        pword = new javax.swing.JTextField(15);


        setSize(400, 300);

        add(login);

        add(username);

        add(password);

        add(uname);

        add(pword);

        setVisible(true);

        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

    }

}

And now, every time you create a new LoginFrame, it will execute init();

#7
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
Thank you soo much!

One more question.. How do i add Layouts and bacground colors ?

Edit: Also when i run the program my buttons and labels and such is not visible..

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Background:
add

setBackground(Color.RED);

in the init() method.

Layouts can be somewhat hard to understand, i suggest you read these tutorials.
There are more layouts than those 3, but i think those 3 are the most used:
How to Use BorderLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
How to Use FlowLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
How to Use GridLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

#9
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
I need a GridBagLayout and i know how it work.. I just whant to know how to add it :) this what i am making now i did before just that i whant it in multiple files now :)

#10
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
When i compile with "setBackground(Color.white);" it gives me an error that says the "Color" variable not exists

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
propably missing this import:
import java.awt.Color
For gridbag, just add

setLayout(new GridBagLayout());

in your init() method, before adding components, then add the components like it goes for gridbaglayouts..
I never really use gridbaglayouts, so can't help you further there (How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container))

#12
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
Thank you soo much! You realy helped me much and teach me new things thank you!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users