0

我正在尝试将 2 个单独的 JLabels 添加到 2 个单独的 JPanel,所有这些都在一个 JWindow 中!第一个标签在第一个 JPanel 中显示良好。然而第二个 JLabel 没有显示,虽然第二个 JPanel 显示了!所以我只需要基本上知道如何将组件(在本例中为第二个 JLabel)添加到第二个 JPanel。任何帮助表示赞赏...

package numchucks;

import java.awt.*;
import java.util.TimerTask;
import javax.swing.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;


/**
 *
 * @author aubrey
 */
public class Numchucks {


    Toolkit toolkit = null;
    Dimension dim = null;
    Integer width = null;
    Integer height = null;
    static NumchuckPanel1 label;
    static NumchuckPanel2 label2;

    JWindow frame = new JWindow();
    Container c = null;

    JLabel title1 = new JLabel("Player 1.");
    JLabel title2 = new JLabel("Player 2.");

    static Timer timer;

    public Numchucks(){
        //get screensize
        //double initialTheta = Math.toRadians(0); // start north
        double initialTheta = Math.toRadians(90); // start west
        // double initialTheta = Math.toRadians(180); // start south
        double initialTheta2 = Math.toRadians(270); // start east

        label = new NumchuckPanel1("Foo Bar", //
                450, // circleCenterX
                325, // circleCenterY
                300, // radius
                initialTheta, //
                0.005); // thetaIncrement


        label2 = new NumchuckPanel2("Foo Bar", //
                450, // circleCenterX
                325, // circleCenterY
                300, // radius
                initialTheta2, //
                0.005); // thetaIncrement

        label.setBorder(BorderFactory.createLineBorder (Color.black, 1));
       label2.setBorder(BorderFactory.createLineBorder (Color.black, 1));

        label.addMouseListener(label);
        label2.addMouseListener(label2);

            label.add(title1); 

            //Problem is Here, why isnt title2 visible on label2????
            label2.add(title2);


         //get screensize
        toolkit =  Toolkit.getDefaultToolkit ();
        dim = toolkit.getScreenSize();
        width = dim.width;
        height = dim.height;
        frame = new JWindow();
        frame.setSize(dim);
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(label);
        frame.add(label2);
        frame.setVisible(true);

        timer = new java.util.Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {

                        label.rotate();
                        //System.out.println(label.theta);
                        label2.rotate();
                        //System.out.println(label2.theta);
                    }
                });

            }
        }, 0, 1000l / 560l); // 60 frames per second

    }

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

我使用 2 个几乎相同的 JPanel,称为 numchuckPanel1 和 numchuckPanel2。继承人的代码:

package numchucks;

import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;

/**
 *
 * @author aubrey
 */
public class NumchuckPanel1 extends JPanel implements MouseListener{
     int circleCenterX;
     int circleCenterY;
     int radius;
     double theta;
    private final double thetaIncrement;
    static Timer timer;
    static Toolkit toolkit = null;
    static Dimension dim = null;
    static Integer width = null;
    static Integer height = null;
    static NumchuckPanel1 label;

    static Boolean isFin = false;
    static int w = 100;
    static int h = 100;
    static Boolean isOpen = true;
    //static JLabel title1;
    //static JLabel title2;
    static JWindow frame;

    public NumchuckPanel1(String text, //
        int circleCenterX, int circleCenterY, int radius, //
        double initialTheta, double thetaIncrement) {
        //super(text);
        //this.setSize(150, 150);

        this.setBackground(Color.WHITE);
        this.setVisible(true);
        this.circleCenterX = circleCenterX;
        this.circleCenterY = circleCenterY;
        this.radius = radius;
        this.theta = initialTheta;
        this.thetaIncrement = thetaIncrement;
        rotate();
    }

    public void rotate() {
                      if (w!=500){
                    w++;
                    h++;
    }
        setBounds( //

                this.circleCenterX - (int) (Math.sin(this.theta) * this.radius), //
                this.circleCenterY - (int) (Math.cos(this.theta) * this.radius), //
                w, h);
        this.theta -= this.thetaIncrement;

        if(this.theta<=-4.704203673205014){
            System.out.println("Stopping Timer....");
            Numchucks.timer.cancel();

            isFin=true;
        }
    }
 public void reverse() {
                      if (w!=100){
                    w--;
                    h--;
    }
        setBounds( //

                this.circleCenterX - (int) (Math.sin(this.theta) * this.radius), //
                this.circleCenterY - (int) (Math.cos(this.theta) * this.radius), //
                w, h);
        this.theta += this.thetaIncrement;

        if(this.theta>=Math.toRadians(90)){
            //this.theta=0.0d;
            System.out.println("Stopping Timer....");
           Numchucks.timer.cancel();

            isFin=true;

        }
    }
   public void mouseClicked(MouseEvent me) {
       if(isOpen){
           isOpen=false;
           NumchuckPanel2.isOpen=false;
        Numchucks.timer = new Timer();
        Numchucks.timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {

                        Numchucks.label.reverse();
                        Numchucks.label2.reverse();
                        System.out.println(theta);
                        //label2.rotate();
                        //System.out.println(label2.theta);
                    }
                });

            }
        }, 0, 1000l / 560l); // 60 frames per second
       return;
       }
       if(!isOpen){
       isOpen=true;
       NumchuckPanel2.isOpen=true;
        Numchucks.timer = new Timer();
        Numchucks.timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {

                        Numchucks.label.rotate();
                        Numchucks.label2.rotate();
                        System.out.println(theta);
                        //label2.rotate();
                        //System.out.println(label2.theta);
                    }
                });

            }
        }, 0, 1000l / 560l); // 60 frames per second
       return;
       }
    }
     public void mouseEntered (MouseEvent me) {}

     public void mousePressed (MouseEvent me) {

    }
     public void mouseReleased (MouseEvent me) {

    } 
     public void mouseExited(MouseEvent me){}



}

第二个 NumchuckPanel 的代码在这里:

package numchucks;

import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;

/**
 *
 * @author aubrey
 */
public class NumchuckPanel2 extends JPanel implements MouseListener{
     int circleCenterX;
     int circleCenterY;
     int radius;
     double theta;
    private final double thetaIncrement;
    static Timer timer;
    static Toolkit toolkit = null;
    static Dimension dim = null;
    static Integer width = null;
    static Integer height = null;
    static NumchuckPanel2 label2;

    static Boolean isFin = false;
    static int w = 100;
    static int h = 100;
    static Boolean isOpen = true;
    //static JLabel title1;
    //static JLabel title2;
    static JWindow frame;

    public NumchuckPanel2(String text, //
        int circleCenterX, int circleCenterY, int radius, //
        double initialTheta, double thetaIncrement) {
        //super(text);
        //this.setSize(150, 150);

        this.setBackground(Color.WHITE);
        this.setVisible(true);
        this.circleCenterX = circleCenterX;
        this.circleCenterY = circleCenterY;
        this.radius = radius;
        this.theta = initialTheta;
        this.thetaIncrement = thetaIncrement;
        rotate();
    }

    public void rotate() {
                      if (w!=500){
                    w++;
                    h++;
    }
        setBounds( //

                this.circleCenterX - (int) (Math.sin(this.theta) * this.radius), //
                this.circleCenterY - (int) (Math.cos(this.theta) * this.radius), //
                w, h);
        this.theta -= this.thetaIncrement;


        //System.out.println(this.theta);

       if(this.theta<=-1.5776110196152204){
            System.out.println("Stopping Timer....");
            //Numchucks.timer.cancel();

            isFin=true;
        }



    }
     public void reverse() {
                      if (w!=100){
                    w--;
                    h--;
    }
        setBounds( //

                this.circleCenterX - (int) (Math.sin(this.theta) * this.radius), //
                this.circleCenterY - (int) (Math.cos(this.theta) * this.radius), //
                w, h);
        this.theta += this.thetaIncrement;
/*
-4.664203673205015
-4.669203673205015
-4.674203673205015
-4.679203673205015
-4.6842036732050145
-4.689203673205014
-4.694203673205014
-4.704203673205014
         */
        if(this.theta>=Math.toRadians(270)){
            //this.theta=0.0d;
            //System.out.println("Stopping Timer....");
           Numchucks.timer.cancel();

            isFin=true;

            /*for (int i = 100; i<500; i++){
                label.setSize(i,i);

            }*/
            //System.exit(0);
        }
    }
     public void mouseClicked (MouseEvent me) {
       if(isOpen){
           isOpen=false;
           NumchuckPanel1.isOpen=false;

        Numchucks.timer = new Timer();
        Numchucks.timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {

                        Numchucks.label.reverse();
                        Numchucks.label2.reverse();
                        //System.out.println(label.theta);
                        //label2.rotate();
                        //System.out.println(label2.theta);
                    }
                });

            }
        }, 0, 1000l / 560l); // 60 frames per second
        return;
       }
       if(!isOpen){
        isOpen=true;
        NumchuckPanel1.isOpen=true;

        Numchucks.timer = new Timer();
        Numchucks.timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {

                        Numchucks.label.rotate();
                        Numchucks.label2.rotate();
                        //System.out.println(label.theta);
                        //label2.rotate();
                        //System.out.println(label2.theta);
                    }
                });

            }
        }, 0, 1000l / 560l); // 60 frames per second
       return;
       }
    }
     public void mouseEntered (MouseEvent me) {}

     public void mousePressed (MouseEvent me) {

    }
     public void mouseReleased (MouseEvent me) {

    } 
     public void mouseExited(MouseEvent me){}
}
4

1 回答 1

1

好的,设法解决了问题。

第二个 Jpanel 将 JLabel 向右对齐。在我的面板上使用左对齐流布局解决了这个问题:

JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
于 2012-06-11T22:55:06.603 回答