2012-06-11 37 views
0

我想添加2個單獨的JLabels到2個獨立的JPanels,全部在一個JWindow中!第一個標籤在第一個JPanel中顯示正常。然而,第二個JLabel不顯示,儘管第二個JPanel是!所以我只需要知道如何添加一個組件(在本例中是第二個JLabel)到第二個JPanel。任何幫助讚賞...不能添加第二個JLabel到單JWindow中的第二個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幾乎相同的JPanels稱爲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){} 
} 

回答

1

OK,設法解決問題。

第二個Jpanel正在將JLabel調整到右側。在我的面板上使用左對齊的流程圖解決了此問題:

JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));