Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java Frame

Options
  • 08-01-2007 2:07pm
    #1
    Closed Accounts Posts: 488 ✭✭


    ey,

    I have a java problem, I know its probably simple but bear with me.

    I have a Jframe that I want to divide up into different panels.

    example:
    public MainFrame(){

    super("The Text System " );
    setSize(900,700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }
    The panels contain labels, buttons etc.
    example of a panel:
    public DetailPanel(){
    super();
    setPreferredSize(new Dimension(1,0));
    setVisible(true);
    //this.setBorder(new TitledBorder("Details"));

    dtxtarea.setLineWrap(true);
    dtxtarea.setWrapStyleWord(true);

    add(rptDetail);
    add(dScroll);
    add(save);
    }
    I want to keep the panels separte and just call them in the Jframe to display them.
    ie
    in the main frame do this
    DetailPanel detailsP= new DetailPanel();
    to display the panel
    I am going wrong some where.
    Any help would be great.


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    You need something like the following:

    public class MyFrame extends JFrame{


    MyFrame (){

    JPanel myPanel=new JPanel();
    //ad stuff to panel

    getContentPane().add(myPanel, BorderLayout.CENTER);

    setVisable(true);
    }
    }
    You may want to look at different Layout managers to arrange the panels nicely in the Frame also.
    The default layout for a JFrame content pane is BorderLayout so you can add panels to
    BorderLayout.CENTER
    BorderLayout.NORTH
    BorderLayout.SOUTH
    BorderLayout.EAST
    BorderLayout.WEST


  • Registered Users Posts: 4,188 ✭✭✭pH


    I want to keep the panels separte and just call them in the Jframe to display them.

    Nothing stopping you creating a separate class (.java file) for each JPanel
    public class DetailPanel1 extends JPanel{
         public DetailPanel1() {
             init();
         }
    
         private void init() {
            // Setup your panel here
         }
    }
    

    The in the main class do exactly as your example

    DetailPanel1 panel1 = new DetailPanel1();


  • Closed Accounts Posts: 488 ✭✭watsgone


    Thank you kindly


Advertisement