Pergunta

I'm trying to add 2 seperate JLabels to 2 seperate JPanels, all within one JWindow! The first label displays fine in the first JPanel. However the second JLabel doesnt show, although the second JPanel does! So I just need to know basically how to add a component (in this case the second JLabel) to the second JPanel. Any help appreciated...

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

Im using 2 almost identical JPanels called numchuckPanel1 and numchuckPanel2. Heres the code:

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){}



}

And the code for the second NumchuckPanel here:

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){}
}
Foi útil?

Solução

OK, managed to solve the problem.

The second Jpanel was aligning the JLabel to the right. Using a Left aligned flowlayout on my panels solved this:

JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top