怎样实现将一个按钮或标签控件拖动??? 重设控件的坐标即可.见 java.awt.Component.setX(); setY(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 呵呵,这个问题有意思,包含了java布局和事件内容。下面这个程序或许对你找到其他最佳答案有所帮助,不过我认为最好是进行控件重画,你运行的时候会看到这个程序的缺陷,奈何现在我还没有足够的知识来提出更好的方案,希望各位兄弟都来添把火:/*class FinalCount{ private int y=1; private int maxValue=10; private int minValue=1; //private boolean ONE_TIME_FLAG=false;//tricky here private boolean releaseIncThreadFlag=false; private boolean releaseDecThreadFlag=false; private boolean arriveMax=false; //private boolean toMin=true;//the startup status public FinalCount(){ } public FinalCount(int max,int min){ maxValue=max; minValue=min; y=min; } public synchronized void increase(){ //if(arriveMax) // return;//do nothing printY(); y++; if(y==maxValue){ //ONE_TIME_FLAG=true;//tricky here releaseIncThreadFlag=true; arriveMax=true; printY();//tricky here notifyAll();//notify the decrease thread... } } public synchronized void decrease(){ //if(!arriveMax) // return;//do nothing y--; printY(); if(y==minValue){ releaseDecThreadFlag=true; arriveMax=false; notifyAll();//notify all the increase thread... } } private void printY(){ System.out.println("thread name=" + Thread.currentThread().getName()); System.out.println(y); } public synchronized boolean isArriveMaxValue(){ return arriveMax; } //public synchronized boolean readOneTimeFlag(){ // return ONE_TIME_FLAG; //} public synchronized boolean isReleaseIncThread(){ return releaseIncThreadFlag; } public synchronized boolean isReleaseDecThread(){ return releaseDecThreadFlag; } public int getMaxValue(){ return maxValue; } }class IncThread extends Thread{ FinalCount count; public IncThread(String name,FinalCount count){ super(name); this.count=count; } public void run(){ /*synchronized(count){ while(count.isArriveMaxValue()){ try{ count.wait(); }catch(Exception x){ } } }*/ /*for(int i=0;i<count.getMaxValue();i++){ synchronized(count){ while(count.isArriveMaxValue() & !count.isReleaseIncThread()){ try{ count.wait(); }catch(Exception x){ } } } //if(count.readOneTimeFlag()){ //break; //} if(count.isReleaseIncThread()) break; count.increase(); } //release prompt: System.out.println("====== Thread(" + getName() + ") is released...."); }}class DecThread extends Thread{ FinalCount count; public DecThread(String name,FinalCount count){ super(name); this.count=count; } public void run(){ /*synchronized(count){ while(!count.isArriveMaxValue()){ try{ count.wait(); }catch(Exception x){ } } }*/ /*for(int i=0;i<count.getMaxValue();i++){ synchronized(count){ while(!count.isArriveMaxValue() & !count.isReleaseDecThread()){ try{ count.wait(); }catch(Exception x){ } } } if(count.isReleaseDecThread()) break; count.decrease(); //if(count.isReleaseDecThread()) //break; } System.out.println("====== Thread(" + getName() + ") is released...."); }}public class Test { public static void main(String[] args){ //System.out.println(FinalCount.y); FinalCount fc=new FinalCount(); IncThread incThread1=new IncThread("incThread1",fc); IncThread incThread2=new IncThread("incThread2",fc); DecThread decThread1=new DecThread("decThread1",fc); DecThread decThread2=new DecThread("decThread2",fc); decThread1.start(); decThread2.start(); incThread2.start(); incThread1.start(); //decThread1.start(); //decThread2.start(); }*//** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author unascribed * @version 1.0 *//*class FinalCount{ public static int y=1; public static synchronized void increase(){ y++; } public static synchronized void decrease(){ y--; }}class Thread1 extends Thread{ public void run(){ try{ System.out.println("Sorry, I am blocked by unknown reason..."); sleep(50); }catch(Exception x){ } for(int i=0;i<5;i++){ FinalCount.increase(); System.out.println(FinalCount.y); } }}class Thread2 extends Thread{ public void run(){ for(int i=0;i<5;i++){ FinalCount.decrease(); System.out.println(FinalCount.y); } }}public class Test { public static void main(String[] args){ System.out.println(FinalCount.y); Thread1 thread1a=new Thread1(); Thread1 thread1b=new Thread1(); Thread2 thread2a=new Thread2(); Thread2 thread2b=new Thread2(); //Thread1 thread1c=new Thread1(); thread2a.start(); thread1a.start(); thread1b.start(); thread2b.start(); }}*/import javax.swing.*;import java.awt.event.*;import java.awt.*;public class Test extends JFrame{ JButton b1; //JButton b2; //JButton b3; private double offsetX; private double offsetY; public Test(){ Container contentPane = getContentPane(); contentPane.setLayout(null); b1 = new JButton("one"); contentPane.add(b1); Insets insets = contentPane.getInsets(); b1.setBounds(25 + insets.left, 5 + insets.top, 75, 20); b1.addMouseListener( new MouseAdapter(){ public void mousePressed(MouseEvent e){ Point p1=b1.getLocation(); Point p2=e.getPoint(); offsetX=p2.getX()-p1.getX(); offsetY=p2.getY()-p1.getY(); } } ); b1.addMouseMotionListener( new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ Point p=e.getPoint(); p.setLocation(p.getX()-offsetX,p.getY()-offsetY); b1.setLocation(p); } } ); } public static void main(String[] args){ Test t=new Test(); t.setSize(200,200); t.setVisible(true); } } sorry,粘贴了一大堆无用的代码,呵呵,不过仍可以运行无误。 用glassPane,swing中的可拖动层,位于所有组件之上,你可以去查api Java调用批处理bat文件产生的乱码问题 java继承问题 打jar包后找不到第三方jar包 请NB的人帮忙说清楚为什么. 工作郁闷... 紧急求助,sql server中有没有像oracle 中 rownum 这样的东西 请问jbuilder8是怎样注册的啊? 请问jList组件怎么才能加有滚动条? 请问‘回车’在java中如何表示?如何替换? JAVA用来做B/S数据库好吗? 求JBuilder8使用方法说明 一个关于改变JTree中结点的颜色问题
private int y=1;
private int maxValue=10;
private int minValue=1;
//private boolean ONE_TIME_FLAG=false;//tricky here
private boolean releaseIncThreadFlag=false;
private boolean releaseDecThreadFlag=false;
private boolean arriveMax=false;
//private boolean toMin=true;//the startup status
public FinalCount(){
}
public FinalCount(int max,int min){
maxValue=max;
minValue=min;
y=min;
} public synchronized void increase(){
//if(arriveMax)
// return;//do nothing
printY();
y++;
if(y==maxValue){
//ONE_TIME_FLAG=true;//tricky here
releaseIncThreadFlag=true;
arriveMax=true;
printY();//tricky here
notifyAll();//notify the decrease thread...
}
}
public synchronized void decrease(){
//if(!arriveMax)
// return;//do nothing
y--;
printY();
if(y==minValue){
releaseDecThreadFlag=true;
arriveMax=false;
notifyAll();//notify all the increase thread...
}
}
private void printY(){
System.out.println("thread name=" + Thread.currentThread().getName());
System.out.println(y);
}
public synchronized boolean isArriveMaxValue(){
return arriveMax;
}
//public synchronized boolean readOneTimeFlag(){
// return ONE_TIME_FLAG;
//}
public synchronized boolean isReleaseIncThread(){
return releaseIncThreadFlag;
}
public synchronized boolean isReleaseDecThread(){
return releaseDecThreadFlag;
}
public int getMaxValue(){
return maxValue;
}
}
class IncThread extends Thread{
FinalCount count;
public IncThread(String name,FinalCount count){
super(name);
this.count=count;
}
public void run(){
/*synchronized(count){
while(count.isArriveMaxValue()){
try{
count.wait();
}catch(Exception x){
}
}
}*/
/*for(int i=0;i<count.getMaxValue();i++){
synchronized(count){
while(count.isArriveMaxValue() & !count.isReleaseIncThread()){
try{
count.wait();
}catch(Exception x){
}
}
}
//if(count.readOneTimeFlag()){
//break;
//}
if(count.isReleaseIncThread())
break;
count.increase();
}
//release prompt:
System.out.println("====== Thread(" + getName() + ") is released....");
}
}
class DecThread extends Thread{
FinalCount count;
public DecThread(String name,FinalCount count){
super(name);
this.count=count;
}
public void run(){
/*synchronized(count){
while(!count.isArriveMaxValue()){
try{
count.wait();
}catch(Exception x){
}
}
}*/
/*for(int i=0;i<count.getMaxValue();i++){
synchronized(count){
while(!count.isArriveMaxValue() & !count.isReleaseDecThread()){
try{
count.wait();
}catch(Exception x){
}
}
}
if(count.isReleaseDecThread())
break;
count.decrease();
//if(count.isReleaseDecThread())
//break;
}
System.out.println("====== Thread(" + getName() + ") is released....");
}
}
public class Test {
public static void main(String[] args){
//System.out.println(FinalCount.y);
FinalCount fc=new FinalCount();
IncThread incThread1=new IncThread("incThread1",fc);
IncThread incThread2=new IncThread("incThread2",fc);
DecThread decThread1=new DecThread("decThread1",fc);
DecThread decThread2=new DecThread("decThread2",fc);
decThread1.start();
decThread2.start();
incThread2.start();
incThread1.start();
//decThread1.start();
//decThread2.start();
}
*//**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
/*class FinalCount{
public static int y=1;
public static synchronized void increase(){
y++;
}
public static synchronized void decrease(){
y--;
}
}
class Thread1 extends Thread{
public void run(){
try{
System.out.println("Sorry, I am blocked by unknown reason...");
sleep(50);
}catch(Exception x){
}
for(int i=0;i<5;i++){
FinalCount.increase();
System.out.println(FinalCount.y);
}
}
}
class Thread2 extends Thread{
public void run(){
for(int i=0;i<5;i++){
FinalCount.decrease();
System.out.println(FinalCount.y);
}
}
}
public class Test {
public static void main(String[] args){
System.out.println(FinalCount.y);
Thread1 thread1a=new Thread1();
Thread1 thread1b=new Thread1();
Thread2 thread2a=new Thread2();
Thread2 thread2b=new Thread2();
//Thread1 thread1c=new Thread1();
thread2a.start();
thread1a.start();
thread1b.start();
thread2b.start();
}}*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JFrame{
JButton b1;
//JButton b2;
//JButton b3;
private double offsetX;
private double offsetY; public Test(){
Container contentPane = getContentPane();
contentPane.setLayout(null); b1 = new JButton("one");
contentPane.add(b1);
Insets insets = contentPane.getInsets();
b1.setBounds(25 + insets.left, 5 + insets.top, 75, 20);
b1.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e){
Point p1=b1.getLocation();
Point p2=e.getPoint();
offsetX=p2.getX()-p1.getX();
offsetY=p2.getY()-p1.getY();
}
}
);
b1.addMouseMotionListener(
new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
Point p=e.getPoint();
p.setLocation(p.getX()-offsetX,p.getY()-offsetY);
b1.setLocation(p);
}
}
);
}
public static void main(String[] args){
Test t=new Test();
t.setSize(200,200);
t.setVisible(true);
}
}