因此,我正在创建一个基本应用程序,我希望在屏幕底部具有一个JLabel,该JLabel从左下角开始,然后在设置的时间内将动画样式移动到右下角,并在中心放置一个静态图像。为此,我使用BorderLayout用JPanel创建了一个JFrame。有一个JLabel,其中ImageIcon已添加到BorderLayout.CENTER,而JPanel位于BorderLayout.SOUTH。我的代码虽然草率编写,而且远非漂亮,但它是:import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension; import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;import javax.swing.BorderFactory;public class GameWindow extends JPanel{private static JLabel mainWindow, arrowLabel, arrowBox;protected static JFrame frame;protected static JPanel arrows;public static int x = 600;public GameWindow(){ mainWindow = new JLabel("Center"); arrowLabel = new JLabel("Moving"); arrows = new JPanel(); arrows.setSize(600, 100); arrows.setLayout(null); arrowBox = new JLabel(""); arrowBox.setBounds(0, 0, 150, 100); arrowBox.setPreferredSize(new Dimension(150, 100)); arrowBox.setBorder(BorderFactory.createLineBorder(Color.black)); arrows.add(arrowBox); this.setSize(600,600); this.setLayout(new BorderLayout()); this.add(mainWindow, BorderLayout.CENTER); this.add(arrows, BorderLayout.SOUTH);}public static void main(String[] args){ GameWindow g = new GameWindow(); frame = new JFrame("Sword Sword Revolution"); frame.add(g); frame.setSize(600,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);}}中心JLabel中的ImageIcon看起来很好,底部带有边框的空JLabel出现,但是我无法获得带有箭头图像的第二个JLabel出现在屏幕上。最终,我将更改为scheduleAtFixedRate以连续移动JLabel,但是现在我什至无法将图像显示在屏幕上。我也了解到,我很可能将无法使用FlowLayout,因为我了解到它不允许您设置组件的位置。我尝试使用null布局,但是使用null布局时,不会显示带有边框的空JLabel。我几乎无法辨认出边框底部的边框顶部,但是即使使用setLocation,我也无法使其显示在想要的位置。显然,我的思考过程存在缺陷,因此可以提供任何帮助。
3 回答

守着星空守着你
TA贡献1799条经验 获得超8个赞
很简单,看一下这个:
javax.swing.JLabel lb = new javax.swing.JLabel();
Image image = Toolkit.getDefaultToolkit().createImage("Your animated GIF");
ImageIcon xIcon = new ImageIcon(image);
xIcon.setImageObserver(this);
lb.setIcon(xIcon);
添加回答
举报
0/150
提交
取消