Results 1 to 4 of 4
  1. #1
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default JFrame custom layout design help

    I'm trying to design a custom user interface and it's alot harder then I thought it would be - I'm creating a gui for a music sequencer with buttons and text areas in diferent locations throught out the JFrame
    (16 buttons Border south - text area border north - misc area border center)
    heres my code so far but i'm running into problems already
    can anybody give me some pointers or dirict me to a good tutorial that deals with custom layouts because the tutorials i've seen so far are just basic
    also why is my for loop showing an error
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.util.jar.Attributes.Name;
    
    /**
     * Created by IntelliJ IDEA.
     * User: Jason
     * Date: 2/1/12
     * Time: 3:48 PM
     * To change this template use File | Settings | File Templates.
     */
    
    
    public class myGUI extends JFrame {
        
        String [] btns = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
        JPanel btnRow =new JPanel(new GridLayout(1,16,30,3));
        for(int i=0; i< btns.length; i++){
            JButton B = new JButton(btns [i]);
            btnRow.add(B);
            B.addActionListener((ActionListener) this);
        }
        
            
        }
    }

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: JFrame custom layout design help

    You are getting an error because your for loop is not part of any method or constructor. (By the way, it's a good idea to post compiler messages so that everyone can see what you can see.)

    Perhaps you mean the for loop to be inside a constructor. But often it is more flexible if you make the components part of a panel and then put the panel inside a frame. As the program grows having small classes that do a well defined thing is more manageable than stuffing everything into a single or few classes. And a panel is a container for components while a frame is something else.

    Use standard Java naming conventions: classes start with a capital letter, MyGui, and variables with a lower case letter, b or, more descriptively toAdd.

    Oracle's Tutorial is very good - and gives a comprehensive description of layout options.

  3. #3
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Re: JFrame custom layout design help

    thanx I figured it out a little bit ago and this is what I have now
    I'm not yet sure on how to put things inside a panel and then inside a frame and also having multi panels
    I'll check out oracle
    and if you have any more advice please let me know

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.util.jar.Attributes.Name;
    
    import static com.sun.deploy.util.DeployUIManager.setLookAndFeel;
    
    /**
     * Created by IntelliJ IDEA.
     * User: Jason
     * Date: 2/1/12
     * Time: 3:48 PM
     * To change this template use File | Settings | File Templates.
     */
    
    
    public class myGUI extends JFrame {
    
    
        public myGUI(){
            super("Shadow GUI");
            setLookAndFeel();
            setSize(500,500);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        String [] btns = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
        JPanel btnRow =new JPanel(new GridLayout(1,16,30,3));
        for(int i=0; i< btns.length; i++){
            JButton B = new JButton(btns [i]);
            btnRow.add(B);
            B.addActionListener((ActionListener) this);
        }
        //JFrame.add(btnRow, BorderLayout.SOUTH);
        
            
        }
    }

  4. #4
    jaytee is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default Re: JFrame custom layout design help

    OK I have one more small problem where it says JFrame.add I get in error in my code
    what should be there instead.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.security.PublicKey;
    import java.util.jar.Attributes.Name;
    
    import static com.sun.deploy.util.DeployUIManager.setLookAndFeel;
    
    /**
     * Created by IntelliJ IDEA.
     * User: Jason
     * Date: 2/1/12
     * Time: 3:48 PM
     * To change this template use File | Settings | File Templates.
     */
    
    
    public class myGUI extends JFrame {
    
    
        public myGUI(){
            super("Shadow GUI");
            setLookAndFeel();
            setSize(500,500);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        String [] btns = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"};
        JPanel btnRow =new JPanel(new GridLayout(1,16,30,3));
        for(int i=0; i< btns.length; i++){
            JButton B = new JButton(btns [i]);
            btnRow.add(B);
            B.addActionListener((ActionListener) this);
        }
    //this is where my errors are
    //where it says JFrame
    //error says error: non-static method add(Component,Object) cannot be referenced from a static context
    //how do i fix this?????
        JFrame.add(btnRow, BorderLayout.SOUTH);
            JTextArea text = new JTextArea();
            JPanel textPanel = new JPanel();
            textPanel.setBorder(BorderFactory.createEmptyBorder(12,6,6,12));// top, left, bottom, right
            textPanel.add(text);
            JFrame.add(text, BorderLayout.CENTER);
            JFrame.setVisible(true);
        
        }
        
            public static void main(String [] args){
            myGUI mG = new myGUI();
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 02-17-2011, 06:15 AM
  2. Custom JTabbedPane layout policy
    By Leanne182x in forum AWT / Swing
    Replies: 0
    Last Post: 02-15-2011, 12:53 PM
  3. Custom JButton with flow layout
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 01-17-2011, 11:44 PM
  4. Which layout to use for this design?
    By kennyblue in forum AWT / Swing
    Replies: 4
    Last Post: 11-13-2010, 08:43 PM
  5. JFrame Layout
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 16
    Last Post: 06-16-2010, 02:48 AM

Posting Permissions

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