从applet的开放的JFrame [英] opening JFrame from applet

查看:229
本文介绍了从applet的开放的JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我编程上午小程序,使得在圈子永远的球滚动,我想使用户决定的圆圈应该推出什么速度,但是当我添加了JFrame中一些失败:

小程序(停止,破坏和更新不会出现,因为它们并不重要,并在开始没有什么):

 公共类主要扩展的Applet实现Runnable {私人形象我;
私有图形GFU;
INT ballX,BALLY = 249;
静态INT半径= 20;
双存储;
INT changeY,changeX = 1;
速S =新速度();@覆盖
公共无效的init(){
    的setSize(750750);
    S.setVisible(真);
}@覆盖
公共无效的run(){
    而(真){
        如果(BALLY> = 250 || BALLY< = - 250){
            changeY = 0 changeY;
            changeX = 0 changeX;
        }
        BALLY + = changeY;
        内存=(双)巴利/ 250;
        内存= Math.asin(内存);
        内存= Math.cos(内存);
        ballX =(int)的(存储器* 250);
        如果(changeX == - 1)
            ballX = 0 ballX;        重绘();
        尝试{
            视频下载(17);
        }
        赶上(InterruptedException的E){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }
}@覆盖
公共无效漆(图形G){
    g.setColor(Color.black);
    g.fillOval(ballX半径+ 250,BALLY半径+ 250,半径* 2,半径* 2);
}
公共无效setChangeY(INT changeY){
    this.changeY = changeY;
}公共无效完成(){
    S.setVisible(假);
    螺纹BallRun =新主题(本);
    BallRun.start();
}}

JFrame中:

 公共类扩展速度JFrame的实现的ActionListener {
私有静态最后的serialVersionUID长1L =;公共速度(){
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel的P =新JPanel();
    JLabel的L =新的JLabel(请输入velosity(每秒像素));
    最终的JTextField TF =新的JTextField(00);
    最终主M =新的Main();
    JButton的B =新的JButton(OK);    B.addActionListener(新的ActionListener(){        @覆盖
        公共无效的actionPerformed(ActionEvent的五){
            M.setChangeY(的Integer.parseInt(TF.getText()));
            M.Done();        }
    });    P.add(L,BorderLayout.NORTH);
    P.add(TF,BorderLayout.WEST);}@覆盖
公共无效的actionPerformed(ActionEvent的为arg0){
}
}

感谢(和抱歉,如果它的困扰你缺乏信息)


解决方案

  setDefaultCloseOperation(EXIT_ON_CLOSE);

这是不是即使在完全受信任的小程序允许的。关闭框架将关闭运行启动它applet的JVM。该JVM也可能运行的其他小程序。

看它这样。该网页承载一个applet就像一个客人,而网页是一个招待所。对于一个小程序结束JVM就像客人烧毁招待所,同时粉碎了所有的窗户。


  setDefaultCloseOperation(DISPOSE_ON_CLOSE);

强权'工作'(不产生的AccessControlException ),但实际上,任何小程序应启动帧。使用的JDialog 来代替。

作为一般的提示:请确保 Java控制台配置为显示小程序和放大器; JWS应用程序。如果在默认级别无输出,提高它,然后再试一次。如果不包含的信息,我怀疑这将有可能成功开发的小程序。

So I programmed am applet that makes a ball roll in circles for ever, and I wanted to make the user decide what speed the circle should roll in, but something failed when I added the JFrame:

applet(the stop,destroy and update do not appear because they aren't important, and in start there is nothing):

public class Main extends Applet implements Runnable{

private Image I;
private Graphics GfU;
int ballX, ballY=249;
static int radius=20;
double Memory;
int changeY ,changeX=1;
Speed S = new Speed();

@Override
public void init() {
    setSize(750,750);
    S.setVisible(true);
}

@Override
public void run() {
    while(true){
        if(ballY>=250 || ballY<=-250){
            changeY=0-changeY;
            changeX=0-changeX;
        }
        ballY+=changeY;
        Memory=(double)ballY/250; 
        Memory=Math.asin(Memory);
        Memory=Math.cos(Memory);
        ballX=(int)(Memory*250);
        if(changeX==-1)
            ballX=0-ballX;

        repaint();
        try {
            Thread.sleep(17);            
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }
}



@Override
public void paint(Graphics g) {
    g.setColor(Color.black);
    g.fillOval(ballX-radius+250, ballY-radius+250, radius*2, radius*2);
}


public void setChangeY(int changeY) {
    this.changeY = changeY;
}

public void Done(){
    S.setVisible(false);
    Thread BallRun = new Thread(this);
    BallRun.start();
}

}

JFrame:

public class Speed extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;

public Speed(){
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel P = new JPanel();
    JLabel L = new JLabel("please enter velosity(pixels per second)");
    final JTextField TF = new JTextField("00");
    final Main M = new Main();
    JButton B = new JButton("OK");

    B.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            M.setChangeY(Integer.parseInt(TF.getText()));
            M.Done();

        }
    });

    P.add(L,BorderLayout.NORTH);
    P.add(TF,BorderLayout.WEST);

}

@Override
public void actionPerformed(ActionEvent arg0) {


}
}

thanks (and sorry if it's bothering you the lack of information)

解决方案

setDefaultCloseOperation(EXIT_ON_CLOSE);

This is not allowed even in a fully trusted applet. Closing the frame would close the JVM that runs the applet that launched it. That JVM might also be running other applets.

Look at it like this. The web page that hosts an applet is like a guest, while the web page is a guest house. For an applet to end the JVM is like the guest burning down the guest house while smashing out all the windows.


setDefaultCloseOperation(DISPOSE_ON_CLOSE);

Might 'work' (to not produce an AccessControlException), but really, no applet should be launching frames. Use a JDialog instead.

As a general tip: Ensure the Java Console is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again. Without the information contained in it, I doubt it would be possible to successfully develop an applet.

这篇关于从applet的开放的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆