Java 小程序赢得';不要在JPanel内部运行

Java 小程序赢得';不要在JPanel内部运行,java,swing,concurrency,applet,jpanel,Java,Swing,Concurrency,Applet,Jpanel,我试图在JFrame中的JPanel中运行一个可运行类。程序可以编译,但不能按预期工作。屏幕上应该有一个旋转的立方体,上面有一些菜单。我做错了什么?如果我把T3D类转换成一个小程序,那么这个程序运行得很好,它可以重新绘制,它可以做任何事情。当我尝试放入JFrame(作为Jpanel或applet)时,它不会重新绘制,也不会执行init()中的内容。谢谢你的帮助 绘图演示类 import javax.swing.*; import java.awt.geom.*; import java.awt.

我试图在JFrame中的JPanel中运行一个可运行类。程序可以编译,但不能按预期工作。屏幕上应该有一个旋转的立方体,上面有一些菜单。我做错了什么?如果我把T3D类转换成一个小程序,那么这个程序运行得很好,它可以重新绘制,它可以做任何事情。当我尝试放入JFrame(作为Jpanel或applet)时,它不会重新绘制,也不会执行init()中的内容。谢谢你的帮助

绘图演示类

import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.Graphics;
import java.applet.Applet;

public class DrawingDemo extends JFrame{
public DrawingDemo()
{
T3D demo = new T3D();
getContentPane().add(demo);
setVisible(true);
setSize(1024,720);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

T3D p = new T3D();
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu settings = new JMenu("Settings");
JMenu help = new JMenu("Help");
JMenuItem exit = new JMenuItem(">Exit");
JMenuItem imp = new JMenuItem(">Import");
JMenuItem exp = new JMenuItem(">Export");
JMenuItem sav = new JMenuItem(">Save");
JMenuItem ope = new JMenuItem(">Open");
file.add(ope);  
file.add(sav);
file.add(imp);
file.add(exp);
file.add(exit);
mb.add(file);
mb.add(edit);
mb.add(settings);
mb.add(help);
setJMenuBar(mb);
}

public static void main(String[] args)
{
    new DrawingDemo();
}

}
import java.awt.*;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;

 public class T3D extends JPanel implements Runnable {
 long nextSecond = System.currentTimeMillis() + 1000;
 int framesInLastSecond = 0;
 int framesInCurrentSecond = 0;
 int[][] LP= new int[19][3];
 double pos_camx,pos_camy,pos_camz,rot_camx,rot_camy,xpoint,ypoint,zpoint;
 double rot_radx,rot_rady,nclip,xscr,yscr,kx,ky,e;
 int pxscr,pyscr,nxscr,nyscr;

 public void start1() {
  Thread th = new Thread(this);
  th.start();

 }

 public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()){
    case KeyEvent.VK_A : 
    {
    pos_camx -= 0.01;
    }
    case KeyEvent.VK_D :
    {
    pos_camx += 0.01;
    }
    case KeyEvent.VK_W :
    {
    pos_camy += 0.01;
    }
    case KeyEvent.VK_S :
    {
    pos_camy -= 0.01;
    }
    } 
 }


 @Override
 public void run(){
  // This will reduce the load the applet has on the runtime
  // system..
  Thread.currentThread().setPriority(1);
  while (true) {
      e+=0.01;
      pos_camx = Math.cos(e);
      pos_camy = Math.sin(e);
      repaint();
     try {
      Thread.sleep(20);
     } catch (InterruptedException ie) {
     }
  }

}

public void init() {

        LP[0][0] = -1;
        LP[0][1] = -1;
        LP[0][2] = -1;

        LP[1][0] = 1;
        LP[1][1] = -1;
        LP[1][2] = -1;

        LP[2][0] = 1;
        LP[2][1] = 1;
        LP[2][2] = -1;

        LP[3][0] = -1;
        LP[3][1] = 1;
        LP[3][2] = -1;

        LP[4][0] = -1;
        LP[4][1] = -1;
        LP[4][2] = -1;

        LP[5][0] = 1;
        LP[5][1] = 1;
        LP[5][2] = -1;

        LP[6][0] = 1;
        LP[6][1] = 1;
        LP[6][2] = 1;

        LP[7][0] = -1;
        LP[7][1] = 1;
        LP[7][2] = 1;

        LP[8][0] = -1;
        LP[8][1] = -1;
        LP[8][2] = 1;

        LP[9][0] = 1;
        LP[9][1] = -1;
        LP[9][2] = 1;

        LP[10][0] = 1;
        LP[10][1] = 1;
        LP[10][2] = 1;

        LP[11][0] = -1;
        LP[11][1] = -1;
        LP[11][2] = 1;

        LP[12][0] = -1;
        LP[12][1] = -1;
        LP[12][2] = -1;

        LP[13][0] = 1;
        LP[13][1] = -1;
        LP[13][2] = 1;

        LP[14][0] = 1;
        LP[14][1] = -1;
        LP[14][2] = -1;

        LP[15][0] = 1;
        LP[15][1] = 1;
        LP[15][2] = 1;

        LP[16][0] = -1;
        LP[16][1] = 1;
        LP[16][2] = -1;

        LP[17][0] = -1;
        LP[17][1] = 1;
        LP[17][2] = 1;

        LP[18][0] = -1;
        LP[18][1] = -1;
        LP[18][2] = -1; 


 pos_camz = 0;

 rot_camx = 0;
 rot_camy = 0;
 rot_radx = 3.1415*rot_camx/180;
 rot_rady = 3.1415*rot_camy/180;

 nclip = 0.275;
 kx = 8.52/getWidth();
 ky = 5.46/getHeight();
}

 // Drawing instructions…
 public void paint(Graphics g) {
  super.paint(g);
  Graphics2D g1 = (Graphics2D)g;
  g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g.setColor(Color.black);
     xpoint = (double)(LP[0][0])/2;
     ypoint = 10 + (double)(LP[0][1])/2;
     zpoint = (double)(LP[0][2])/2;

     pxscr = (int)(
     ((xpoint - pos_camx)*Math.cos(rot_radx) + (ypoint + pos_camy)*Math.sin(rot_radx))*nclip
     /((ypoint - pos_camy)*Math.cos(rot_radx) + (pos_camx- xpoint)*Math.sin(rot_radx) + 0.0000000012)*100/kx
                  + getWidth()/2);

     pyscr = (int)(
                  getHeight()/2-
     (((ypoint + pos_camy)*Math.sin(rot_rady) + (zpoint - pos_camz)*Math.cos(rot_rady))*nclip
     /((ypoint - pos_camy)*Math.cos(rot_radx) + (pos_camx + xpoint)*Math.sin(rot_radx) + 0.0000000012)*100/ky)
                  );
     for (int i=1; i<19;i++){
            xpoint = (double)(LP[i][0])/2;
            ypoint = 10 + (double)(LP[i][1])/2;
            zpoint = (double)(LP[i][2])/2;

            nxscr = (int)(
         ((xpoint - pos_camx)*Math.cos(rot_radx) + (ypoint + pos_camy)*Math.sin(rot_radx))*nclip
         /((ypoint - pos_camy)*Math.cos(rot_radx)+(pos_camx + xpoint)*Math.sin(rot_radx)+0.0000000012)*100/kx
                         +getWidth()/2);

            nyscr = (int)(
                         getHeight()/2-
         (((ypoint + pos_camy)*Math.sin(rot_rady)+(zpoint - pos_camz)*Math.cos(rot_rady))*nclip
         /((ypoint - pos_camy)*Math.cos(rot_radx)+(pos_camx + xpoint)*Math.sin(rot_radx)+0.0000000012)*100/ky)
                         );
            g1.drawLine(pxscr,pyscr,nxscr,nyscr);
         pxscr = nxscr;
         pyscr = nyscr;
        }
         g.drawString("(" + pxscr + "," + pyscr + ")",20,40);
         g.drawString("" + pos_camx + ")",20,60);
         g.drawString("" + pos_camy,20,80);
         g.drawString("" + rot_camx,20,100);
  long currentTime = System.currentTimeMillis();
if (currentTime > nextSecond) {
    nextSecond += 1000;
    framesInLastSecond = framesInCurrentSecond;
    framesInCurrentSecond = 0;
}
    framesInCurrentSecond++;

g.drawString(framesInLastSecond + " fps", 20, 20);
}

// The standard Applet “GO” function…
public void start() {
   Thread th = new Thread(this);
  th.start();
}
}
3D类

import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.Graphics;
import java.applet.Applet;

public class DrawingDemo extends JFrame{
public DrawingDemo()
{
T3D demo = new T3D();
getContentPane().add(demo);
setVisible(true);
setSize(1024,720);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

T3D p = new T3D();
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu settings = new JMenu("Settings");
JMenu help = new JMenu("Help");
JMenuItem exit = new JMenuItem(">Exit");
JMenuItem imp = new JMenuItem(">Import");
JMenuItem exp = new JMenuItem(">Export");
JMenuItem sav = new JMenuItem(">Save");
JMenuItem ope = new JMenuItem(">Open");
file.add(ope);  
file.add(sav);
file.add(imp);
file.add(exp);
file.add(exit);
mb.add(file);
mb.add(edit);
mb.add(settings);
mb.add(help);
setJMenuBar(mb);
}

public static void main(String[] args)
{
    new DrawingDemo();
}

}
import java.awt.*;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;

 public class T3D extends JPanel implements Runnable {
 long nextSecond = System.currentTimeMillis() + 1000;
 int framesInLastSecond = 0;
 int framesInCurrentSecond = 0;
 int[][] LP= new int[19][3];
 double pos_camx,pos_camy,pos_camz,rot_camx,rot_camy,xpoint,ypoint,zpoint;
 double rot_radx,rot_rady,nclip,xscr,yscr,kx,ky,e;
 int pxscr,pyscr,nxscr,nyscr;

 public void start1() {
  Thread th = new Thread(this);
  th.start();

 }

 public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()){
    case KeyEvent.VK_A : 
    {
    pos_camx -= 0.01;
    }
    case KeyEvent.VK_D :
    {
    pos_camx += 0.01;
    }
    case KeyEvent.VK_W :
    {
    pos_camy += 0.01;
    }
    case KeyEvent.VK_S :
    {
    pos_camy -= 0.01;
    }
    } 
 }


 @Override
 public void run(){
  // This will reduce the load the applet has on the runtime
  // system..
  Thread.currentThread().setPriority(1);
  while (true) {
      e+=0.01;
      pos_camx = Math.cos(e);
      pos_camy = Math.sin(e);
      repaint();
     try {
      Thread.sleep(20);
     } catch (InterruptedException ie) {
     }
  }

}

public void init() {

        LP[0][0] = -1;
        LP[0][1] = -1;
        LP[0][2] = -1;

        LP[1][0] = 1;
        LP[1][1] = -1;
        LP[1][2] = -1;

        LP[2][0] = 1;
        LP[2][1] = 1;
        LP[2][2] = -1;

        LP[3][0] = -1;
        LP[3][1] = 1;
        LP[3][2] = -1;

        LP[4][0] = -1;
        LP[4][1] = -1;
        LP[4][2] = -1;

        LP[5][0] = 1;
        LP[5][1] = 1;
        LP[5][2] = -1;

        LP[6][0] = 1;
        LP[6][1] = 1;
        LP[6][2] = 1;

        LP[7][0] = -1;
        LP[7][1] = 1;
        LP[7][2] = 1;

        LP[8][0] = -1;
        LP[8][1] = -1;
        LP[8][2] = 1;

        LP[9][0] = 1;
        LP[9][1] = -1;
        LP[9][2] = 1;

        LP[10][0] = 1;
        LP[10][1] = 1;
        LP[10][2] = 1;

        LP[11][0] = -1;
        LP[11][1] = -1;
        LP[11][2] = 1;

        LP[12][0] = -1;
        LP[12][1] = -1;
        LP[12][2] = -1;

        LP[13][0] = 1;
        LP[13][1] = -1;
        LP[13][2] = 1;

        LP[14][0] = 1;
        LP[14][1] = -1;
        LP[14][2] = -1;

        LP[15][0] = 1;
        LP[15][1] = 1;
        LP[15][2] = 1;

        LP[16][0] = -1;
        LP[16][1] = 1;
        LP[16][2] = -1;

        LP[17][0] = -1;
        LP[17][1] = 1;
        LP[17][2] = 1;

        LP[18][0] = -1;
        LP[18][1] = -1;
        LP[18][2] = -1; 


 pos_camz = 0;

 rot_camx = 0;
 rot_camy = 0;
 rot_radx = 3.1415*rot_camx/180;
 rot_rady = 3.1415*rot_camy/180;

 nclip = 0.275;
 kx = 8.52/getWidth();
 ky = 5.46/getHeight();
}

 // Drawing instructions…
 public void paint(Graphics g) {
  super.paint(g);
  Graphics2D g1 = (Graphics2D)g;
  g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g.setColor(Color.black);
     xpoint = (double)(LP[0][0])/2;
     ypoint = 10 + (double)(LP[0][1])/2;
     zpoint = (double)(LP[0][2])/2;

     pxscr = (int)(
     ((xpoint - pos_camx)*Math.cos(rot_radx) + (ypoint + pos_camy)*Math.sin(rot_radx))*nclip
     /((ypoint - pos_camy)*Math.cos(rot_radx) + (pos_camx- xpoint)*Math.sin(rot_radx) + 0.0000000012)*100/kx
                  + getWidth()/2);

     pyscr = (int)(
                  getHeight()/2-
     (((ypoint + pos_camy)*Math.sin(rot_rady) + (zpoint - pos_camz)*Math.cos(rot_rady))*nclip
     /((ypoint - pos_camy)*Math.cos(rot_radx) + (pos_camx + xpoint)*Math.sin(rot_radx) + 0.0000000012)*100/ky)
                  );
     for (int i=1; i<19;i++){
            xpoint = (double)(LP[i][0])/2;
            ypoint = 10 + (double)(LP[i][1])/2;
            zpoint = (double)(LP[i][2])/2;

            nxscr = (int)(
         ((xpoint - pos_camx)*Math.cos(rot_radx) + (ypoint + pos_camy)*Math.sin(rot_radx))*nclip
         /((ypoint - pos_camy)*Math.cos(rot_radx)+(pos_camx + xpoint)*Math.sin(rot_radx)+0.0000000012)*100/kx
                         +getWidth()/2);

            nyscr = (int)(
                         getHeight()/2-
         (((ypoint + pos_camy)*Math.sin(rot_rady)+(zpoint - pos_camz)*Math.cos(rot_rady))*nclip
         /((ypoint - pos_camy)*Math.cos(rot_radx)+(pos_camx + xpoint)*Math.sin(rot_radx)+0.0000000012)*100/ky)
                         );
            g1.drawLine(pxscr,pyscr,nxscr,nyscr);
         pxscr = nxscr;
         pyscr = nyscr;
        }
         g.drawString("(" + pxscr + "," + pyscr + ")",20,40);
         g.drawString("" + pos_camx + ")",20,60);
         g.drawString("" + pos_camy,20,80);
         g.drawString("" + rot_camx,20,100);
  long currentTime = System.currentTimeMillis();
if (currentTime > nextSecond) {
    nextSecond += 1000;
    framesInLastSecond = framesInCurrentSecond;
    framesInCurrentSecond = 0;
}
    framesInCurrentSecond++;

g.drawString(framesInLastSecond + " fps", 20, 20);
}

// The standard Applet “GO” function…
public void start() {
   Thread th = new Thread(this);
  th.start();
}
}
import java.awt.*;
导入java.applet.applet;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.event.*;
导入javax.swing.*;
公共类T3D扩展JPanel实现可运行{
long nextSecond=System.currentTimeMillis()+1000;
int framesinastsecond=0;
int framesInCurrentSecond=0;
int[]LP=新int[19][3];
双pos_camx、pos_camy、pos_camz、rot_camx、rot_camy、xpoint、ypoint、zpoint;
双rot_radx,rot_rady,nclip,xscr,yscr,kx,ky,e;
int pxscr、pyscr、nxscr、nyscr;
公开作废开始1(){
螺纹th=新螺纹(此);
th.start();
}
按下公共无效键(按键事件e){
开关(如getKeyCode()){
case KeyEvent.VK_A:
{
pos_camx-=0.01;
}
case KeyEvent.VK_D:
{
pos_camx+=0.01;
}
case KeyEvent.VK_W:
{
pos_camy+=0.01;
}
case KeyEvent.VK_S:
{
pos_camy-=0.01;
}
} 
}
@凌驾
公开募捐{
//这将减少小程序在运行时上的负载
//系统。。
Thread.currentThread().setPriority(1);
while(true){
e+=0.01;
pos_camx=数学cos(e);
pos_camy=数学sin(e);
重新油漆();
试一试{
睡眠(20);
}捕获(中断异常ie){
}
}
}
公共void init(){
LP[0][0]=-1;
LP[0][1]=-1;
LP[0][2]=-1;
LP[1][0]=1;
LP[1][1]=-1;
LP[1][2]=-1;
LP[2][0]=1;
LP[2][1]=1;
LP[2][2]=-1;
LP[3][0]=-1;
LP[3][1]=1;
LP[3][2]=-1;
LP[4][0]=-1;
LP[4][1]=-1;
LP[4][2]=-1;
LP[5][0]=1;
LP[5][1]=1;
LP[5][2]=-1;
LP[6][0]=1;
LP[6][1]=1;
LP[6][2]=1;
LP[7][0]=-1;
LP[7][1]=1;
LP[7][2]=1;
LP[8][0]=-1;
LP[8][1]=-1;
LP[8][2]=1;
LP[9][0]=1;
LP[9][1]=-1;
LP[9][2]=1;
LP[10][0]=1;
LP[10][1]=1;
LP[10][2]=1;
LP[11][0]=-1;
LP[11][1]=-1;
LP[11][2]=1;
LP[12][0]=-1;
LP[12][1]=-1;
LP[12][2]=-1;
LP[13][0]=1;
LP[13][1]=-1;
LP[13][2]=1;
LP[14][0]=1;
LP[14][1]=-1;
LP[14][2]=-1;
LP[15][0]=1;
LP[15][1]=1;
LP[15][2]=1;
LP[16][0]=-1;
LP[16][1]=1;
LP[16][2]=-1;
LP[17][0]=-1;
LP[17][1]=1;
LP[17][2]=1;
LP[18][0]=-1;
LP[18][1]=-1;
LP[18][2]=-1;
pos_camz=0;
rot_camx=0;
rot_camy=0;
rot_radx=3.1415*rot_camx/180;
rot_rady=3.1415*rot_camy/180;
nclip=0.275;
kx=8.52/getWidth();
ky=5.46/getHeight();
}
//绘图说明…
公共空间涂料(图g){
超级油漆(g);
图形2d g1=(图形2d)g;
g1.setRenderingHint(renderingHits.KEY\u ANTIALIASING,renderingHits.VALUE\u ANTIALIAS\u ON);
g、 设置颜色(颜色为黑色);
xpoint=(双精度)(LP[0][0])/2;
ypoint=10+(双)(LP[0][1])/2;
zpoint=(双)(LP[0][2])/2;
pxscr=(int)(
((xpoint-pos_-camx)*数学cos(rot_-radx)+(ypoint+pos_-camy)*数学sin(rot_-radx))*nclip
/((ypoint-pos_-camy)*数学cos(rot_-radx)+(pos_-camx-xpoint)*数学sin(rot_-radx)+0.000000012)*100/kx
+getWidth()/2);
pyscr=(int)(
getHeight()/2-
((ypoint+pos_-camy)*数学sin(rot_-rady)+(zpoint-pos_-camz)*数学cos(rot_-rady))*nclip
/((ypoint-pos_-camy)*数学cos(rot_-radx)+(pos_-camx+xpoint)*数学sin(rot_-radx)+0.00000000 12)*100/ky)
);
for(int i=1;i下一秒){
下一秒+=1000;
framesInLastSecond=framesInCurrentSecond;
framesInCurrentSecond=0;
}
framesInCurrentSecond++;
g、 抽绳(帧最后一秒+“fps”,20,20);
}
//标准小程序“GO”功能…
公开作废开始(){
螺纹th=新螺纹(此);
th.start();
}
}

现在我想我明白你的问题了。您已经使用了一个扩展JApplet的类,并试图将其更改为JPanel,而保持init()方法和所有内容不变。Swing不是那样工作的。您需要重新编写JApplet类型类,去掉特定于applet的方法,如init()和start(),而可能在类的构造函数中初始化该代码。您需要了解如何在Swing中使用图形和动画,例如使用JPanel的paintComponent方法和Swing计时器,这种方法很可能会成功。

什么小程序?扩展Applet或JApplet的类在哪里?您的问题和代码似乎没有关联。请澄清。还要注意,即使您有小程序代码,您通常也不会在JPanels中运行小程序,小程序是一个类似JFrame的顶级窗口,因此它通常保存JPanels,而不是相反。PS:我试图扩展Applet,但结果是一样的。您声明您试图在JPanel中运行Applet,而您的代码没有显示任何类似的内容。让你澄清你的问题使之有意义对我来说似乎很有帮助。