opening JFrame from applet

百般思念 提交于 2019-12-16 18:04:20

问题


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)


回答1:


Here are some things to consider:

  1. Don't use a JFrame. Use a JDialog as a popup window. Also, you should probably not create the dialog in the constructor. Instead you should have a JMenuItem so that the user can click on the menu when they want the popup to display.

  2. Don't use "Applet", that is an AWT component. You should be using "JApplet" in a Swing application.

  3. You should not be overriding the paint() method of the applet. Instead you should be adding a JPanel to the applet and then override the paintComponent(...) with your custom painting.

  4. Don't use a loop to control the animation. Instead you should be using a Swing Timer.

Start by reading the Swing tutorial. There are sections on:

  1. How to Make Applets
  2. How to Use Swing Timers
  3. Performing Custom Painting



回答2:


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.




回答3:


Your Speed class extends JFrame, but the only things that you set is setDefaultCloseOperation(EXIT_ON_CLOSE), you should set at least se size of your JFrame with setSize(width, height) and set it visible with: setVisible(true). Another thing... i can't see where you added your JFrame to the Main class... You should add it creating a new Speed object: Speed objectname = new Speed()

If i've understood correctly that was your problem. I think you could read here to learn how to use the JFrame: http://www.dreamincode.net/forums/topic/206344-basic-gui-in-java-using-jframes/



来源:https://stackoverflow.com/questions/24344267/opening-jframe-from-applet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!