Results 1 to 11 of 11
- 05-09-2011, 01:46 AM #1
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
New to SWING / a tad confused on showing things...
Here is the code for two files, I am expecting a blue ball to show up on a panel, in a frame. I can get it to work if I place my ball on the frame directly, but not if I place it on the panel, on the frame. Please keep in mind I am new to Java GUI in general...
Assn4.java:
Ball.java:Java Code:import javax.swing.*; public class Assn4 { public static void main(String[] args) { System.out.printf("Starting Program 4\n"); JFrame frame = new JFrame("Balls"); frame.setSize(256, 256); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); frame.add(panel); Ball x = new Ball(); frame.add(x); // panel.add(x); <-- want this to work panel.setVisible(true); frame.setVisible(true); } }
Thanks in advance for the insight.Java Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Ball extends JPanel implements ActionListener{ // Eclipse wants this serial thing... private static final long serialVersionUID = -4316629291579494934L; static int xsize; static int ysize; static Color color; public Ball() { System.out.printf("Creating new ball\n"); xsize = 50; ysize = 50; color = Color.BLUE; } public void paintComponent( Graphics g ){ System.out.printf("Painting ball\n"); g.setColor(color); g.fillOval(15, 15, xsize, ysize); } public void actionPerformed(ActionEvent e){ System.out.printf("Ball action...\n"); this.repaint(); } }
Andrew
- 05-09-2011, 01:54 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You want to pre-load the panel before adding it to the frame. Try adding the ball to the panel, then add the panel to the frame.
-
Why do you want to place your Ball inside of a JPanel as it achieves no purpose? Why not simply add the Ball object to the JFrame (as you've done above)? If you need to nest JPanels, then the one holding the Ball object needs to have a layout that tells the Ball JPanel to completely fill it, for instance a BorderLayout. You will also want to set one of the JPanel's preferred size and call pack() on the JFrame before calling setVisible(true).
- 05-09-2011, 02:29 AM #4
Member
- Join Date
- Apr 2011
- Location
- Kansas
- Posts
- 26
- Rep Power
- 0
You can't get it to show because you're adding the ball to the frame, not to the panel.
See, you've added the ball to the frame.Java Code:Ball x = new Ball(); frame.add(x);
-
David: Please re-read the original post. If he adds the Ball object directly to the JFrame (actually the JFrame's contentPane), it should show, and in fact he tells us that it does show. This code is not in error and in fact is the correct way to do this. This is because the contentPane uses a BorderLayout as its default layout, and adding a component to a BorderLayout without using BorderLayout constants will add that component to the BorderLayout.CENTER position and will have that component fill the container and display it.
Original poster, please disregard David's well-meaning but mistaken post.
- 05-09-2011, 02:37 AM #6
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
I have tried this with the code below, same result (empty looking window):
Java Code:import javax.swing.*; public class Assn4 { public static void main(String[] args) { System.out.printf("Starting Program 4\n"); JFrame frame = new JFrame("Balls"); frame.setSize(256, 256); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); Ball x = new Ball(); // frame.add(x); panel.add(x); panel.setVisible(true); frame.add(panel); frame.setVisible(true); } }
- 05-09-2011, 02:38 AM #7
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
-
No, that is not 'correct'. When you add something to a JFrame, you are actually adding it to the JFrame's contentPane which is a JPanel. But also your Ball class is a JPanel. Your use of another JPanel is unnecessary and is messing you up. I strongly urge you to read the Oracle tutorial on the layout managers (google "adding components to a container") to see why this is messing you up and how to use layout managers to your advantage.
- 05-09-2011, 02:46 AM #9
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
- 05-09-2011, 02:47 AM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
When you create a custom component it is your responsibility to determine the preferred size of the component so the layout manager can use this information to layout the component. Add something like the following to your Ball class:
Yes it is unnecessary to add the Ball component to another panel first and if will work if you use:Java Code:public Dimension getPreferredSize() { return new Dimension(80, 80); }
However, it won't work if you use:Java Code:frame.add(ball).
The key issue with this problem is the custom component needs to have a preferred size and you should not rely on a layout manager to adjust the size of the component.Java Code:frame.add(ball, BorderLayout.NORTH);
Last edited by camickr; 05-09-2011 at 02:53 AM.
- 05-15-2011, 10:48 PM #11
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Need Help. with two simple things
By Fred1 in forum New To JavaReplies: 32Last Post: 04-28-2011, 02:35 AM -
Checking if 2 things are false
By ile4 in forum New To JavaReplies: 4Last Post: 11-16-2010, 10:40 AM -
Showing NetBeans-generated Swing forms
By _SAS in forum AWT / SwingReplies: 0Last Post: 06-19-2010, 02:45 AM -
XML, and other things.
By Tortex in forum New To JavaReplies: 5Last Post: 03-28-2010, 05:53 PM -
so whats going on? (things aren't showing up)
By Adrien in forum AWT / SwingReplies: 9Last Post: 02-20-2010, 07:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks