How to place JLabel on top right corner just below the title bar?

前端 未结 4 820
孤城傲影
孤城傲影 2021-01-27 12:52

Hello I read about Layouts but didn\'t get which one to use for my application. I want to add image to JPanel and place JLabel on op right corner just below the title bar.

4条回答
  •  迷失自我
    2021-01-27 13:19

    Since you have a dominating feature in your layout (the image) you probably want to use a BorderLayout as your main layout.

    frame.getContentPane().setLayout(new BorderLayout(4, 4));
    

    then add the image to the BorderLayout.CENTER

    next the JLabel wants to go in the BorderLayout.NORTH

    Now, if you do that, it won't go to the far right, so create a JPanel for the north, add the JLabel to the panel, and place the panel in the north.

    Now you need a layout for the north panel. A BoxLayout will work

    northPanel.add(Box.createHorizontalGlue());
    northPanel.add(label);
    

提交回复
热议问题