User interface 如何使用计时器移动JButton
所以我想让一个按钮通过x,y坐标绝对定位移动,我有一个白色的块移动,没有图像,不能点击 图像确实与使用图像的绘制方法一起工作,但我想使用一个按钮User interface 如何使用计时器移动JButton,user-interface,animation,button,timer,jbutton,User Interface,Animation,Button,Timer,Jbutton,所以我想让一个按钮通过x,y坐标绝对定位移动,我有一个白色的块移动,没有图像,不能点击 图像确实与使用图像的绘制方法一起工作,但我想使用一个按钮 //******************************************************************** // ReboundPanel.java Java Foundations // // Represents the primary panel for the Rebound program. //******
//********************************************************************
// ReboundPanel.java Java Foundations
//
// Represents the primary panel for the Rebound program.
//********************************************************************
package ch0;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ReboundPanel extends JPanel
{
private final int WIDTH = 300, HEIGHT = 100;
private final int DELAY = 20, IMAGE_SIZE = 35;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY;
JButton button;
//-----------------------------------------------------------------
// Sets up the panel, including the timer for the animation.
//-----------------------------------------------------------------
public ReboundPanel()
{
this.setLayout(null); //Worked before I put this stuff but
super.setLayout(null);// I'm just trying stuff
timer = new Timer(DELAY, new ReboundListener());
image = new ImageIcon("smile.jpg");
button = new JButton(image);
button.setIcon(new ImageIcon("smile.jpg"));
x = 0;
y = 40;
moveX = moveY = 3;
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.black);
timer.start();
//button.setBounds(x, y, 10, 10);
//add(button);
}
//*****************************************************************
// Represents the action listener for the timer.
//*****************************************************************
private class ReboundListener implements ActionListener
{
//-----------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
//-----------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
x += moveX;
y += moveY;
if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
moveX = moveX * -1;
if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
moveY = moveY * -1;
button.setBounds(x, y, 100, 100);
add(button);
}
}
}
//********************************************************************
// Rebound.java Java Foundations
//
// Demonstrates an animation and the use of the Timer class.
//********************************************************************
package ch0;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rebound
{
//-----------------------------------------------------------------
// Displays the main frame of the program.
//-----------------------------------------------------------------
public static void main(String[] args)
{
JFrame frame = new JFrame("Rebound");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ReboundPanel());
frame.pack();
frame.setVisible(true);
}
}
//********************************************************************
//java.java基础
//
//表示回弹程序的主面板。
//********************************************************************
包装ch0;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类面板扩展JPanel
{
专用最终整型宽度=300,高度=100;
私有最终整数延迟=20,图像大小=35;
私有图像图标图像;
私人定时器;
私有整数x,y,moveX,moveY;
按钮;
//-----------------------------------------------------------------
//设置面板,包括动画的计时器。
//-----------------------------------------------------------------
公共事务委员会()
{
this.setLayout(null);//在我放置这些东西之前工作过,但是
super.setLayout(null);//我只是在尝试一些东西
计时器=新计时器(延迟,新侦听器());
图像=新图像图标(“smile.jpg”);
按钮=新的JButton(图像);
按钮。设置图标(新的图像图标(“smile.jpg”);
x=0;
y=40;
moveX=moveY=3;
setPreferredSize(新尺寸(宽度、高度));
挫折背景(颜色:黑色);
timer.start();
//按钮.立根(x,y,10,10);
//添加(按钮);
}
//*****************************************************************
//表示计时器的操作侦听器。
//*****************************************************************
私有类侦听器实现ActionListener
{
//-----------------------------------------------------------------
//更新图像的位置,可能还更新方向
//每当计时器触发动作事件时的移动次数。
//-----------------------------------------------------------------
已执行的公共无效操作(操作事件)
{
x+=moveX;
y+=moveY;
如果(x=宽度图像大小)
moveX=moveX*-1;
if(y=高度-图像大小)
moveY=moveY*-1;
按钮.立根(x,y,100,100);
添加(按钮);
}
}
}
//********************************************************************
//java.java基础
//
//演示动画和计时器类的使用。
//********************************************************************
包装ch0;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共阶层反弹
{
//-----------------------------------------------------------------
//显示程序的主框架。
//-----------------------------------------------------------------
公共静态void main(字符串[]args)
{
JFrame=新JFrame(“回弹”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(新面板());
frame.pack();
frame.setVisible(true);
}
}
显然图像太大了显然图像太大了