我在一个窗体中加入了两个JPanel面板,第一个里面有一个按钮,第二个为空. 当点击按钮后在按钮的actionPerformed函数中为第二个面板添加了两个标签,它们是重叠的,然后再进行碰撞检测让它们自动分开.现在我遇到的问题是运行时当点过按钮后标签并不立即显示,而只显示分开后的结果,为什么点击后不显示初始的重叠状态和检测过程中自动分开的过程?
代码:
import
javax.swing.
*
;
import java.awt. * ;
import java.awt.event. * ;
import javax.swing.BorderFactory;
import javax.swing.border. * ;
public class test
... {
public static void main(String[] args)
...{
JFrame.setDefaultLookAndFeelDecorated(true);
frame f = new frame();
}
}
class frame extends JFrame
... {
JButton b1,b2,b4,b5,b6;
JLabel lb,lbs;
JPanel m,p;
ImageIcon icon;
Container c;
public frame()...{
super("test");
setBounds(100,100,500,500);
c=getContentPane();
Border border=BorderFactory.createBevelBorder(BevelBorder.RAISED,new Color(122,138,153),Color.DARK_GRAY);
m=new JPanel();
m.setPreferredSize(new Dimension(500,30));
m.setBorder(border);
m.setLayout(null);
m.setBackground(new Color(100,115,130));
m.setOpaque(true);
b4=new JButton("go");
b4.setBounds(253,3,80,25);
hds hd =new hds();
b4.addActionListener(hd);
m.add(b4);
p=new JPanel();
p.setLayout(null);
p.setBackground(Color.white);
c.add(m,BorderLayout.NORTH);
c.add(p,BorderLayout.CENTER);
show();
}
public static final boolean isIn(int ax, int ay, int aw,int ah, int bx, int by, int bw, int bh) ...{
if (by + bh < ay ||
by > ay + ah ||
bx + bw < ax ||
bx > ax + aw)
return false;
else
return true;
}
private class hds implements ActionListener
...{
public void actionPerformed(ActionEvent ee)
...{
if(ee.getSource()==b4)...{
lb=new JLabel("hello");
lb.setSize(50,30);
lb.setLocation(50,50);
lb.setBorder(BorderFactory.createLineBorder(Color.black));
lb.setBackground(Color.gray);
lb.setOpaque(true);
lbs=new JLabel("sdfsfsf");
lbs.setSize(70,30);
lbs.setLocation(70,70);
lbs.setBorder(BorderFactory.createLineBorder(Color.black));
lbs.setBackground(Color.blue);
lbs.setOpaque(true);
p.add(lb);
p.add(lbs);
p.repaint();
try...{
Thread.currentThread().sleep(50);
}catch(InterruptedException e)...{
}
while(isIn(lb.getX(),lb.getY(),lb.getWidth(),lb.getHeight(),lbs.getX(),lbs.getY(),lbs.getWidth(),lbs.getHeight()))
...{
lb.setLocation(lb.getX()-1,lb.getY());
try...{
Thread.currentThread().sleep(50);
}catch(InterruptedException e)...{
}
p.repaint();
}
}
}
}
}
import java.awt. * ;
import java.awt.event. * ;
import javax.swing.BorderFactory;
import javax.swing.border. * ;
public class test
... {
public static void main(String[] args)
...{
JFrame.setDefaultLookAndFeelDecorated(true);
frame f = new frame();
}
}
class frame extends JFrame
... {
JButton b1,b2,b4,b5,b6;
JLabel lb,lbs;
JPanel m,p;
ImageIcon icon;
Container c;
public frame()...{
super("test");
setBounds(100,100,500,500);
c=getContentPane();
Border border=BorderFactory.createBevelBorder(BevelBorder.RAISED,new Color(122,138,153),Color.DARK_GRAY);
m=new JPanel();
m.setPreferredSize(new Dimension(500,30));
m.setBorder(border);
m.setLayout(null);
m.setBackground(new Color(100,115,130));
m.setOpaque(true);
b4=new JButton("go");
b4.setBounds(253,3,80,25);
hds hd =new hds();
b4.addActionListener(hd);
m.add(b4);
p=new JPanel();
p.setLayout(null);
p.setBackground(Color.white);
c.add(m,BorderLayout.NORTH);
c.add(p,BorderLayout.CENTER);
show();
}
public static final boolean isIn(int ax, int ay, int aw,int ah, int bx, int by, int bw, int bh) ...{
if (by + bh < ay ||
by > ay + ah ||
bx + bw < ax ||
bx > ax + aw)
return false;
else
return true;
}
private class hds implements ActionListener
...{
public void actionPerformed(ActionEvent ee)
...{
if(ee.getSource()==b4)...{
lb=new JLabel("hello");
lb.setSize(50,30);
lb.setLocation(50,50);
lb.setBorder(BorderFactory.createLineBorder(Color.black));
lb.setBackground(Color.gray);
lb.setOpaque(true);
lbs=new JLabel("sdfsfsf");
lbs.setSize(70,30);
lbs.setLocation(70,70);
lbs.setBorder(BorderFactory.createLineBorder(Color.black));
lbs.setBackground(Color.blue);
lbs.setOpaque(true);
p.add(lb);
p.add(lbs);
p.repaint();
try...{
Thread.currentThread().sleep(50);
}catch(InterruptedException e)...{
}
while(isIn(lb.getX(),lb.getY(),lb.getWidth(),lb.getHeight(),lbs.getX(),lbs.getY(),lbs.getWidth(),lbs.getHeight()))
...{
lb.setLocation(lb.getX()-1,lb.getY());
try...{
Thread.currentThread().sleep(50);
}catch(InterruptedException e)...{
}
p.repaint();
}
}
}
}
}
我已在此提问:http://community.csdn.net/Expert/topic/5460/5460280.xml?temp=.599209
但仍找不出原因,后来我用update(getGraphics());重绘了各个窗体后问题得到了解决.
while
(isIn(lb.getX(),lb.getY(),lb.getWidth(),lb.getHeight(),lbs.getX(),lbs.getY(),lbs.getWidth(),lbs.getHeight()))
... {
lb.setLocation(lb.getX()-1,lb.getY());
try...{
Thread.currentThread().sleep(10);
}catch(InterruptedException e)...{
}
update(getGraphics());
}
... {
lb.setLocation(lb.getX()-1,lb.getY());
try...{
Thread.currentThread().sleep(10);
}catch(InterruptedException e)...{
}
update(getGraphics());
}