Results 1 to 8 of 8
  1. #1
    myclassic is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default [SOLVED] My First Program

    Dear

    here goes my first program........... please help me in removing my errors.
    1. show() is not working.
    2. Back ground colours are not working.
    3. Maximize() is not working

    what could be the error and
    how to solve it?

    regards.



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

    public class MyForm extends JFrame{

    public MyForm(){
    Container c = getContentPane();

    c.setLayout(new FlowLayout());
    JLabel jl1 = new JLabel("My Form");
    JLabel jl2 = new JLabel("Employee Number");
    JTextField jt1 = new JTextField(20);
    JLabel jl3 = new JLabel("Employee Name");
    JTextField jt2 = new JTextField(50);
    JButton jb1 = new JButton("Ok");
    JButton jb2 = new JButton("Cancel");

    c.add(jl1);
    c.add(jl2);
    c.add(jt1);
    c.add(jl3);
    c.add(jt2);
    c.add(jb1);
    c.add(jb2);

    c.setBackground("RED");
    jl1.setBackground("BLUE");
    c.setSize(800,600);
    c.setLocation(400,300);
    c.setMaximize();
    c.show();

    }

    public static void main(String Arg[]){
    MyForm mf = new MyForm();
    }
    }

  2. #2
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    6

    Default

    Hi,
    You cant show a container.
    Use a JFrame(best option) and do the necessary operations like show(),setBackground() and maximize()

    If you take an example a glass filled with water. You are trying to do operations on water that had to be done on glass. ;) So do operations on glass(JFrame) that contains water.
    To finish sooner, take your own time....
    Nivedithaaaa

  3. #3
    myclassic is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Niveditha View Post
    Hi,
    You cant show a container.
    Use a JFrame(best option) and do the necessary operations like show(),setBackground() and maximize()

    If you take an example a glass filled with water. You are trying to do operations on water that had to be done on glass. ;) So do operations on glass(JFrame) that contains water.
    True what you said.

    As I am new to JAVA.
    plz read the code and suggest what should be added at which place, in code to show the output.

    one thing more.
    while using setVisible(true) instead of show() returns no warning. still does not show any output.

    waiting plz.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Well, read the JDK 6 Documentation

    show is deprecated (i.e. you should not use it anymore, not that you can not, but should not, if you want to be assurred of having what you want to happen actually happen). use setVisible

    setBackground needs a color (see the Color class description in the API docs linked to above), not just a String representation of the Color wanted.

    There is no setMaximize method (as the API docs would also tell you). There is a (and I am adding spaces to make this clear) a "set Maximum Size" method, but that is not maximize. There is also, however, a setExtendedState method that will allow you to programtically alter the "state" of the frame.

    Edit: And, you need to call pack before setVisible, or your frame is not actually constructed, at all.
    Last edited by masijade; 08-28-2008 at 08:30 AM.

  5. #5
    myclassic is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    thanks dear.
    You were really helpful.
    as i have changed my code

    JFrame d = new JFrame();
    d.setContentPane(c);
    d.show();
    it is working fine but height and width is min.

    thanks again

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Why? Why create a second Frame?

    Simply call setVisibile(true) on "this" rather than on the contentpane, and make sure to call pack on "this" before doing that (and before calling setExtendedState).

    i.e.

    this.pack()
    this.setExtendedState(.....)
    this.setVisible(true)


    "c" in your code is refering to the "contentPane" (i.e. the content of the frame not the frame itself) and you are, at the end of that method, performing the methods that should be performed on the farme, on the content.

  7. #7
    myclassic is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Running Fine.....................
    Thank U Dear..... really thank U...

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

    public class MyForm1 extends JFrame{

    public MyForm1(){
    Container c = getContentPane();

    //Layout Style Definition
    c.setLayout(new FlowLayout());
    JLabel jl1 = new JLabel("My Form");
    JLabel jl2 = new JLabel("Employee Number");
    JTextField jt1 = new JTextField(20);
    JLabel jl3 = new JLabel("Employee Name");
    JTextField jt2 = new JTextField(50);
    JButton jb1 = new JButton("Ok");
    JButton jb2 = new JButton("Cancel");

    c.add(jl1);
    c.add(jl2);
    c.add(jt1);
    c.add(jl3);
    c.add(jt2);
    c.add(jb1);
    c.add(jb2);


    //c.setBackground("RED");
    //jl1.setBackground("BLUE");
    c.setSize(800,600);
    c.setLocation(400,300);
    // c.setMaximize();


    this.setExtendedState(MAXIMIZED_BOTH);
    this.show();
    // d.setVisible(true);

    }

    public static void main(String Arg[]){
    MyForm1 mf = new MyForm1();
    }
    }

  8. #8
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    6

    Default

    :) good now mark this as solved please.
    To finish sooner, take your own time....
    Nivedithaaaa

Similar Threads

  1. Help with program
    By bri1547 in forum New To Java
    Replies: 16
    Last Post: 07-27-2008, 05:26 AM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. cannot run the program
    By amiey in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 04:13 AM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •