Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:07:29+00:00 2026-06-03T03:07:29+00:00

I have a problem with my JLayeredPane , I am probably doing something incredibly

  • 0

I have a problem with my JLayeredPane, I am probably doing something incredibly simple but I cannot wrap my head around it. The problem i have is that all the components are merged together and have not order. Could you please rectify this as I have no idea. The order I am trying to do is have a layout like this


output


label1 (behind)
input (in Front)


import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class window extends JFrame implements KeyListener {
/**
 * 
 */
private static final long serialVersionUID = 7092006413113558324L;
private static int NewSize;
public static String MainInput;
public static JLabel label1 = new JLabel();
public static JTextField input = new JTextField(10);
public static JTextArea output = new JTextArea(main.Winx, NewSize);

public window() {
    super("Satine. /InDev-01/");
    JLabel label1;
    NewSize = main.Winy - 20;
    setLayout(new BorderLayout());
    output.setToolTipText("");
    add(input, BorderLayout.PAGE_END);
    add(output, BorderLayout.CENTER);
    input.addKeyListener(this);
    input.requestFocus();
    ImageIcon icon = new ImageIcon("C:\\Users\\" + System.getProperty("user.name") + "\\AppData\\Roaming\\.Satine\\img\\textbox.png", "This is the desc");
    label1 = new JLabel(icon);
    add(label1, BorderLayout.PAGE_END);
}  
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_ENTER) {
        try {
            MainMenu.start();
        } catch (IOException e1) {
            System.out.print(e1.getCause());
        }
    }
}


@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {
}

 }

And the main class.

import java.awt.Container;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;


public class main {
public static int Winx, Winy;
private static JLayeredPane lpane = new JLayeredPane();
public static void main(String[] args) throws IOException{  
    Winx = window.WIDTH;
    Winy = window.HEIGHT;
    window Mth= new window();
    Mth.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Mth.setSize(1280,720);
    Mth.setVisible(true);
    lpane.add(window.label1);
    lpane.add(window.input);
    lpane.add(window.output);
    lpane.setLayer(window.label1, 2, -1);
    lpane.setLayer(window.input, 1, 0);
    lpane.setLayer(window.output, 3, 0);
    Mth.pack();
}
  }

Thank you for your time and I don’t expect the code to be written for me, all I want is tips on where I am going wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:07:30+00:00Added an answer on June 3, 2026 at 3:07 am

    I recommend that you not use JLayeredPane as the overall layout of your GUI. Use BoxLayout or BorderLayout, and then use the JLayeredPane only where you need layering. Also, when adding components to the JLayeredPane, use the add method that takes a Component and an Integer. Don’t call add(...) and then setLayer(...).

    Edit: it’s ok to use setLayer(...) as you’re doing. I’ve never used this before, but per the API, it’s one way to set the layer.

    e.g.,

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class LayeredPaneFun extends JPanel {
       public static final String IMAGE_PATH = "http://duke.kenai.com/" +
            "misc/Bullfight.jpg";
    
       public LayeredPaneFun() {
          try {
             BufferedImage img = ImageIO.read(new URL(IMAGE_PATH));
             ImageIcon icon = new ImageIcon(img);
             JLabel backgrndLabel = new JLabel(icon);
             backgrndLabel.setSize(backgrndLabel.getPreferredSize());
    
             JPanel forgroundPanel = new JPanel(new GridBagLayout());
             forgroundPanel.setOpaque(false);
    
             JLabel fooLabel = new JLabel("Foo");
             fooLabel.setFont(fooLabel.getFont().deriveFont(Font.BOLD, 32));
             fooLabel.setForeground(Color.cyan);
             forgroundPanel.add(fooLabel);
             forgroundPanel.add(Box.createRigidArea(new Dimension(50, 50)));
             forgroundPanel.add(new JButton("bar"));
             forgroundPanel.add(Box.createRigidArea(new Dimension(50, 50)));
             forgroundPanel.add(new JTextField(10));
             forgroundPanel.setSize(backgrndLabel.getPreferredSize());
    
             JLayeredPane layeredPane = new JLayeredPane();
             layeredPane.setPreferredSize(backgrndLabel.getPreferredSize());
             layeredPane.add(backgrndLabel, JLayeredPane.DEFAULT_LAYER);
             layeredPane.add(forgroundPanel, JLayeredPane.PALETTE_LAYER);
    
             setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
             add(new JScrollPane(new JTextArea("Output", 10, 40)));
             add(layeredPane);
    
          } catch (MalformedURLException e) {
             e.printStackTrace();
             System.exit(1);
          } catch (IOException e) {
             e.printStackTrace();
             System.exit(1);
          }
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("LayeredPaneFun");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new LayeredPaneFun());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with fancybox. I want to write a function that will run
i have problem with autorotate on iphone i set up in all classes -
i have problem with libhid . i found that there 2 way 4 accessing
I have problem with my own Subversion repository. It worked yesterday. But I received
I have problem with my code. I don't know what am I doing wrong.
I have problem with Joomla layouts in my component..There must be something bad with
I have problem with Activities navigation, searching works good. Activities hierarchy looks like that:
I have problem with passing variables through views. But, first some code // i
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with some JS virus on all of my websites. They're on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.