控件显示问题 我在一个窗体中加入了两个JPanel面板,第一个里面有一个按钮,第二个为空. 当点击按钮后在按钮的actionPerformed函数中为第二个面板添加了两个标签,它们是重叠的,然后再进行碰撞检测让它们自动分开.现在我遇到的问题是运行时当点过按钮后标签并不立即显示,而只显示分开后的结果,请高手指教缘由! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JPanel 好象是flowLayout 布局的,把setLayout(null),试试 设置了,不行的。我把代码帖上来大家帮我看看~~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;elsereturn 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(); }} } }}为什么点击后不显示初始的重叠状态和检测过程中自动分开的过程? 你将 p.repaint(); 换成 p.updateUI();试试看! 好了,我用update(getGraphics());重绘了各个窗体后问题得到了解决.谢谢两位的帮助! import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.BorderFactory;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.border.BevelBorder;import javax.swing.border.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(); new Thread() { public void run() { 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(); } } }.start(); } } }} Thread.currentThread().sleep(50);这个得到当前线程 然后sleep你当然看不到移开的效果 线程都被你阻塞了 谢谢insiku,线程我不是很懂,经你这么讲解我觉得我上边的解决方法并不算是真正的答案。多谢! javolution.util.FastList 用过的请进 JTree中对结点编辑后如何能获得这个结点编辑后的名字? 问下,校内网里的游戏,是用什么语言开发的? jbuilder 2007安装之前是不是需要安装JDK的? 三层c/s和三层b/s的优越性比较? java 中的e符号 关于正则表达式的一个简单问题! 这个是什么错误? java 中a=a++ 的问题 怎样在APPLET中发EMAIL? SQL 语句的问题 大家快快帮忙啊
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.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.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(); new Thread()
{
public void run()
{
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();
}
}
}.start(); }
}
}
}
你当然看不到移开的效果 线程都被你阻塞了