Почему я не могу добавить jpanel с графикой в ​​другую jpanel? - PullRequest
1 голос
/ 26 октября 2019

/ Я хочу добавить движущийся квадрат в jPanel, jpSpot, который должен быть добавлен к другой jpanel, jp. Но когда я добавляю "jp.add (jpSpot); frm1.add (jp);"в мой код, на этих двух строках появляются ошибки. Пожалуйста, смотрите код ниже: /

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;
    }
}

/ Поскольку эти две строки, jp.add (jpSpot);и frm1.add (jp);есть ошибки, программа не может быть запущена. /

...