Java Swing - how to show a panel on top of another panel?

后端 未结 6 1302
野趣味
野趣味 2020-11-30 03:56

I wish to have an internal (non window) dialog to ask for member input. I would like the dialog to be placed centrally on an existing JPanel.

I have looked at layered

相关标签:
6条回答
  • 2020-11-30 04:18

    You can add an undecorated JDialog like this:

    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class TestSwing {
      public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("Parent");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setVisible(true);
    
        final JDialog dialog = new JDialog(frame, "Child", true);    
        dialog.setSize(300, 200);
        dialog.setLocationRelativeTo(frame);
        JButton button = new JButton("Button");
        button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            dialog.dispose();
          }
        });
        dialog.add(button);
        dialog.setUndecorated(true);
        dialog.setVisible(true);
      }
    }
    
    0 讨论(0)
  • 2020-11-30 04:19

    I think LayeredPane is your best bet here. You would need a third panel though to contain A and B. This third panel would be the layeredPane and then panel A and B could still have a nice LayoutManagers. All you would have to do is center B over A and there is quite a lot of examples in the Swing trail on how to do this. Tutorial for positioning without a LayoutManager.

    public class Main {
        private JFrame frame = new JFrame();
        private JLayeredPane lpane = new JLayeredPane();
        private JPanel panelBlue = new JPanel();
        private JPanel panelGreen = new JPanel();
        public Main()
        {
            frame.setPreferredSize(new Dimension(600, 400));
            frame.setLayout(new BorderLayout());
            frame.add(lpane, BorderLayout.CENTER);
            lpane.setBounds(0, 0, 600, 400);
            panelBlue.setBackground(Color.BLUE);
            panelBlue.setBounds(0, 0, 600, 400);
            panelBlue.setOpaque(true);
            panelGreen.setBackground(Color.GREEN);
            panelGreen.setBounds(200, 100, 100, 100);
            panelGreen.setOpaque(true);
            lpane.add(panelBlue, new Integer(0), 0);
            lpane.add(panelGreen, new Integer(1), 0);
            frame.pack();
            frame.setVisible(true);
        }
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            new Main();
        }
    
    }
    

    You use setBounds to position the panels inside the layered pane and also to set their sizes.

    Edit to reflect changes to original post You will need to add component listeners that detect when the parent container is being resized and then dynamically change the bounds of panel A and B.

    0 讨论(0)
  • 2020-11-30 04:20

    There's another layout manager that may be of interest here: Overlay Layout Here's a simple tutorial: OverlayLayout: for layout management of components that lie on top of one another. Overlay Layout allows you to position panels on top of one another, and set a layout inside each panel. You are also able to set size and position of each panel separately.

    (The reason I'm answering this question from years ago is because I have stumbled upon the same problem, and I have seen very few mentions of the Overlay Layout on StackOverflow, while it seems to be pretty well documented. Hopefully this may help someone else who keeps searching for it.)

    0 讨论(0)
  • 2020-11-30 04:21

    I just thought that I'd add that there is a notion of Z-order in Swing, see [java.awt.Component#setComponentZOrder][1]

    which affects the positions of a component in its parents component array, which determines the painting order.

    Note that you should override javax.swing.JComponent#isOptimizedDrawingEnabled to return false in the parent container to get your overlapping components to repaint correctly, otherwise their repaints will clobber each other. (JComponents assume no overlapping children unless isOptimizedDrawingEnabled returns false)

    [1]: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html#setComponentZOrder(java.awt.Component, int)

    0 讨论(0)
  • 2020-11-30 04:26

    Use a 1 by 1 GridLayout on the existing JPanel, then add your Panel to that JPanel. The only problem with a GridLayout that's 1 by 1 is that you won't be able to place other items on the JPanel. In this case, you will have to figure out a layout that is suitable. Each panel that you use can use their own layout so that wouldn't be a problem.

    Am I understanding this question correctly?

    0 讨论(0)
  • 2020-11-30 04:26

    JOptionPane.showInternalInputDialog probably does what you want. If not, it would be helpful to understand what it is missing.

    0 讨论(0)
提交回复
热议问题