【问题标题】:Click Button to Open Another Applet单击按钮打开另一个小程序
【发布时间】:2015-03-07 03:13:40
【问题描述】:

我想通过点击一个按钮来调用另一个小程序;然后旧的小程序将被关闭或重新加载到新的小程序。

我的动作监听器还没有任何东西。

public class ConImage extends JApplet implements ActionListener {
    Button btn;
    Applet second;

    public void init()
    {
        setSize(1600,900);
        setLayout(null);
        btn=new Button("Replace with other applet");

        add(btn);
        btn.addActionListener(this);
    }


    public void paint(Graphics g)
    {
        super.paint(g);

        btn.setLocation(100, 100);
        btn.setSize(100, 50);            
    }

    public void actionPerformed(ActionEvent e)
    {   second=null;
        second= getAppletContext().getApplet("SecondClass");
        if (second!=null)
        {
            if(e.getSource()==Time)
            {
                SecondClass ma= (SecondClass) second;

            }
        }
    }
}

【问题讨论】:

  • 请不要以大写开头的变量名
  • 我认为这是不可能的,因为 Java 的安全功能。
  • 使用 CardLayout 在视图之间切换
  • 我已经删除了所有只会影响您的问题重点的附带细节。
  • 感谢大家的建议,我会尽力改进。我只是这方面的新手,我只是通过教程学习

标签: java applet


【解决方案1】:

我很确定这是不可能的,因为 Java 的安全系统。最好的方法是有一个大师班,它有一个JApplet 的数组。在那个主小程序上,我将创建一个从数组中设置可见小程序的方法,调用 init() 并在请求渲染时调用该小程序的 paint()

像这样:

public class MasterApplet extends JApplet {

private int index = 0;
private JApplet[] applets;

public void init(){
    JApplet appletA = new AppletA();
    JApplet appletB = new AppletB();
    applets = new JApplet[]{appletA, appletB};
    setViewing(index);
}

public void paint(Graphics g){
    applets[index].paint(g);
}

public void setViewing(int idex){
    index = idex;
    applets[idex].init();
    revalidate();
    repaint();
}

如果您想更改小程序,请将其添加到小程序数组中,然后使用该小程序的索引调用setViewing()

【讨论】:

  • 或者只使用 CardLayout...另外,您如何将当前视图附加到浏览器容器内的本机对等方...
猜你喜欢
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
相关资源
最近更新 更多