Java 无法在单个JWindow中将第二个JLabel添加到第二个JPanel
我正在尝试将两个独立的jlabel添加到两个独立的jpanel中,都在一个JWindow中!第一个标签在第一个JPanel中显示良好。然而,第二个JLabel没有显示,尽管第二个JPanel显示!所以我只需要知道如何将一个组件(在本例中是第二个JLabel)添加到第二个JPanel。感谢您的帮助Java 无法在单个JWindow中将第二个JLabel添加到第二个JPanel,java,swing,jpanel,flowlayout,Java,Swing,Jpanel,Flowlayout,我正在尝试将两个独立的jlabel添加到两个独立的jpanel中,都在一个JWindow中!第一个标签在第一个JPanel中显示良好。然而,第二个JLabel没有显示,尽管第二个JPanel显示!所以我只需要知道如何将一个组件(在本例中是第二个JLabel)添加到第二个JPanel。感谢您的帮助 package numchucks; import java.awt.*; import java.util.TimerTask; import javax.swing.*; import java.
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使用两个几乎相同的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){}
}
package-numchucks;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.Timer;
导入java.util.TimerTask;
导入javax.swing.*;
/**
*
*@作者奥布里
*/
公共类NumchuckPanel1扩展了JPanel实现MouseListener{
int CircleCenter;
内环肠;
整数半径;
双θ;
私人最终双重薪酬;
静态定时器;
静态工具箱=null;
静态维度dim=null;
静态整数宽度=空;
静态整数高度=空;
静态NumchuckPanel1标签;
静态布尔值isFin=false;
静态int w=100;
静态int h=100;
静态布尔等参线=真;
//静态JLabel标题1;
//静态JLabel标题2;
静窗框;
公共NumchuckPanel1(字符串文本//
整数圈中心,整数圈中心,整数半径//
双首字母θ,双首字母θ){
//超级(文本);
//这个。设置大小(150150);
这个.背景(颜色.白色);
此.setVisible(true);
this.circleCenterX=circleCenterX;
this.circlecentry=circlecentry;
这个半径=半径;
this.theta=初始θ;
这个。防腐层=防腐层;
旋转();
}
公共空间(英文){
如果(w!=500){
w++;
h++;
}
挫折(//
this.circlecentrex-(int)(数学sin(this.theta)*this.radius)//
this.circlecentre-(int)(数学cos(this.theta)*this.radius)//
w、 h);
this.theta-=this.thetaIncrement;
如果(这个θ=数学托拉迪安(90)){
//θ=0.0d;
System.out.println(“停止计时器…”);
Numchucks.timer.cancel();
isFin=true;
}
}
公共无效mouseClicked(MouseEvent me){
if(等参线){
isOpen=假;
NumchuckPanel2.isOpen=false;
Numchucks.timer=新计时器();
Numchucks.timer.scheduleAtFixedRate(新的TimerTask(){
@凌驾
公开募捐{
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
Numchucks.label.reverse();
Numchucks.label2.reverse();
系统输出打印ln(θ);
//标签2.旋转();
//System.out.println(label2.theta);
}
});
}
},0,1000l/560l);//每秒60帧
返回;
}
如果(!isOpen){
isOpen=真;
NumchuckPanel2.isOpen=true;
Numchucks.timer=新计时器();
Numchucks.timer.scheduleAtFixedRate(新的TimerTask(){
@凌驾
公开募捐{
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
Numchucks.label.rotate();
Numchucks.label2.rotate();
系统输出打印ln(θ);
//标签2.旋转();
//System.out.println(label2.theta);
}
});
}
},0,1000l/560l);//每秒60帧
返回;
}
}
公共无效mouseenterned(MouseEvent me){}
public void mousePressed(MouseEvent me){
}
公共无效MouseEvent me(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){}
}
package-numchucks;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.Timer;
导入java.util.TimerTask;
导入javax.swing.*;
/**
*
*@作者奥布里
*/
公共类NumchuckPanel2扩展了JPanel实现MouseListener{
int CircleCenter;
内环肠;
整数半径;
双θ;
私人最终双重薪酬;
静态定时器;
静态工具箱=null;
静态维度dim=null;
静态整数宽度=空;
静态整数高度=空;
静态NumchuckPanel2 label2;
静态布尔值isFin=false;
静态int w=100;
静态int h=100;
静态布尔等参线=真;
//静态JLabel标题1;
//静态JLabel标题2;
静窗框;
公共NumchuckPanel2(字符串文本//
整数圈中心,整数圈中心,整数半径//
双首字母θ,双首字母θ){
//超级(文本);
//这个。设置大小(150150);
这个.背景(颜色.白色);
此.setVisible(true);
this.circleCenterX=circleCenterX;
this.circlecentry=circlecentry;
这个半径=半径;
this.theta=初始θ;
这个。防腐层=防腐层;
旋转();
}
公共空间(英文){
如果(w!=500){
w++;
h++;
}
挫折(//
this.circlecentrex-(int)(数学sin(this.theta)*this.radius)//
this.circlecentre-(int)(数学cos(this.theta)*this.radius)//
w、 h);
this.theta-=this.thetaIncrement;
//System.out.println(this.theta);
如果(这个θ=数学托拉迪安(270)){
//θ=0.0d;
//System.out.println(“停止计时器…”);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));