简体   繁体   English

从applet打开JFrame

[英]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,使球永远在圆中滚动,我想让用户决定圆应该以什么速度滚动,但是当我添加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: 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) 谢谢(对不起,如果它困扰您缺少信息)

Here are some things to consider: 这里有一些要考虑的事情:

  1. Don't use a JFrame. 不要使用JFrame。 Use a JDialog as a popup window. 使用JDialog作为弹出窗口。 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. 相反,您应该有一个JMenuItem,以便用户在希望显示弹出窗口时可以单击菜单。

  2. Don't use "Applet", that is an AWT component. 不要使用“ Applet”,这是一个AWT组件。 You should be using "JApplet" in a Swing application. 您应该在Swing应用程序中使用“ JApplet”。

  3. You should not be overriding the paint() method of the applet. 您不应该重写applet的paint()方法。 Instead you should be adding a JPanel to the applet and then override the paintComponent(...) with your custom painting. 相反,您应该向小程序中添加一个JPanel,然后使用自定义绘画覆盖paintComponent(...)。

  4. Don't use a loop to control the animation. 不要使用循环来控制动画。 Instead you should be using a Swing Timer. 相反,您应该使用Swing计时器。

Start by reading the Swing tutorial . 首先阅读Swing教程 There are sections on: 有以下部分:

  1. How to Make Applets 如何制作小程序
  2. How to Use Swing Timers 如何使用摇摆计时器
  3. Performing Custom Painting 进行定制绘画
setDefaultCloseOperation(EXIT_ON_CLOSE);

This is not allowed even in a fully trusted applet. 即使在完全信任的applet中也不允许这样做。 Closing the frame would close the JVM that runs the applet that launched it. 关闭框架将关闭运行启动它的applet的JVM。 That JVM might also be running other applets. 该JVM也可能正在运行其他小程序。

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. 对于小应用程序来说,结束JVM就像来宾在摧毁所有窗口的同时烧毁了旅馆。


setDefaultCloseOperation(DISPOSE_ON_CLOSE);

Might 'work' (to not produce an AccessControlException ), but really, no applet should be launching frames. 可以“工作”(不产生AccessControlException ),但实际上,任何小程序都不应启动框架。 Use a JDialog instead. 请改用JDialog

As a general tip: Ensure the Java Console is configured to show for applets & JWS apps. 作为一般提示:确保将Java控制台配置为针对applet和JWS应用程序显示。 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。

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) . Speed类扩展了JFrame,但是您设置的唯一东西是setDefaultCloseOperation(EXIT_ON_CLOSE) ,您应该使用setSize(width, height)至少设置JFrame大小,并使用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() 另一件事...我看不到将JFrame添加到Main类的位置...您应该添加它以创建一个新的Speed对象: 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/ 我想您可以在这里阅读以了解如何使用JFramehttp : //www.dreamincode.net/forums/topic/206344-basic-gui-in-java-using-jframes/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM