java Application 中 JBUTTON 为啥不能慢慢移动

joan933 2003-11-05 08:53:32
java Application 中 JBUTTON 为啥不能慢慢移动?
能帮我看看吗?已经三星期了,还是不知如何做。



package image_test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;

public class Frame2 extends JFrame {
JPanel contentPane;

BorderLayout borderLayout1 = new BorderLayout();
JButton jButton1 = new JButton();

//Construct the frame
public Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jButton1.setBounds(new Rectangle(162, 65, 192, 66));

contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");

jButton1.setText("jButton1");
contentPane.addMouseListener(new Frame2_contentPane_mouseAdapter(this));
contentPane.add(jButton1, null);
}
//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void contentPane_mouseClicked(MouseEvent e) {

for(int i=55; i>2; i=i-5){
try{
Thread.sleep(2000);
System.out.println(i);
this.getContentPane(). setLayout(null);
// jButton1.setBounds(new Rectangle(80, 69, 206, 53));
jButton1.setLocation(i,115);
// jButton1.repaint();
this.getContentPane().add(jButton1, null);
contentPane.repaint();
//this.repaint();


}catch(InterruptedException exception) {
System.err.println(exception.toString());
}
}//end for

}//// end contentPanel mouse_clicked


}

class Frame2_contentPane_mouseAdapter extends java.awt.event.MouseAdapter {
Frame2 adaptee;

Frame2_contentPane_mouseAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.contentPane_mouseClicked(e);
}
}


...全文
17 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
alieneagle 2003-11-07
  • 打赏
  • 举报
回复
我试了试,是线程的问题,线程睡眠一次是可以的,多次就不行了,改为Timer后就可以了,代码如下:

package pa1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;

public class Frame2
extends JFrame
{
JPanel contentPane;
Timer t = null;
int x = 0;

BorderLayout borderLayout1 = new BorderLayout();
JButton jButton1 = new JButton();

//Construct the frame
public Frame2()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
Frame2 frame21 = new Frame2();
frame21.setVisible(true);
frame21.setBounds(0, 0, 500, 400);
}

//Component initialization
private void jbInit() throws Exception
{
contentPane = (JPanel)this.getContentPane();
jButton1.setBounds(new Rectangle(0, 0, 192, 66));

contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");

jButton1.setText("jButton1");
contentPane.addMouseListener(new Frame2_contentPane_mouseAdapter(this));
contentPane.add(jButton1, null);
}
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}

void contentPane_mouseClicked(MouseEvent e)
{


try
{
//Thread.sleep(1000);
t = new Timer(1420, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
x++;
//t.stop();
jButton1.setLocation(x, 115);
}
});
t.start();
validate();
repaint();

}
catch (Exception exception)
{
System.err.println(exception.toString());
}
}

}

class Frame2_contentPane_mouseAdapter
extends java.awt.event.MouseAdapter
{
Frame2 adaptee;

Frame2_contentPane_mouseAdapter(Frame2 adaptee)
{
this.adaptee = adaptee;
}

public void mouseClicked(MouseEvent e)
{
adaptee.contentPane_mouseClicked(e);
}
}
alieneagle 2003-11-07
  • 打赏
  • 举报
回复
是要做控件拖动吗

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧