質問

I have been trying to make a Java Swing GUI in Eclipse Window Builder. After much of toil, I have been able to get to this. What I did was make a JFrame, add a BorderLayout to it. I added two panels in the north and south positions of this layout. I added a few buttons in the bottom panel.

My question is from the top panel. This panel has a Flow Layout, and has two more panels. The left panel has buttons which are at the right place. The right panel is itself not at the right place.

  1. I want the right panel to be aligned to the right side of the parent window (and the left panel should stay on the left) - so ultimately there will be some space between the right and left panels. That is both the right and the left panels should be right and left aligned) and there should be space between them, because I want 4 buttons to the left, and 2 buttons to the right side of my screen.

  2. In the right panel, the two rightmost button among the two buttons should be right aligned, and there should be some space between both the buttons.

I am providing the source code. I will be grateful if somebody can help/guide me to achieve what I want.

enter image description here


SOURCE CODE:-

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

public class ParentFrame extends JFrame {
    public ParentFrame() {
        getContentPane().setLayout(new BorderLayout(50, 50));

        JPanel parentPanel_bottom = new JPanel();
        getContentPane().add(parentPanel_bottom, BorderLayout.SOUTH);
        JButton btnInstr = new JButton("1");
        parentPanel_bottom.add(btnInstr);
        JButton btnCourse = new JButton("2");
        parentPanel_bottom.add(btnCourse);
        JButton btnModule = new JButton("3");
        parentPanel_bottom.add(btnModule);
        JButton btnDays = new JButton("4");
        parentPanel_bottom.add(btnDays);
        JButton btnXtra = new JButton("5");
        parentPanel_bottom.add(btnXtra);

        JPanel parentPanel_top = new JPanel();
        getContentPane().add(parentPanel_top, BorderLayout.NORTH);
        parentPanel_top.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));


        JPanel topleftpanel = new JPanel();
        parentPanel_top.add(topleftpanel, BorderLayout.WEST);
        JButton button = new JButton("1");
        topleftpanel.add(button);
        JButton button_1 = new JButton("2");
        topleftpanel.add(button_1);
        JButton button_2 = new JButton("3");
        topleftpanel.add(button_2);
        JButton button_3 = new JButton("4");
        topleftpanel.add(button_3);

        JPanel toprightpanel = new JPanel();
        parentPanel_top.add(toprightpanel, BorderLayout.EAST);
        parentPanel_top.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        JButton button_4 = new JButton("1");
        toprightpanel.add(button_4);
        JButton button_5 = new JButton("2");
        toprightpanel.add(button_5);
    }

}
役に立ちましたか?

解決

Try this one

JPanel parentPanel_top = new JPanel(new GridLayout(1, 2));
...
JPanel topleftpanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
parentPanel_top.add(topleftpanel);    
...
JPanel toprightpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
parentPanel_top.add(toprightpanel);

Note : remove extra setLayout() set for above JPanels.

enter image description here

他のヒント

The layout you set to the parentPanel_top panel is flowLayout you cant just call the BorderLayout.WEST to specify it to the west.. BorderLayout can only use it not FlowLayout..

Solution:

change the layout to BorderLayout..

parentPanel_top.setLayout(new BorderLayout());
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top