当前位置: 技术问答>java相关
一个头疼的问题,请对java多态性有深入了解的高手给予关注
来源: 互联网 发布时间:2015-01-02
本文导语: public class Init_Graph extends JApplet { B b = new B(); JPanel pnlAll = new JPanel(); //constructor public Init_Graph() { init();} void init() { this.getContentPane.add(pnlAll); pnlAll.add(b, null); } class A extends JPanel { JButton btn...
public class Init_Graph extends JApplet
{
B b = new B();
JPanel pnlAll = new JPanel();
//constructor
public Init_Graph()
{ init();}
void init()
{ this.getContentPane.add(pnlAll);
pnlAll.add(b, null);
}
class A extends JPanel
{ JButton btn = new JButton();
//constructor,add the btn to A's container
}
class B extends A
{//constructor is 空
}
Applet运行时,在界面上见不到对象b的java组件btn(该btn是类B从类A继承的),如何解决,问题的原因是什么???
{
B b = new B();
JPanel pnlAll = new JPanel();
//constructor
public Init_Graph()
{ init();}
void init()
{ this.getContentPane.add(pnlAll);
pnlAll.add(b, null);
}
class A extends JPanel
{ JButton btn = new JButton();
//constructor,add the btn to A's container
}
class B extends A
{//constructor is 空
}
Applet运行时,在界面上见不到对象b的java组件btn(该btn是类B从类A继承的),如何解决,问题的原因是什么???
|
老兄,明明可以显示嘛!我刚用下面的代码测试了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class test2 extends JFrame
{
static B b = new B();
public static void main(String[] args)
{
test2 tt = new test2();
tt.getContentPane().add (b);
tt.setSize (200,300);
tt.setVisible (true);
}
}
class B extends A
{
public B()
{
}
}
class A extends JPanel
{
JButton btn = new JButton ("hello");
public A()
{
add(btn);
}
}
不用上面的代码试试看!在我这儿那个Button确实是显示了!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class test2 extends JFrame
{
static B b = new B();
public static void main(String[] args)
{
test2 tt = new test2();
tt.getContentPane().add (b);
tt.setSize (200,300);
tt.setVisible (true);
}
}
class B extends A
{
public B()
{
}
}
class A extends JPanel
{
JButton btn = new JButton ("hello");
public A()
{
add(btn);
}
}
不用上面的代码试试看!在我这儿那个Button确实是显示了!
|
呵呵!其实断肠人的第一个程序b不定义成static也行,只要在引用时加上对象名即可:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class test2 extends JFrame
{
B b = new B(); //〈--------
public static void main(String[] args)
{
test2 tt = new test2();
tt.getContentPane().add (tt.b); //〈---------
tt.setSize (200,300);
tt.setVisible (true);
}
}
class B extends A
{
public B()
{
}
}
class A extends JPanel
{
JButton btn = new JButton ("hello");
public A()
{
add(btn);
}
}
to woxin():如果你的问题解决了的话,请你给此贴加分吧。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class test2 extends JFrame
{
B b = new B(); //〈--------
public static void main(String[] args)
{
test2 tt = new test2();
tt.getContentPane().add (tt.b); //〈---------
tt.setSize (200,300);
tt.setVisible (true);
}
}
class B extends A
{
public B()
{
}
}
class A extends JPanel
{
JButton btn = new JButton ("hello");
public A()
{
add(btn);
}
}
to woxin():如果你的问题解决了的话,请你给此贴加分吧。