看看我放在这里的评论,任何想法,为什么这不会工作的人?它使用FlowLayout btw,这应该是完全正常工作。我真的可以使用一些帮助,非常感谢!JPanel定位器不工作的位置
package Primary;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class OpenPanel implements Runnable,Header{
private static String panel;
public OpenPanel(String enterpanel){
panel = enterpanel;
}
private Timer nextloc;
private JPanel usePanel;
private static ClosePanel cp = new ClosePanel("null");
public void run(){
Timer andgo = new Timer(10,new TimerListener());
while(!cp.getCloseDone()){
andgo.start();
andgo.restart();
}
if(panel.equals("topiccreator")){
usePanel=topiccreator;
}else{
System.out.println("Error moving panel");
usePanel=mainmenu;
}
// This is the setLocation part that does nothing
// All the set locations in the loop with the timer work completely fine
usePanel.setLocation(usePanel.getX()+800,usePanel.getY());
usePanel.setVisible(true);
nextloc = new Timer(50,new TimerListener());
for(int r=0;r<3500;r+=6){
nextloc.start();
nextloc.restart();
}
}
private class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==nextloc){
usePanel.setLocation(usePanel.getX()-5,usePanel.getY());
}
}
}
}
此代码段是不编译的,所以很难说一些事情.. –
的代码是没有意义的。首先,面板在布局管理器的控制下,下一次重新验证时,所有更改都将失效,并且布局管理器将按照需要执行任务。在你的循环中,你声明和重新设置(启动)定时器,在循环完成之前,定时器并没有改变实际触发。只需启动它,让它运行,直到满足最终条件... – MadProgrammer
[setLocation不工作]的可能重复(http://stackoverflow.com/questions/14857219/setlocation-do-not-work) – Piro