• Post Reply Bookmark Topic Watch Topic
  • New Topic

JApplet Problem

 
vijay malhotra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code given below there are 2 classes mybank.java and your bank.java
in the package mybanks.
The label of JButton b1 is initially set to "In Mybank" in class mybank.java
I want to change this label to "In Yourbank" from set method defined in
class yourbank.java , But when I compile this program compiler generates
the following error
E:\javaprog\mybanks\yourbank.java:7: cannot resolve symbol
symbol : variable b1
location: class mybanks.mybank
m.b1.setText("In Your Bank");
^
1 error
Following is the code of 2 classes
/** mybank.java **/
package mybanks;
import java.awt.*;
import javax.swing.*;
/*
<applet code="mybank" width=250 height=150>
</applet>
*/
public class mybank extends JApplet
{

public void init()
{

Container contentpane = getContentPane();

JButton b1 = new JButton("In Mybank");
yourbank y = new yourbank();
y.set(this);
contentpane.add(b1);
}


}
/** yourbank.java **/
package mybanks;
class yourbank
{
void set(mybank m)
{
m.b1.setText("In Your Bank");
}
}
Q1. Why the compiler is giving the error even though I am passing
the reference with the help of "this" from init()method of mybank class in the statement y.set(this);
Q2. How can I rectify the problem so that I am able to change
the text of JButton b1 from the set method defined in class
yourbank ?
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class mybank extends JApplet
{
public void init()
{
Container contentpane = getContentPane();
JButton b1 = new JButton("In Mybank");
yourbank y = new yourbank();
y.set(this);
contentpane.add(b1);
}

b1 is only defined within the init() method. Move the definition like this and that should solve your problem.
public class mybank extends JApplet
{
JButton b1 = new JButton("In Mybank");
public void init()
{
Container contentpane = getContentPane();

yourbank y = new yourbank();
y.set(this);
contentpane.add(b1);
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!