Results 1 to 17 of 17
Thread: JFrame Layout
- 06-11-2010, 06:38 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
JFrame Layout
Hi
I have a problem with my cuurent project. Using GUI, I have an interface that has 3 sections:
1. Upper tab that has to be card layout.
2. Board GUI that has to be Grid Layout.
3. Left tab that also has to be Box Layout.
All these 3 sections must be in 1 interface.
I actually have decided to group them using JFrame and I wrote the following code which has a mistake that I couldn't find it.
I hope you can help meJava Code:package Try; import java.awt.*; import java.awt.event.*; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import javax.swing.*; import javax.swing.border.LineBorder; import javax.swing.text.AbstractDocument.Content; public class ThirdInterface extends JFrame{ JFrame pane = new JFrame(); JPanel UpperPanel = new JPanel(); JPanel BorderPanel = new JPanel(); JPanel LeftPanel = new JPanel(); //Upper tab JLabel space = new JLabel(" "); // add space JLabel space1 = new JLabel(" "); // add space JLabel label1 = new JLabel("Username: "); JLabel label11 = new JLabel("name"); JLabel label2 = new JLabel("Oponent name: "); JLabel label22 = new JLabel("name"); JLabel label = new JLabel(); JLabel Timelabel = new JLabel(); Locale alocal = new Locale("CANADA"); Date today = new Date(); DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,alocal); String dateOut = dateFormatter.format(today); Calendar cal = new GregorianCalendar(); int hour12 = cal.get(Calendar.HOUR); // 0..11 int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23 int min = cal.get(Calendar.MINUTE); // 0..59 int sec = cal.get(Calendar.SECOND); // 0..59 int ms = cal.get(Calendar.MILLISECOND); // 0..999 int ampm = cal.get(Calendar.AM_PM); // 0=AM, 1=PM String time = ""+ hour12 + ":"+ min + ":"+ sec ; // Left tab JLabel myInfo = new JLabel("My Information:"); JTextArea list = new JTextArea(10,25); JButton scores = new JButton(" My Scores "); JButton oponents = new JButton(" My Oponents "); JButton logout = new JButton(" Logout "); // Board private int currentPlayer; JLabel[][] slots = new JLabel[7][6]; public void init(){ pane.getContentPane(); pane.setLayout(new BorderLayout()); pane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane.pack(); pane.setVisible(true); // Upper tab UpperPanel.setLayout(new CardLayout()); UpperPanel.add(label1); UpperPanel.add(label11); UpperPanel.add(space); UpperPanel.add(label2); UpperPanel.add(label22); UpperPanel.add(space1); UpperPanel.add(label); UpperPanel.add(Timelabel); // Border BorderPanel.setLayout(new GridLayout(7,7)); for (int row=5; row>=0; row--) { for (int column=0; column<7; column++) { slots[column][row] = new JLabel(); slots[column][row].setFont(new Font("SansSerif", Font.BOLD, 18)); slots[column][row].setHorizontalAlignment(SwingConstants.CENTER); slots[column][row].setBorder(new LineBorder(Color.BLACK)); BorderPanel.add(slots[column][row]); } } currentPlayer = 1; // Left tab LeftPanel.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS)); LeftPanel.add(Box.createVerticalStrut(10)); myInfo.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(myInfo); list.setAlignmentX(Component.CENTER_ALIGNMENT); list.setEditable(false); LeftPanel.add(list); LeftPanel.add(Box.createVerticalStrut(10)); scores.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(scores); oponents.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(oponents); logout.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(logout); pane.add(UpperPanel) ; pane.add(BorderPanel); pane.add(LeftPanel); } public static void main(String[] args){ ThirdInterface ti = new ThirdInterface(); ti.init(); } }
Thanks
- 06-11-2010, 07:22 AM #2
You forgot to tell us how you deduced that the code has a mistake.
-- didn't compile? --> post the compile time error
-- didn't run? --> post the runtime error
-- didn't give the expected result? --> post the desired and actual results
-- computer went up in a puff of green smoke? --> don protective gear and grab a fire extinguisher
db
- 06-11-2010, 07:44 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Sorry, I forgot that.
Line 69:Java Code:at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at Try.ThirdInterface.init(ThirdInterface.java:69) at Try.ThirdInterface.main(ThirdInterface.java:118)
Line 118:Java Code:UpperPanel.add(label1);
These are the mistakesJava Code:ti.init();
- 06-11-2010, 11:06 AM #4
- 06-11-2010, 03:40 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Why are you packing the content pane and setting it visible before anything is added to it/at all? Usually you would pack the actual JFrame and setting that visible after you add everything to it.
I'm not saying this is you're problem, because as Darryl pointed out you left out the important line of the exception, but this may cause a problem down the line.
You are also extending JFrame and have a JFrame variable as well. Why extend it if you aren't going to treat it as one? I suggest removing the JFrame variable and adding the components directly to your class object.
- 06-14-2010, 06:57 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
First, I am going to post my code after I added the component before setting the content pane.
Second, when I run this code with Eclipse program, it gives me:Java Code:package Try; import java.awt.*; import java.awt.event.*; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import javax.swing.*; import javax.swing.border.LineBorder; import javax.swing.text.AbstractDocument.Content; public class ThirdInterface extends JFrame{ JFrame pane = new JFrame(); JPanel UpperPanel = new JPanel(); JPanel BorderPanel = new JPanel(); JPanel LeftPanel = new JPanel(); //Upper tab JLabel space = new JLabel(" "); // add space JLabel space1 = new JLabel(" "); // add space JLabel label1 = new JLabel("Username: "); JLabel label11 = new JLabel("name"); JLabel label2 = new JLabel("Oponent name: "); JLabel label22 = new JLabel("name"); JLabel label = new JLabel(); JLabel Timelabel = new JLabel(); Locale alocal = new Locale("CANADA"); Date today = new Date(); DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,alocal); String dateOut = dateFormatter.format(today); Calendar cal = new GregorianCalendar(); int hour12 = cal.get(Calendar.HOUR); // 0..11 int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23 int min = cal.get(Calendar.MINUTE); // 0..59 int sec = cal.get(Calendar.SECOND); // 0..59 int ms = cal.get(Calendar.MILLISECOND); // 0..999 int ampm = cal.get(Calendar.AM_PM); // 0=AM, 1=PM String time = ""+ hour12 + ":"+ min + ":"+ sec ; // Left tab JLabel myInfo = new JLabel("My Information:"); JTextArea list = new JTextArea(10,25); JButton scores = new JButton(" My Scores "); JButton oponents = new JButton(" My Oponents "); JButton logout = new JButton(" Logout "); // Board private int currentPlayer; JLabel[][] slots = new JLabel[7][6]; public void init(){ // Upper tab UpperPanel.setLayout(new CardLayout()); UpperPanel.add(label1); UpperPanel.add(label11); UpperPanel.add(space); UpperPanel.add(label2); UpperPanel.add(label22); UpperPanel.add(space1); UpperPanel.add(label); UpperPanel.add(Timelabel); // Border BorderPanel.setLayout(new GridLayout(7,7)); for (int row=5; row>=0; row--) { for (int column=0; column<7; column++) { slots[column][row] = new JLabel(); slots[column][row].setFont(new Font("SansSerif", Font.BOLD, 18)); slots[column][row].setHorizontalAlignment(SwingConstants.CENTER); slots[column][row].setBorder(new LineBorder(Color.BLACK)); BorderPanel.add(slots[column][row]); } } currentPlayer = 1; // Left tab LeftPanel.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS)); LeftPanel.add(Box.createVerticalStrut(10)); myInfo.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(myInfo); list.setAlignmentX(Component.CENTER_ALIGNMENT); list.setEditable(false); LeftPanel.add(list); LeftPanel.add(Box.createVerticalStrut(10)); scores.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(scores); oponents.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(oponents); logout.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(logout); pane.add(UpperPanel, "south") ; pane.add(BorderPanel); pane.add(LeftPanel); pane.getContentPane(); pane.setLayout(new BorderLayout()); pane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane.pack(); pane.setVisible(true); } public static void main(String[] args){ ThirdInterface ti = new ThirdInterface(); ti.init(); } }
Third, The idea from this code is to design this interface.Java Code:Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string at java.awt.CardLayout.addLayoutComponent(Unknown Source) at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at Try.ThirdInterface.init(ThirdInterface.java:62) at Try.ThirdInterface.main(ThirdInterface.java:116)

I want to devide this interface to 3 layouts:
1) The upper section using CardLayout.
2) The left section using BoxLayout.
3) The right section which is the board using GridLayout.
I want to arrange all these layouts in one panel.
I hope you can help me.
- 06-14-2010, 07:26 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
It's complicated and I'm really confused
Waiting u
- 06-14-2010, 07:39 PM #8
What is the source record at line 62 in your program?at Try.ThirdInterface.init(ThirdInterface.java:62)
It appears you are trying to call the add() method with the wrong args:
Find the statement, then get the type of the Container on that line and read the API doc for its add() method. Check to see if you used a String where it is required.IllegalArgumentException: cannot add to layout: constraint must be a string
- 06-15-2010, 07:14 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
First, I discovered the problem with setting Layouts in init() class.
I found that I have to setLayout after I add components.
e.g. in this block, I move the line of setLayout after add components
Now, the first part of the problem has solved.Java Code:UpperPanel.add(label1); UpperPanel.add(label11); UpperPanel.add(space); UpperPanel.add(label2); UpperPanel.add(label22); UpperPanel.add(space1); UpperPanel.add(label); UpperPanel.add(Timelabel); UpperPanel.setLayout(new CardLayout());
After I run the program, it gives:
This line refers toJava Code:at Try.ThirdInterface.init(ThirdInterface.java:106)
And also gives meJava Code:pane.pack();
which refers toJava Code:at Try.ThirdInterface.main(ThirdInterface.java:117)
I'm trying to solve the problem.Java Code:ti.init();
- 06-15-2010, 07:43 PM #10
Your post left off the error message itself. You've only shown the line number, but have not said what was wrong with the code.
- 06-15-2010, 07:52 PM #11
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
I did some changes in the code and finally no error messages but the problem now with the components. They don't show up in the frame.
Java Code:package Try; import java.awt.*; import java.awt.event.*; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import javax.swing.*; import javax.swing.border.LineBorder; import javax.swing.text.AbstractDocument.Content; public class ThirdInterface extends JFrame{ //JFrame pane = new JFrame(); private JPanel UpperPanel = new JPanel(); private JPanel BorderPanel = new JPanel(); private JPanel LeftPanel = new JPanel(); //Upper tab JLabel space = new JLabel(" "); // add space JLabel space1 = new JLabel(" "); // add space JLabel label1 = new JLabel("Username: "); JLabel label11 = new JLabel("name"); JLabel label2 = new JLabel("Oponent name: "); JLabel label22 = new JLabel("name"); JLabel label = new JLabel(); JLabel Timelabel = new JLabel(); Locale alocal = new Locale("CANADA"); Date today = new Date(); DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,alocal); String dateOut = dateFormatter.format(today); Calendar cal = new GregorianCalendar(); int hour12 = cal.get(Calendar.HOUR); // 0..11 int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23 int min = cal.get(Calendar.MINUTE); // 0..59 int sec = cal.get(Calendar.SECOND); // 0..59 int ms = cal.get(Calendar.MILLISECOND); // 0..999 int ampm = cal.get(Calendar.AM_PM); // 0=AM, 1=PM String time = ""+ hour12 + ":"+ min + ":"+ sec ; // Left tab JLabel myInfo = new JLabel("My Information:"); JTextArea list = new JTextArea(10,25); JButton scores = new JButton(" My Scores "); JButton oponents = new JButton(" My Oponents "); JButton logout = new JButton(" Logout "); // Board private int currentPlayer; JLabel[][] slots = new JLabel[7][6]; public ThirdInterface(){ // Upper tab UpperPanel.add(label1); UpperPanel.add(label11); UpperPanel.add(space); UpperPanel.add(label2); UpperPanel.add(label22); UpperPanel.add(space1); UpperPanel.add(label); UpperPanel.add(Timelabel); UpperPanel.setLayout(new CardLayout()); // Border BorderPanel.setLayout(new GridLayout(7,7)); for (int row=5; row>=0; row--) { for (int column=0; column<7; column++) { slots[column][row] = new JLabel(); slots[column][row].setFont(new Font("SansSerif", Font.BOLD, 18)); slots[column][row].setHorizontalAlignment(SwingConstants.CENTER); slots[column][row].setBorder(new LineBorder(Color.BLACK)); BorderPanel.add(slots[column][row]); } } currentPlayer = 1; // Left tab LeftPanel.add(Box.createVerticalStrut(10)); myInfo.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(myInfo); list.setAlignmentX(Component.CENTER_ALIGNMENT); list.setEditable(false); LeftPanel.add(list); LeftPanel.add(Box.createVerticalStrut(10)); scores.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(scores); oponents.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(oponents); logout.setAlignmentX(Component.CENTER_ALIGNMENT); LeftPanel.add(logout); LeftPanel.setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS)); getContentPane().add(UpperPanel, BorderLayout.NORTH); getContentPane().add(BorderPanel , BorderLayout.WEST); getContentPane().add(LeftPanel , BorderLayout.EAST); //ti.add(UpperPanel, BorderLayout.CENTER) ; //ti.add(BorderPanel); //ti.add(LeftPanel); //ti.getContentPane(); //ti.setLayout(new BorderLayout()); //ti.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args){ //ThirdInterface tt = new ThirdInterface(); JFrame ti = new JFrame(); ti.setSize(600, 800); ti.setVisible(true); } }
- 06-15-2010, 07:56 PM #12
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
In your main method, you are just creating a empty JFrame.
Create an instance of your ThirdInterface class and set it visible.
- 06-16-2010, 12:36 AM #13
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
I did what you say but it gives me an error
The error isJava Code:public static void main(String[] args){ ThirdInterface tt = new ThirdInterface(); JFrame ti = new JFrame(); ti.setSize(600, 800); ti.setVisible(true); tt.setVisible(true); }
Java Code:tt.setVisible(true);
- 06-16-2010, 01:36 AM #14
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
You just might want to tell folks what the error message is. Just a thought.
- 06-16-2010, 01:41 AM #15
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Java Code:Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared at javax.swing.BoxLayout.checkContainer(Unknown Source) at javax.swing.BoxLayout.invalidateLayout(Unknown Source) at java.awt.Container.invalidate(Unknown Source) at java.awt.Component.addNotify(Unknown Source) at java.awt.Container.addNotify(Unknown Source) at javax.swing.JComponent.addNotify(Unknown Source) at java.awt.Container.addNotify(Unknown Source) at javax.swing.JComponent.addNotify(Unknown Source) at java.awt.Container.addNotify(Unknown Source) at javax.swing.JComponent.addNotify(Unknown Source) at java.awt.Container.addNotify(Unknown Source) at javax.swing.JComponent.addNotify(Unknown Source) at javax.swing.JRootPane.addNotify(Unknown Source) at java.awt.Container.addNotify(Unknown Source) at java.awt.Window.addNotify(Unknown Source) at java.awt.Frame.addNotify(Unknown Source) at java.awt.Window.show(Unknown Source) at java.awt.Component.show(Unknown Source) at java.awt.Component.setVisible(Unknown Source) at java.awt.Window.setVisible(Unknown Source) at Try.ThirdInterface.main(ThirdInterface.java:123)
- 06-16-2010, 01:46 AM #16
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
The error message is telling you what's wrong: you're using BoxLayout wrong on line 123 of the ThirdInterface class. You're setting BoxLayout on the LeftPanel, but for some unknown reason are passing the contentPane to the BoxLayout constructor. Answer: don't do this, but instead pass the panel that you're currently setting to use BoxLayout. Please read the BoxLayout tutorial and API for more details if you're not sure how to use it.
More importantly, don't assume that error messages are cryptic and mystical but rather understand that usually they contain important information that if used will likely help you fix your code yourself.
- 06-16-2010, 01:48 AM #17
Similar Threads
-
Passing data from one JFrame to another JFrame. - need help.
By Unsub in forum New To JavaReplies: 6Last Post: 04-12-2010, 11:33 AM -
Edit layout Layout please help me
By manhtungtnk28@gmail.com in forum New To JavaReplies: 4Last Post: 11-23-2009, 08:41 AM -
Passing data from one JFrame to another JFrame
By tarami in forum New To JavaReplies: 3Last Post: 08-06-2009, 05:44 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
[SOLVED] JFrame Layout
By AndrewM16921 in forum New To JavaReplies: 4Last Post: 04-09-2009, 01:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks