2015-11-13 99 views
0

Pic防止JLabel职位重置?

在左侧面板上我想我的JLabel左右移动的盒子内,并在右侧面板我希望我的JLabel与当前时间频繁更新,而盒子内走动,以随机位置上的随机位置以及。

我现在收到的问题是,当我设置JLabel的时间和位置时,我的JLabel在我的左边框中出现了错误。在移动到它应该移动的随机位置之前,它首先出现在初始位置(顶部)。

我有一个类实现了runnable,它处理左边的JLabel,另一个类也实现了可随时间更新处理JLabel的runnable。

任何人都可以帮我解决这个问题吗?我希望通过这种方式来设置这个程序,其中两个类实现可运行的JLabel移动的runnable。

这是左侧面板,关心班级:

public class MoveDisplay { 
    private GUIFrame gui; 
    private boolean moving = true; 


    public MoveDisplay(GUIFrame gui) { 
     this.gui = gui; 
    } 

    public void start() { 
     moving = true; 
     Random rand = new Random(); 
     while (moving) { 
      int x = rand.nextInt(150) + 1; 
      int y = rand.nextInt(150) + 1; 

      java.awt.EventQueue.invokeLater(new Runnable() { 
        public void run() { 

         gui.moveDisplay(x, y, 100, 100); 
        } 
       }); 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void stop() { 
     moving = false; 
    } 
} 

这是正确的面板,它是时钟。

public class MoveClock { 
     private GUIFrame gui; 
     private boolean clock = true; 
     private volatile boolean running = true; 

     public MoveClock(GUIFrame gui) { 
      this.gui = gui; 
     } 

     public void start() { 
      clock = true; 
      Random rand = new Random(); 
      while (clock) { 

       Calendar cal = Calendar.getInstance(); 
        int hour = cal.get(Calendar.HOUR_OF_DAY); 
        int minute = cal.get(Calendar.MINUTE); 
        int second = cal.get(Calendar.SECOND); 
       gui.Klockan(hour, minute, second); 
        int a = rand.nextInt(100) + 1; 
        int b = rand.nextInt(100) + 1; 

        java.awt.EventQueue.invokeLater(new Runnable() { //Utan detta så kör inte Klockan random placeringar 
         public void run() { 

          gui.moveClock(a, b, 150, 150); 
         } 
        }); 


       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

     public void stop() { 
      clock = false; 
     } 
    } 

而这正是所有的帧,Jpanels等类:

private JLabel movingDisplay; 
    private JLabel movingClock; 
    private MP3Player mp3_player; 
    private boolean playing = true; 
    private boolean moving = true; 
    private boolean clocking = true; 
// private Thread t1; 
    JDialog playingDialog; 
    private MoveDisplay moveDisplay; 
    private MoveClock moveClock; 

    Clip clip; 

    /** 
    * Starts the application 
    */ 
    public void Start() { 
     frame = new JFrame(); 
     frame.setBounds(0, 0, 494, 437); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //frame.setLayout(null); 
     frame.setTitle("Multiple Thread Demonstrator"); 
     InitializeGUI(); // Fill in components 
     frame.setVisible(true); 
     frame.setResizable(false); // Prevent user from change size 
     frame.setLocationRelativeTo(null); // Start middle screen 
     moveDisplay = new MoveDisplay(this); 
     moveClock = new MoveClock(this); 
    } 

JPanel pnlDisplay = new JPanel(); 
     Border b2 = BorderFactory.createTitledBorder("Display Thread"); 
     pnlDisplay.setBorder(b2); 
     pnlDisplay.setBounds(12, 118, 222, 269); 
     pnlDisplay.setLayout(null); 

     // Add buttons and drawing panel to this panel 
     btnDisplay = new JButton("Start Display"); 
     btnDisplay.setBounds(10, 226, 121, 23); 
     pnlDisplay.add(btnDisplay); 

     btnDStop = new JButton("Stop"); 
     btnDStop.setBounds(135, 226, 75, 23); 
     pnlDisplay.add(btnDStop); 

     pnlMove = new JPanel(); 
     pnlMove.setBounds(10, 19, 200, 200); 
     Border b21 = BorderFactory.createLineBorder(Color.black); 
     pnlMove.setBorder(b21); 
     pnlDisplay.add(pnlMove); 
     // Then add this to main window 
     frame.add(pnlDisplay); 

     // The moving graphics outer panel 
     JPanel pnlTriangle = new JPanel(); 
     Border b3 = BorderFactory.createTitledBorder("Triangle Thread"); 
     pnlTriangle.setBorder(b3); 
     pnlTriangle.setBounds(240, 118, 222, 269); 
     pnlTriangle.setLayout(null); 

     // Add buttons and drawing panel to this panel 
     btnTriangle = new JButton("Start Rotate"); 
     btnTriangle.setBounds(10, 226, 121, 23); 
     pnlTriangle.add(btnTriangle); 

     btnTStop = new JButton("Stop"); 
     btnTStop.setBounds(135, 226, 75, 23); 
     pnlTriangle.add(btnTStop); 

     pnlRotate = new JPanel(); 
     pnlRotate.setBounds(10, 19, 200, 200); 
     Border b31 = BorderFactory.createLineBorder(Color.black); 
     pnlRotate.setBorder(b31); 
     pnlTriangle.add(pnlRotate); 
     // Add this to main window 
     frame.add(pnlTriangle); 



     movingDisplay = new JLabel("DisplayThread"); 
     pnlMove.add(movingDisplay); 
     btnDStop.setEnabled(false); 


     movingClock = new JLabel("TriangleThread"); 
     pnlRotate.add(movingClock); 
     btnTStop.setEnabled(false); 


     btnDisplay.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       moving = true; 
       btnDisplay.setEnabled(false); 
       btnDStop.setEnabled(true); 
       startMoveDisplay(); 
      } 
     }); 

     btnDStop.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       moving = false; 
       btnDisplay.setEnabled(true); 
       btnDStop.setEnabled(false); 
       startMoveDisplay(); 
      } 
     }); 

     btnTriangle.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       clocking = true; 
       btnTriangle.setEnabled(false); 
       btnTStop.setEnabled(true); 
       startMoveClock(); 
      } 
     }); 


    btnTStop.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      clocking = false; 
      btnTriangle.setEnabled(true); 
      btnTStop.setEnabled(false); 
      startMoveClock(); 
     } 
    }); 
} 

    public void startMoveDisplay() { 
     if(moving) { 
      new Thread(new Runnable() { 
       public void run() { 
        moveDisplay.start(); 
       } 
      }).start(); 
     } else { 
      moveDisplay.stop(); 
     } 
    } 

    public void startMoveClock() { 
     if(clocking) { 
      new Thread(new Runnable() { 
       public void run() { 
        moveClock.start(); 
       } 
      }).start(); 
     } else { 
      moveClock.stop(); 
     } 
    } 

    public void moveDisplay(int x, int y, int width, int height) { 
     movingDisplay.setBounds(x, y, width, height); 
    } 

    public void moveClock(int a, int b, int width, int height) { 
     movingClock.setBounds(a, b, width, height); 
    } 

    public void Klockan(int hour, int minute, int second) { 
     movingClock.setText(hour + ":" + minute + ":" + second); 
    } 
+1

我看到你的代码在你的新问题工作正常:http://stackoverflow.com/questions/33710527/cant-set-image-in-jlabel-from-anywhere-except-jframe/33710599# 33710599。不要忘了单击复选标记以“接受”答案,以便人们知道问题已解决。或者,如果您使用了不同的解决方案,请再次发布自己的答案,以便人们知道问题已得到解决。请考虑使用论坛的人员,以便我们不花时间回答已有解决方案的问题。 – camickr

+0

@camickr我没有注意到我在这里回答。有了这个,我得到了我的问题排序,并得到了一切工作:)谢谢! – WeInThis

+0

你为什么要删除你的其他问题?答案没有帮助解决问题吗? – camickr

回答

2

它出现在它的初始位置第一(盒的顶部)移动到它之前的随机位置每它应该移动。

那么,您的整个程序使用空布局(这是不正确的,因为Swing旨在与布局管理器一起使用),除了添加随机标签的面板。

movingDisplay = new JLabel("DisplayThread"); 
    pnlMove.add(movingDisplay); 

这就是我不明白,你用pnlMove.setLayout(null)

所以我猜测布局管理器暂时被调用,并且标签在面板的开始处被绘制,然后它被移动到其随机位置。

所以我的建议是:

  1. 不要构建使用空布局(除了与随机位置的面板)的图形用户界面。 Swing旨在与布局经理一起使用。

  2. 不要使用Thread和Thread.sleep()。对于动画使用摆动计时器。当Timer触发时,代码在EDT上执行,并且不需要SwingUtilities.invokeLater()。

  3. 所有你想要的代码是改变标签的位置,所以只需使用setLocation(...)方法,而不是setBounds()方法。

  4. 方法名称不应以大写字母开头。你的大部分名字都是正确的。始终如一!请注意论坛如何突出显示方法名称,比如它们是类名称?