我必须做关于电梯管理系统的Java项目。
我的问题出在2 JPanel
。首先是JLabel
和Button
,每层一个 - 我使用布局,因为我希望它具有响应性。在程序启动之前我有对话框,我可以设置第一层和最后一层。这是代码:
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);
}
}
我想将电梯移动动画移动到另一个JLabel
中每个JPanel
的Y.任何想法如何做到这一点? getY()
之类的东西不起作用。
答案 0 :(得分:0)
如果我理解正确,包含按钮的面板和包含动画的面板将并排放置,从而具有相同的高度。在这种情况下,您可以通过了解面板的当前高度和您拥有的按钮数量来计算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);
}
}
}
您可以修改此代码,以便按钮触发动画,移动电梯&#34;到特定的楼层。通过计算电梯的当前Y位置之间的差异&#34;和想要的楼层的Y位置,你可以设置要执行的动画的方向和范围。