There's very little information to go on and so many reasons why you might be having problems.
Below is a simple example of how I might solve your problems. Note- I, personally, don't like null
layouts, they mess around to much with how the internals of Swing works and tend to produce more problems then they solve. Instead, when I want to do away with layout managers, I use a JLayeredPane
instead.
public class BouncingLabel {
public static void main(String[] args) {
new BouncingLabel();
}
public BouncingLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JLayeredPane {
private JLabel label;
private Point p;
private int dv = 2;
public TestPane() {
p = new Point();
label = new JLabel("Look Ma, no hands!");
add(label);
Timer timer = new Timer(60, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
p.y = (getHeight() - label.getHeight()) / 2;
p.x = p.x + dv;
if (p.x + label.getWidth() > getWidth()) {
dv *= -1;
p.x = getWidth() - label.getWidth() + dv;
} else if (p.x < 0) {
dv *= -1;
p.x = 0 + dv;
}
label.setLocation(p);
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
@Override
public void doLayout() {
super.doLayout();
label.setSize(label.getPreferredSize());
label.getLocation(p);
}
}
}
Updated
On close inspection of the code, you appear to be violating the Event Dispatching Threads - no blocking rule. All UI updates are maintained within a single thread (the EDT), any action which blocks this thread, will prevent the EDT from dispatching events to the all the components, including repaint requests.
Doing something like this..
for (int i=x1;i<x2;i++)
{
label.setbound(i,y1+(x-i)*m,label.getwidth(),label.gethigh());
}
Means that you've stopped the EDT from dispatching any required update events to the rest of the UI, preventing it from updating the screen, until you exit the method.
Take a look at Concurrency in Swing for more details.
I would also take a close look at How to use Swing Timers and Swing Worker for some solutions.