Results 1 to 4 of 4
Thread: JFrame custom layout design help
- 02-02-2012, 12:09 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
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); } } }
- 02-02-2012, 12:56 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 02-02-2012, 01:20 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
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); } }
- 02-02-2012, 02:41 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 15
- Rep Power
- 0
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
-
The Proper Class Design For Designing a Custom Language Type System
By abdulsami in forum Advanced JavaReplies: 2Last Post: 02-17-2011, 05:15 AM -
Custom JTabbedPane layout policy
By Leanne182x in forum AWT / SwingReplies: 0Last Post: 02-15-2011, 11:53 AM -
Custom JButton with flow layout
By phil128 in forum AWT / SwingReplies: 1Last Post: 01-17-2011, 10:44 PM -
Which layout to use for this design?
By kennyblue in forum AWT / SwingReplies: 4Last Post: 11-13-2010, 07:43 PM -
JFrame Layout
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 16Last Post: 06-16-2010, 01:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks