Not sure where compiler errors occur when using JLayeredPane

So, in my last question (β€œCan't figure out how to overlap images in java”), I was kindly recommended to use layout managers and JLayeredPane. However, having studied the demos and generated my own code, I have a whopping 34 compiler errors. Compiler errors are consistently "", so maybe something is wrong with the import. However, I copied the import list from the LayeredPane demo. Once again, I'm at a standstill. And again, I thank you in advance for your advice!

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(); } }); } } 
+1
source share
2 answers

This has nothing to do with JLayeredPane and has a lot to do with basic Java. You cannot call a method in a class or outside a method or constructor or static / non-static initializer blocks.

+4
source

The following instructions should be placed inside the method.

 bGsetBounds(1500, 760, background.getIconWidth(), background.getIconHeight()); pane.add (bG, newInteger(0),0); pane.add (showSlotAnimators (), newInteger (1)); 
+3
source

All Articles