简体   繁体   English

我将如何在Java swing中进行此布局?

[英]How would I do this layout in Java swing?

I have a very specific layout in mind that I have been trying to accomplish for some time with no success. 我有一个非常具体的布局,已经尝试了一段时间,但没有成功。 The layout I am trying to create is just to put a single JPanel, of a fixed height positioned in the vertical center of the other panel. 我尝试创建的布局只是将一个固定高度的JPanel放置在另一个面板的垂直中心。 Horizontally, it should stretch to fit the bounds of the other. 在水平方向上,它应该伸展以适合另一个的边界。

Again, the needs are for it to be: 同样,需求是:

  1. Two JPanels, one placed inside the other 两个JPanel,一个放置在另一个内部
  2. The inner panel should have a static height 内面板应具有静态高度
  3. The inner panel should be vertically centered 内面板应垂直居中
  4. The inner panel should horizontally stretch to meet the sides of the outer panel. 内面板应水平拉伸以与外面板的侧面交汇。

The following is a picture of an example of this: 以下是此示例的图片: 在此处输入图片说明

Per several people's requests for me to post my code which doesn't work: 每几个人要求我发布无效的代码:

JPanel MainPanel = new JPanel();
frmPhoneClicker.getContentPane().add(MainPanel);
MainPanel.setLayout(new MigLayout("", "[984px]", "[165px]"));


Panel subPanel = new JPanel();
subPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
MainPanel.add(subPanel, "cell 0 0 1 1,growx,aligny center");

You definitely need to learn more about Layouts in your case GridBagLayout would be helpful. 在您的情况下,您肯定需要了解有关布局的更多信息,而GridBagLayout会有所帮助。 But here you go: 但是,您在这里:

// your Panels
JPanel mainPanel = new JPanel();
JPanel subPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout()); // use GridBagLayout with mainPanel
subPanel.setPreferredSize(new Dimension (0,165)); // use a preferred height for the subPanel
GridBagConstraints c = new GridBagConstraints(); // your gridBagLayout Constraints
c.fill = GridBagConstraints.HORIZONTAL; // stretch subPanel horizontal
c.weightx = 1.0; // with 100% of screen
mainPanel.add(subPanel,c); // add sub to mainPanel with the constraints

A complete working example with colors to follow your scheme: 一个完整的工作示例,并带有颜色以符合您的方案:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class testPanel extends JFrame{

    public testPanel() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        this.setSize(screenWidth / 2, screenHeight * 2 / 3);

        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setResizable(true);
        this.setAlwaysOnTop(false);
        this.setUndecorated(false);

        JPanel outerPanel = new JPanel(new GridBagLayout());
        outerPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
        JPanel innerPanel = new JPanel();
        innerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        innerPanel.setPreferredSize(new Dimension (0,300));

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;//Size
        outerPanel.add(innerPanel, c);

        this.add(outerPanel);
        this.setVisible(true);
    }

}

Please note that if the containing window size goes below the size of the innerPanel , then the fixed height will not apply anymore. 请注意,如果包含的窗口大小小于innerPanel的大小,则固定高度将不再适用。

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

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