java 如何在 JPanel 上定位/布局组件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16084840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:51:21  来源:igfitidea点击:

How to position/lay out components on JPanel?

javaswinglayout-managerboxlayout

提问by Bernard

I'm trying to position 5 buttons on a JFrame. Four buttons should be in first row and the fifth one should be in the second row. But here all of the buttons appear in one row and half of the fifth button is out of the panel. By the way if I use frame.pack(); my frame become smaller than what I expected. I don't want to change the size of frame. I want to locate the fifth one in the second row.

我试图在 JFrame 上放置 5 个按钮。四个按钮应该在第一行,第五个应该在第二行。但是这里所有的按钮都出现在一行中,第五个按钮的一半在面板之外。顺便说一下,如果我使用 frame.pack(); 我的框架变得比我预期的要小。我不想改变框架的大小。我想在第二行找到第五个。

import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.*;


public class test {

    public static void main(String[] args) {


        JFrame frame = new JFrame();
        frame.setBounds(100, 100, 529, 300);
        frame.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));


        JPanel panel = new JPanel();
        frame.getContentPane().add(panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));


        JButton btnNewButton_4 = new JButton("New button");
        panel.add(btnNewButton_4);

        JButton btnNewButton_3 = new JButton("New button");
        panel.add(btnNewButton_3);

        JButton btnNewButton = new JButton("New button");
        panel.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("New button");
        panel.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("New button");
        panel.add(btnNewButton_2);


        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

}

回答by mKorbel

Four buttons should be in first row and the fifth one should be in the second row.

四个按钮应该在第一行,第五个应该在第二行。

  • standard and booking examples in Oracle tutorial about How to use Layout Managers

  • use GridLayout (two rows)for JPanel, but all JButtons will have the same dimension

  • use GridBagLayout, then each of JButtons will have diferrent size or not too

  • invisible JComponents or empty JLabel can help you in most complicated variations

回答by Pablo Lozano

You are using a BoxLayout, where components will not wrap (check the API documentation: http://docs.oracle.com/javase/6/docs/api/javax/swing/BoxLayout.html)

您正在使用 BoxLayout,其中组件不会包装(检查 API 文档:http: //docs.oracle.com/javase/6/docs/api/javax/swing/BoxLayout.html

About the size, try to use panel.setMinimumSize(Dimension d)

关于尺寸,尽量使用 panel.setMinimumSize(Dimension d)

回答by tgkprog

What I do is use null as the layout manager and place the components my self. A manager works well if you want your components to be relayed when the window is resized or you do not know the final size of applet etc

我所做的是使用 null 作为布局管理器并将组件放置在我自己的位置上。如果您希望在调整窗口大小时中继组件或者您不知道小程序的最终大小等,则管理器可以很好地工作

But most of the time that is not true or over kill

但大多数时候这不是真的或过度杀戮

Instead just place the components on a grid by pixel using setBounds(x,y,width,height); Example:

而是使用 setBounds(x,y,width,height) 将组件按像素放置在网格上;例子:

    JFrame frame = new JFrame();
    frame.setBounds(100, 100, 529, 300);
    frame.getContentPane().setLayout(null);//over ride default
    Container c = frame.getContentPane();


    JButton btnNewButton_4 = new JButton("New button");
    c.add(btnNewButton_4);
    c.seBounds(4,10,40,25);

    JButton btnNewButton_3 = new JButton("New button 3");
    c.add(btnNewButton_3);
    c.seBounds(40,10,40,25);//...etc

Samples

样品

回答by Erfan

You can also use setBounds(arg1 , arg2 , arg3 , arg4) method. Use this for a better help. Doing Without a Layout Manager (Absolute Positioning)

您还可以使用 setBounds(arg1 , arg2 , arg3 , arg4) 方法。使用它以获得更好的帮助。无需布局管理器(绝对定位)