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
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);
}
}
}
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.
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
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);
}
}
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.
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();
}
}