Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

private Panel member of a Frame

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Consider this program :
/* Creating a purchase order screen */
import java.awt.*;
import javax.swing.*;
class POFrame extends JFrame
{


// constructor
public POFrame()
{
super("Purchase Order");

Panel topLeftPanel; // 1
Panel topRightPanel; // 2
Panel bottomLeftPanel; // 3
Panel bottomRightPanel; // 4

setBounds(50,50,600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(2,2));

/* // add Buttons

Button CancelButton = new Button("Cancel");
// Button SubmitButton = new Button("Submit");

CancelButton.setBackground(Color.blue);
//SubmitButton.setBackground(Color.red);

getContentPane().add(CancelButton);
//getContentPane().add(SubmitButton);
setVisible(true);*/

topLeftPanel=new Panel();
topRightPanel=new Panel();
bottomLeftPanel=new Panel();
bottomRightPanel=new Panel();

getContentPane().add(topLeftPanel);
getContentPane().add(topRightPanel);
getContentPane().add(bottomLeftPanel);
getContentPane().add(bottomRightPanel);
setVisible(true);


}
}

If I prefix the lines marked 1,2,3 and 4 with private, then the program doesn't compile. Why?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is those Panels are within the constructor and it doesn't really make sense to put an access modifier in front of what is essentially a local variable. Try doing this instead:-
import java.awt.*;
import javax.swing.*;
class POFrame extends JFrame
{
private Panel topLeftPanel; // 1
private Panel topRightPanel; // 2
private Panel bottomLeftPanel; // 3
private Panel bottomRightPanel; // 4
// constructor
public POFrame()
{
super("Purchase Order");
setBounds(50,50,600,500);
. . . and carry on with the rest of the program as was.
By moving the Panel declarations out of the constructor - you make panel into a class member variable which is what I think you wanted to do.
Hope this solves the problem,
Kathy
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I didn't notice what was perhaps the very obvious thing.
Thanks for pointing out.
Shree
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic