Java Swing动画
我要做一个关于电梯管理系统的Java项目 我的问题是在2杰帕内尔的。第一个是JLabel和Button,每层一个-我使用布局,因为我希望它能够响应。在程序开始之前,我有一个对话框,可以设置第一层和最后一层的标高。以下是代码:Java Swing动画,java,swing,animation,Java,Swing,Animation,我要做一个关于电梯管理系统的Java项目 我的问题是在2杰帕内尔的。第一个是JLabel和Button,每层一个-我使用布局,因为我希望它能够响应。在程序开始之前,我有一个对话框,可以设置第一层和最后一层的标高。以下是代码: public void setFloors() { floorNavigation = new JPanel(); floorNavigation.setLayout(new BoxLayout(floorNavigation, BoxLayout.Y_AX
public void setFloors() {
floorNavigation = new JPanel();
floorNavigation.setLayout(new BoxLayout(floorNavigation, BoxLayout.Y_AXIS));
floorNavigation.add(Box.createVerticalGlue());
for(JPanel p : floorPanels){
floorNavigation.add(p);
}
floorNavigation.add(Box.createVerticalGlue());
frame.add(floorNavigation, BorderLayout.LINE_START);
}
public void createFloorPanel() {
JPanel floorPanel;
for(JLabel l : floorLabels){
floorPanel = new JPanel(new FlowLayout());
floorPanel.add(l);
floorPanel.add(floorButtons.get(floorLabels.indexOf(l)));
floorPanels.add(floorPanel);
}
}
我想制作一个电梯移动动画到另一个JPanel中每个JLabel的Y。有什么办法吗?像盖蒂这样的事情是行不通的 如果我理解正确,包含按钮的面板和包含动画的面板将并排放置,从而具有相同的高度。在这种情况下,只需知道面板的当前高度和按钮数量,即可计算Y坐标位置。只需将高度除以按钮数,再乘以你想要到达的楼层数。注意,这将按相反的顺序,自上而下计算楼层数 计算完这些位置后,您可以使用简单的线性动画将电梯移动到按钮的相应位置,我猜是导入的图标或图像。下面是一个简单的动画模板,小圆圈代表您的电梯图像:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleAnimation {
static int x;
static int y = 150;
static int floor_distance = 130;
static int animation_speed = 10; //less is faster
static MyDrawPanel panel = new MyDrawPanel();
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton buttonUp = new JButton("Floor Up");
final JButton buttonDown = new JButton("Floor Down");
buttonUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
(new AnimatorUp()).execute();
}
});
buttonDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
(new AnimatorDown()).execute();
}
});
frame.getContentPane().add(BorderLayout.NORTH, buttonUp);
frame.getContentPane().add(BorderLayout.SOUTH, buttonDown);
frame.getContentPane().add(panel);
frame.setSize(300,300);
frame.setVisible(true);
}
static class AnimatorUp extends SwingWorker<String, Object> {
@Override
public String doInBackground() {
try {
x = (int)(panel.getWidth() / 2);
for(int i = 0; i < floor_distance; i++) {
y--;
panel.repaint();
try{ Thread.sleep(animation_speed); } catch(Exception ex) { ex.printStackTrace(); }
}
} catch (Exception ignore) {
}
return "";
}
}
static class AnimatorDown extends SwingWorker<String, Object> {
@Override
public String doInBackground() {
try {
x = (int)(panel.getWidth() / 2);
for(int i = 0; i < floor_distance; i++) {
y++;
panel.repaint();
try{ Thread.sleep(animation_speed); } catch(Exception ex) { ex.printStackTrace(); }
}
} catch (Exception ignore) {
}
return "";
}
}
static class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(215,215,215));
x = (int)(panel.getWidth() / 2);
g.fillOval(x, y, 50, 50);
}
}
}
您可以修改此代码,使按钮触发将电梯移动到特定楼层的动画。通过计算电梯当前Y位置与所需楼层Y位置之间的差值,可以设置要执行的动画的方向和范围