问题描述
所以我编写了一个小程序,它可以让一个球永远在圆圈中滚动,我想让用户决定圆圈应该以什么速度滚动,但是当我添加 JFrame 时失败了:
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);
即使在完全受信任的小程序中,这也是不允许的.关闭框架将关闭运行启动它的小程序的 JVM.该 JVM 也可能正在运行其他小程序.
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.
这样看.承载小程序的网页就像一个客人,而网页则是一个宾馆.一个小程序结束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.
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
可能会工作"(不会产生 AccessControlException
),但实际上,没有小程序应该启动框架.使用 JDialog
代替.
Might 'work' (to not produce an AccessControlException
), but really, no applet should be launching frames. Use a JDialog
instead.
一般提示:确保 Java 控制台配置为显示小程序和JWS 应用程序.如果默认级别没有输出,请将其提高并重试.如果没有其中包含的信息,我怀疑是否有可能成功开发小程序.
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.
这篇关于从小程序打开 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!