Arranging Rotation Components in the Swing Panel

I am new to Swing but I am trying to achieve what I am trying to do. I would like to create a screen that looks like this:

enter image description here

I'm not sure what layout is best for such a screen. I tried using BorderLayout but it never exits. I got buttons on the bottom right, but the components in the middle turned out to be quite complex. I know how to add components to the panel, but the area I'm struggling with is how to align them. Sometimes, if I add a textbox to a panel with BORDERLAYOUT, it takes up the full space of the entire panel, which I don't want.

I managed to get it to almost work using AbsoluteLayout, but I suspect this is not the best option if I want the screen to be liquid.

setBounds(100, 100, 775, 599);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBackground(SystemColor.control);
contentPanel.setForeground(Color.RED);
contentPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BorderLayout(0, 0));
{
    JPanel topHeadersPanel = new JPanel();
    contentPanel.add(topHeadersPanel, BorderLayout.NORTH);
    topHeadersPanel.setLayout(new BorderLayout(0, 0));
    {
        JLabel lblSetSourceRecord = new JLabel("Source customer record:");
        lblSetSourceRecord.setFont(new Font("Tahoma", Font.BOLD, 12));
        topHeadersPanel.add(lblSetSourceRecord, BorderLayout.WEST);
    }
    {
        JLabel lblSetDestinationRecord = new JLabel("Destination customer record:");
        lblSetDestinationRecord.setFont(new Font("Tahoma", Font.BOLD, 12));
        topHeadersPanel.add(lblSetDestinationRecord, BorderLayout.EAST);
    }

    {
        JLabel lblSetDestinationRecord = new JLabel("Source customer:");
        lblSetDestinationRecord.setFont(new Font("Tahoma", Font.BOLD, 12));
        topHeadersPanel.add(lblSetDestinationRecord, BorderLayout.SOUTH);
    }
}
{
    JPanel buttonPane = new JPanel();
    buttonPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
    getContentPane().add(buttonPane, BorderLayout.SOUTH);
    {
        JPanel panel = new JPanel();
        buttonPane.add(panel);
    }
    {
        JButton okButton = new JButton("OK");
        okButton.setActionCommand("OK");
        buttonPane.add(okButton);
        getRootPane().setDefaultButton(okButton);
    }
    {
        JButton cancelButton = new JButton("Cancel");
        cancelButton.setActionCommand("Cancel");
        buttonPane.add(cancelButton);
    }

      

+3


source to share


3 answers


Try to make the top in GridLayout

with 1 row and 2 columns, add this in BorderLayout

as CENTER and buttons as BorderLayout.BOTTOM

. At the top, use two panels, one for the left, one for the right.

And here is some code to show it (I couldn't really copy / paste your example):



public class MW extends JFrame {

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }

    private static void createAndShowUI() {
        MW x = new MW();

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(1, 2));

        JPanel leftPanel = new JPanel();
        JLabel srcLabel = new JLabel("Source record:");
        leftPanel.add(srcLabel);
        JPanel rightPanel = new JPanel();
        JLabel dstLabel = new JLabel("Destination record:");
        rightPanel.add(dstLabel);

        mainPanel.add(leftPanel);
        mainPanel.add(rightPanel);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton okButton = new JButton("OK");
        buttonPanel.add(okButton);
        JButton cancelButton = new JButton("Cancel");
        buttonPanel.add(cancelButton);

        x.setSize(400, 400);
        x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        x.add(mainPanel, BorderLayout.CENTER);
        x.add(buttonPanel, BorderLayout.PAGE_END);
        x.setLocationRelativeTo(null);
        x.setVisible(true);
    }
}

      

+5


source


Look at this image where I have inserted nested components that will do this

enter image description here

On the upper level, there are two brown sections in a vertical box.

Then the top one has either a grid or a horizontal border shown in blue.

Then they each have a yellowish box layout with appropriate spacing in between.



Next containers and use the appropriate layouts in each, and the final components go into the container.

There are several tools that make this easier. I've used the NetBeans Editor for a long time. Then I used the editor in the eclipse version of IBM WSAD / RAD. I now use the Matisse editor in MyEclipse if I ever need to make a layout like this.

Edit

Just noticed @DaDaDom suggested something simlar outside, brown level. He likes the layout of the border with the top drawer in CENTER and the button on SOUTH. This will work really well.

+4


source


I would also use Layout Group as it will help you align labels and text boxes.

-1


source







All Articles