Positioning a JPanel in a JFrame at specific position

后端 未结 3 984
难免孤独
难免孤独 2021-01-14 03:48

I need an help for positioning a JPanel in a specific position into a Jframe.

I have a JPanel in a class who extends JFrame, and I need to put this JPanel in a speci

相关标签:
3条回答
  • 2021-01-14 04:43

    using setBounds(new Rectangle(96, 67, 98, 41)); you can do this... just see the example

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(300, 200);
        this.setContentPane(getJContentPane());
        this.setTitle("JFrame");
    }
    
    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getLoginButton(), null);
        }
        return jContentPane;
    }
    

    }

    0 讨论(0)
  • 2021-01-14 04:44

    Of the frame's content panel, the layout manager by default is a BorderLayout (NWSE+C). Do:

    this.getContentPanel().setLayout(null);
    
    0 讨论(0)
  • 2021-01-14 04:49

    A JFrame object uses borderlayout manager by default and JPanels use framelayout manager by default. If you want to use absolute positioning then you MUST use null layout because any other layout manager will use setLocation() and setSize() methods according to its own moods and not according to how YOU want it.

    Summary: Use setLayout(null) and use null manager and then use setBounds(X,Y,width,height) method.

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