Question

I have 3 JPanels wich have no Components and have just Graphics2D pictures on them. The positioning of pictures wasn't a problem. But i have a problem trying to put them in necessary order on a JFrame. This is the code I am using:

    setLayout(new BorderLayout());
    JPanel panel = new JPanel();
    BoxLayout bxLayout=new BoxLayout(panel,BoxLayout.Y_AXIS);

    setLayout(new BorderLayout());
    CaptionPanel cPanel= new CaptionPanel("Евгений",new Font("Serif",Font.BOLD,20),20,0);
    cPanel.setPreferredSize(new Dimension(220,70));

    BFieldPanel bField = new BFieldPanel(20,20);
    bField.setPreferredSize(new Dimension(220,220));

    BStatePanel bsPanel=new BStatePanel(20,0);
    bsPanel.setPreferredSize(new Dimension(220,70));

    panel.add(cPanel);
    panel.add(bField);
    panel.add(bsPanel);

    add(panel,BorderLayout.CENTER);

I need a efficient method to force layout manager to consider Panel's size. setPrefferedSize() method as i saw has too law prioritet in form panel's pisitioning calculation. U can find the result i want

Was it helpful?

Solution

You may benefit from reading this. Its not clear what your question is because you haven't described what problem you're having, you've just described what you want to do, not what is not working or what's preventing you from doing what you want to do.

I think you're looking for an example of how to get Components (in your case a CaptionPanel, BFieldPanel, and BStatePanel) to align vertically using a BorderLayout. Since I don't have the components in your example, this example shows generally how to use the border layout:

import javax.swing.*;
import java.awt.*;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Example");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.getContentPane().add(getPanel());
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static JPanel getPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JLabel("Top"), BorderLayout.NORTH);
        panel.add(new JLabel("Center"), BorderLayout.CENTER);
        panel.add(new JLabel("Bottom"), BorderLayout.SOUTH);
        panel.setPreferredSize(new Dimension(400, 300));
        return panel;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top