Java 为什么不能';我不能将一个带有图形的jpanel添加到另一个jpanel吗?

Java 为什么不能';我不能将一个带有图形的jpanel添加到另一个jpanel吗?,java,swing,graphics,compiler-errors,jpanel,Java,Swing,Graphics,Compiler Errors,Jpanel,/我想在一个jPanel,jpSpot中添加一个移动方块,它将被添加到另一个jPanel,jp中。但是当我在代码中添加“jp.add(jpSpot);frm1.add(jp);”时,在这两行上会出现错误。请参阅下面的代码:/ /由于这两行,jp.add(jpSpot);和frm1.add(日本);出现错误,程序无法运行。//p>//此行出现错误什么错误?始终复制/粘贴错误和异常输出!我必须编辑代码并重试。//这一行出现错误什么错误?始终复制/粘贴错误和异常输出!我必须编辑代码并重试。 impor

/我想在一个jPanel,jpSpot中添加一个移动方块,它将被添加到另一个jPanel,jp中。但是当我在代码中添加“jp.add(jpSpot);frm1.add(jp);”时,在这两行上会出现错误。请参阅下面的代码:/


/由于这两行,jp.add(jpSpot);和frm1.add(日本);出现错误,程序无法运行。//p>
//此行出现错误
什么错误?始终复制/粘贴错误和异常输出!我必须编辑代码并重试。
//这一行出现错误
什么错误?始终复制/粘贴错误和异常输出!我必须编辑代码并重试。
import java.awt.*;
import javax.swing.*;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

class jpSpot extends JPanel{    
    int x = 250;    
    int y = 100;        
    public void display(){      
        x --;       
        //y ++; 
        this.repaint(); 
    }

public void paint(Graphics g)   {
    super.paint(g); 
    Graphics2D g2d = (Graphics2D)g; 
    Color customColor = new Color(100,0,153); //Purple color
    g2d.setColor(customColor);
    g2d.fill3DRect(x, y, 50, 50, true);
    }
}

public class PlaySong {

    public static void main(String[] args) {

        Song song1 = new Song("紫竹調", 4, 4, "C");

        char[] lyric = {};
        int[] numNotes = {};
        int[][] notes = {};

        JFrame frm1 = new JFrame("康樂彩歌");
     frm1.setBounds(0, 0, 1368, 500);
        frm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label1 = new JLabel("選歌:");    //Label is created
         label1.setFont(new Font("新細明體", Font.PLAIN, 20));

        JPanel jp = new JPanel();    //The first jPanel is created

         JComboBox cmbox = new JComboBox();    //ComboBox is created
        cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));

        cmbox.addItem("紫竹調");
         cmbox.addItem("走一同去郊遊");
         cmbox.addItem("大野狼");
         cmbox.addItem("歸來吧蘇連多");
         cmbox.addItem("追尋");
         cmbox.addItem("三輪車");
         cmbox.addItem("我家門前有小河");
         cmbox.addItem("漁家樂");
         cmbox.addItem("嚕啦啦");
         cmbox.addItem("踏雪尋梅");

         jp.add(label1);
        jp.add(cmbox);

        frm1.setVisible(true);

        JRadioButton rb1 = new JRadioButton("加簡譜", false);
        rb1.setFont(new Font("新細明體", Font.PLAIN, 20));

        JRadioButton rb2 = new JRadioButton("加人聲", false);
        rb2.setFont(new Font("新細明體", Font.PLAIN, 20));

        rb1.setBounds(450, 180, 50, 50);
        rb2.setBounds(500, 180, 50, 50);

        jp.add(rb1);
        jp.add(rb2);

        jpSpot spot1 = new jpSpot();
        spot1.setVisible(true); 

        while(true){
            spot1.display();
            try {
                Thread.sleep(300);  
                }
                catch (InterruptedException e)  
                {
                e.printStackTrace();}
                }
        }
        jp.add(jpSpot);//  Error appears on this line
        frm1.add(jp);  //  Error appears on this line
}


public class Song {
     String title;
     int beats;
     int noteunit;
     String key;
     Song(String title, int beats, int noteunit, String key){
         this.title = title;
         this.beats = beats;
         this.noteunit = noteunit;
         this.key = key;
    }
}