所以,在我的上一个问题(“无法弄清楚如何在java中重叠图像”)中,我建议使用布局管理器和JLayeredPane。然而,在研究了演示并形成我自己的代码之后,我遇到了34个编译错误。编译器错误始终是“”,因此导入可能有问题。但是我完全从LayeredPane演示中复制了导入列表。再一次,我很难过。还有一次,我提前感谢任何人的建议!
import javax.swing.*;
import javax.swing.border.*;
import javax.accessibility.*;
import java.awt.*;
import java.awt.event.*;
public class SlotAnimatorTest extends JPanel
{
JPanel pane = new JPanel ();
pane.setPreferredSize(new Dimension(1500, 1500));
JPanel slotAnim;
private JPanel showSlotAnimators ()
{
slotAnim = new JPanel ();
SlotAnimator a0 = new SlotAnimator (45);
SlotAnimator a1 = new SlotAnimator (90);
SlotAnimator a2 = new SlotAnimator (180);
slotAnim.setLayout (new GridLayout (3,0,20,30));
slotAnim.add (a0);
slotAnim.add (a1);
slotAnim.add (a2);
return slotAnim;
}
ImageIcon background = new ImageIcon ("/Users/haleywight/Documents/slotmachine.png");
JLabel bG = new JLabel (background);
bGsetBounds(1500, 760, background.getIconWidth(), background.getIconHeight());
pane.add (bG, newInteger(0),0);
pane.add (showSlotAnimators (), newInteger (1));
private static void createAndShowGUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new SlotAnimatorTest();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main (String [] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
答案 0 :(得分:4)
这与JLayeredPane无关,与基本Java有很大关系。您不能在类和方法或构造函数或静态/非静态初始化程序块之外调用方法。
答案 1 :(得分:3)
以下陈述必须放在方法内。
bGsetBounds(1500, 760, background.getIconWidth(), background.getIconHeight());
pane.add (bG, newInteger(0),0);
pane.add (showSlotAnimators (), newInteger (1));