简体   繁体   English

如何使用Swing创建布局?

[英]How to create layout with Swing?

I want to design like frame using four panel, taking into account zoom in and out the screen... 考虑到屏幕的放大和缩小,我想使用四个面板来设计框架。

Like this design: 像这样的设计:

JPanel testPanel = new JPanel();
testPanel.setBorder(BorderFactory.createLineBorder(Color.black));
frame.add(testPanel, BorderLayout.NORTH);

JPanel testPanel1 = new JPanel();
testPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
frame.add(testPanel1, BorderLayout.NORTH);

JPanel testPane2 = new JPanel();
testPane2.setBorder(BorderFactory.createLineBorder(Color.black));
frame.add(testPane2, BorderLayout.EAST);

JPanel testPane3 = new JPanel();
testPane3.setBorder(BorderFactory.createLineBorder(Color.black));
frame.add(testPane3, BorderLayout.CENTER);

You could... 你可以...

Use a GridBagLayout ... 使用GridBagLayout ...

例

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(makePanel("panel 2"), gbc);
            gbc.gridy++;
            add(makePanel("panel 3"), gbc);
            gbc.gridy++;
            gbc.weighty = 0.66;
            gbc.weightx = 0.66;
            add(makePanel("panel 3"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.gridheight = gbc.REMAINDER;
            add(makePanel("panel 1"), gbc);
        }

        protected JPanel makePanel(String name) {
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(new JLabel(name));
            panel.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(10, 10, 10, 10)));
            return panel;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 400);
        }



    }

}

You could... 你可以...

Also make use of compound layouts, this is useful in situations where the rules are complex and/or only effect certain components. 还可以使用复合布局,这在规则复杂和/或仅影响某些组件的情况下很有用。

Which you might use will depend on what you are trying to achieve, at the end of the day, there is not hard and fast rule, you need to need look at the underlying requirements and consider which approaches are the best - don't forget, you can combine different approaches to build different solutions 您可能使用哪种方法取决于您要实现的目标,最终没有硬性规定和快速的规则,您需要查看基础需求并考虑哪种方法是最佳的-不要忘记,您可以结合使用不同的方法来构建不同的解决方案

Don't forget to have a look at Laying Out Components Within a Container for more details 不要忘记查看在容器内布置组件以了解更多详细信息

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

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