问题描述
... WAW !!
我实际上将我的JLabel变量命名为 jll_img
我已经设置了一个图标。
我的目的是旋转这个jlabel,我把图标放进去。
我通过制作一个BUtton
,然后当按钮被点击时,它将由线程旋转。
一个单一的匿名线程,将从0度循环到360度。
这里
`//我的按钮动作
new Thread(){
private boolean doRotating = true;
private double norm = 0.0;
private double numbs = 0.0;
private double degreeIncrementation = 10.0;
@Override
public void run(){
while(doRotating){
try {
ci = new CompoundIcon(jll_img.getIcon(),new TextIcon(jll_img,));
if(numbs == 360){
numbs = 0.0;
System.err.println(转换成较早的帖子);
doRotating = false;
ri = new RotatedIcon(ci,norm);
} else {
ri = new RotatedIcon(ci,degreeIncrementation);
}
System.err.println(degree is+ numbs);
jll_img.setIcon(ri);
numbs + = degreeIncrementation;
this.sleep(10);
ri = null;
ci = null;
} catch(Exception exp){
System.err.println(blurp!);
exp.printStackTrace();
}
}
}
} .start();`
要旋转,我需要
添加两个类,即和
因为这两个类主要做这个工作。
当我尝试点击它直到第22次时,我得到了这个错误。 p>
线程中的异常AWT-EventQueue-0java.lang.StackOverflowError
at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211 )
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java: 129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.R otatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在逻辑.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.Rotated Icon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在逻辑.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.g etIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon .getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在逻辑。 RotateIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在逻辑.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129)
在logic.CompoundIcon.getIconHeight(CompoundIcon.java:211)
在logic.CompoundIcon.paintI con(CompoundIcon.java:229)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon在逻辑1.CompoundIcon.paintIcon(CompoundIcon.java:233)上的
$逻辑上的
(RotatedIcon.java:175)
在逻辑.RotatedIcon.paintIcon(RotatedIcon.java:175)
在逻辑。 CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在逻辑.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.p aintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
at logic.CompoundIcon .paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在逻辑。 RotateIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在逻辑.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
at logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在逻辑上con.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在逻辑.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
在logic.RotatedIcon.paintIcon(RotatedIcon.java:175)
在logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
这怎么可能发生?
如果它可以运行到第21 ...但下一个22,等等将最终失败?
很少有评论:
a)没有必要使用CompoundIcon。您发布的代码是使用TextIcon的空字符串。
b)您不应该嵌套RotatedIcons。如果你嵌套,实际轮换将是所有漫游的总和,因此旋转将不会被顺序。如果你嵌套3个角度为1,2,3的图标,那么你实际上旋转了6度,而不是3。现在你的代码就像:
RotatedIcon ri = new RotatedIcon(ri,angle);
相反,您应该使用:
RotatedIcon ri = new RotatedIcon(ri.getIcon(),angle);
这是一个重组版本的代码,我认为你想要的:
import java.awt。*;
import java.awt.event。*;
import javax.swing。*;
import javax.swing.event。*;
public class IconSSCCE扩展JPanel实现ActionListener
{
JLabel label = new JLabel();
TextIcon文本;
ImageIcon image;
浮点角= 0.0f;
public IconSSCCE()
{
setLayout(new BorderLayout());
text = new TextIcon(label,Some Text,TextIcon.Layout.HORIZONTAL);
// text = new TextIcon(label,,TextIcon.Layout.HORIZONTAL);
image = new ImageIcon(dukeWaveRed.gif);
CompoundIcon ci = new CompoundIcon(CompoundIcon.Axis.X_AXIS,image,text);
label.setIcon(ci);
add(label,BorderLayout.NORTH);
JButton rotate = new JButton(Rotate);
add(rotate,BorderLayout.SOUTH);
rotate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new Timer(50,IconSSCCE.this).start() ;
}
});
}
public void actionPerformed(ActionEvent e)
{
angle ++;
RotatedIcon ri = new RotatedIcon(image,angle);
CompoundIcon ci = new CompoundIcon(ri,text);
label.setIcon(ci);
}
private static void createAndShowUI()
{
JFrame frame = new JFrame(IconSSCCE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new IconSSCCE());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
waw...!!
I actually have my JLabel variable named as jll_imgAnd I already set an icon into it.
My purpose is to rotate this jlabel that i put icon into it.
I test it out by making a single BUtton,and then when the button is clicked, it will do rotating by a thread.A single anonymous thread that will do loop from 0 degree into 360 degree.
Here
`// my button actions
new Thread() {
private boolean doRotating = true;
private double norm = 0.0;
private double numbs = 0.0;
private double degreeIncrementation = 10.0;
@Override
public void run() {
while (doRotating) {
try {
ci = new CompoundIcon(jll_img.getIcon(), new TextIcon(jll_img, ""));
if (numbs == 360) {
numbs = 0.0;
System.err.println("transformed into earlier post");
doRotating = false;
ri = new RotatedIcon(ci, norm);
} else {
ri = new RotatedIcon(ci, degreeIncrementation);
}
System.err.println("degree is " + numbs);
jll_img.setIcon(ri);
numbs += degreeIncrementation;
this.sleep(10);
ri = null;
ci = null;
} catch (Exception exp) {
System.err.println("blurp!");
exp.printStackTrace();
}
}
}
}.start();`
To do rotating I need toadd two more classes namely as RotatedIcon.java and CompoundIcon.javabecause this two classes that do the job mostly.
Somehow I got this error when I tried to click it until 22nd times.
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.RotatedIcon.getIconHeight(RotatedIcon.java:129) at logic.CompoundIcon.getIconHeight(CompoundIcon.java:211) at logic.CompoundIcon.paintIcon(CompoundIcon.java:229) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233) at logic.RotatedIcon.paintIcon(RotatedIcon.java:175) at logic.CompoundIcon.paintIcon(CompoundIcon.java:233)
how could this happened?if it could run until 21st... but the next 22nd and so on will eventually failed?
A few comments:
a) there is no need for you to use a CompoundIcon. The code you posted is using an empty String for the TextIcon.
b) you should not nest RotatedIcons. If you nest, the actual rotation will be the sum of all the roatations so the rotation will not be sequentail. If you nest 3 icons with angles of 1, 2, 3, then you are actually rotating a total of 6 degrees, not 3. Right now your code is like:
RotatedIcon ri = new RotatedIcon(ri, angle);
Instead you should be using:
RotatedIcon ri = new RotatedIcon(ri.getIcon(), angle);
Here is a restructured version of you code which I think does what you want:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class IconSSCCE extends JPanel implements ActionListener
{
JLabel label = new JLabel();
TextIcon text;
ImageIcon image;
Float angle = 0.0f;
public IconSSCCE()
{
setLayout( new BorderLayout() );
text = new TextIcon(label, "Some Text", TextIcon.Layout.HORIZONTAL);
// text = new TextIcon(label, "", TextIcon.Layout.HORIZONTAL);
image = new ImageIcon("dukeWaveRed.gif");
CompoundIcon ci = new CompoundIcon(CompoundIcon.Axis.X_AXIS, image, text);
label.setIcon( ci );
add(label, BorderLayout.NORTH);
JButton rotate = new JButton("Rotate");
add(rotate, BorderLayout.SOUTH);
rotate.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new Timer(50, IconSSCCE.this).start();
}
});
}
public void actionPerformed(ActionEvent e)
{
angle++;
RotatedIcon ri = new RotatedIcon(image, angle);
CompoundIcon ci = new CompoundIcon(ri, text);
label.setIcon( ci );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("IconSSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new IconSSCCE() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
这篇关于Java JLabel与StackOverflowError旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!