简体   繁体   English

在JPanel中布局组件

[英]Layouting components in a JPanel

I have 3 JPanels wich have no Components and have just Graphics2D pictures on them. 我有3个JPanels,它们没有任何组件,而只有Graphics2D图片。 The positioning of pictures wasn't a problem. 图片的位置不是问题。 But i have a problem trying to put them in necessary order on a JFrame. 但是我试图将它们按必要的顺序放在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. 正如我所看到的,setPrefferedSize()方法在表单面板的位置计算中也具有法律上的优先权。 U can find the result i want 你可以找到我想要的结果

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. 我认为您正在寻找一个示例,该示例如何使用BorderLayout使Components(在您的情况下为CaptionPanel,BFieldPanel和BStatePanel)垂直对齐。 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM