如何用JAVA实现聊天窗口的抖动效果,最好是具体代码
展开全部
public class ShakeFrame extends JFrame {
private JButton btn = new JButton("Click me!");
public ShakeFrame() {
super("抖动窗口");
this.setSize(300, 200);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
btn.setBounds(10, 10, 100, 30);
this.add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x = ShakeFrame.this.getX();
int y = ShakeFrame.this.getY();
for (int i = 0; i < 20; i++) {
if ((i & 1) == 0) {
x += 3;
y += 3;
} else {
x -= 3;
y -= 3;
}
ShakeFrame.this.setLocation(x, y);
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
});
}
public static void main(String[] args) {
new ShakeFrame();
}
}
private JButton btn = new JButton("Click me!");
public ShakeFrame() {
super("抖动窗口");
this.setSize(300, 200);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
btn.setBounds(10, 10, 100, 30);
this.add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x = ShakeFrame.this.getX();
int y = ShakeFrame.this.getY();
for (int i = 0; i < 20; i++) {
if ((i & 1) == 0) {
x += 3;
y += 3;
} else {
x -= 3;
y -= 3;
}
ShakeFrame.this.setLocation(x, y);
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
});
}
public static void main(String[] args) {
new ShakeFrame();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |