Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

java - opening JFrame from applet

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...