-1

I have a main frame named as "frame1". In "frame1" i want to add a panel to display something on the panel but I am not able to add the panel to my main frame i.e "frame1"

 public void drawstack()
 {
    JPanel m1 = new JPanel(new BorderLayout());
    m1.setBorder(BorderFactory.createRaisedSoftBevelBorder());
    m1.setBackground(Color.red);
    frame1.add(m1);
 }

This is my code m getting an error at the last line i.e "frame1.add(m1);" error is

cannot find symbol : frame1.
rick
  • 913
  • 6
  • 13
  • 28
  • 2
    Where is frame1 decalred? Show the rest of the code. – Vincent Ramdhanie Jun 16 '12 at 11:56
  • 1
    In all likelihood, the frame1 variable is out of scope when trying to use it in the method above -- perhaps it was declared in another method or constructor and thus is only visible in the other method or constructor. It should probably be a class field. – Hovercraft Full Of Eels Jun 16 '12 at 12:01
  • i also dont see frame1 declared? – shareef Jun 16 '12 at 12:01
  • 1
    Create the `frame1`, as @Vincent Ramdhanie suggests, or paste [the whole concise example](http://sscce.org) related to the problem. Also if the `m1` serves as the content pane of the frame set it as such using `setContentPane()` method this will [let you avoid issues as for example this one from yesterday](http://stackoverflow.com/a/11055804/613495). – Boro Jun 16 '12 at 12:02
  • "frame1" is the default frame. – rick Jun 16 '12 at 12:02
  • 1
    If the class itself is the "frame1" that you are talking about, you can just use `this.add(m1);` or even just `add(m1);` – Austin Jun 16 '12 at 12:03
  • 1
    rick, "default frame" has no meaning. Please tell us the details of your problem, else we can't help you. – Hovercraft Full Of Eels Jun 16 '12 at 12:04
  • okk...got the solution...thank u all – rick Jun 16 '12 at 12:12
  • rick please tell also what was the solution.. since you got it :) – Boro Jun 16 '12 at 12:13
  • 1
    @HovercraftFullOfEels *'"default frame" has no meaning'* Maybe not, but that combined with `frame1` (the number) suggests maybe the OP has multiple frames. On that matter, the OP should read [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) before proceeding. Likely to make this problem redundant (disappear). – Andrew Thompson Jun 16 '12 at 12:14
  • i ve put a panel onto "frame1" from the toolbox and named it as "m1" and commented the first and last line of the above code. – rick Jun 16 '12 at 12:16

4 Answers4

1

frame1 has to be a field in your class or has to be passed as a parameter to your method

1

The error is telling you that the compiler cannot find a variable (or class) named frame1. In order for the flagged statement to work you will need to declare a variable named frame1 and instantiate it with a JFrame object, something like this:

JFrame frame1 = new JFrame();
frame1.add(m1);

Note: You can make this variable a member of the class as well if you want to access it from other methods of the same class.

Note: The intantiated JFrame referenced by frame1 is initially invisible, you will need to make it visible by calling setVisible(true) on it

If your class derives from JFrame and you want to add the panel to the frame represented by the current object, you can use the this reference instead of frame1:

this.add(m1);

in this ase you can even leave the this out:

add(m1);
Attila
  • 28,265
  • 3
  • 46
  • 55
0

obviously theres an error..you haven't initialized a variable named frame1.. use this.add(m1); instead..it should work.

Rajitha Siriwardena
  • 2,749
  • 1
  • 18
  • 22
0

If method you posted is inside your class that extends JFrame you need to call

getContentPane().add(m1);

Also if you want to add more than 1 element to your frame use layout managers.

alaster
  • 3,821
  • 3
  • 24
  • 32