简体   繁体   English

如何在JPanel上放置/布局组件?

[英]How to position/lay out components on JPanel?

I'm trying to position 5 buttons on a JFrame. 我正在尝试在JFrame上放置5个按钮。 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(); 顺便说一下,如果我使用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. 我想在第二行中找到第五个。

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);


    }

}

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 Oracle教程中有关如何使用布局管理器的标准和预订示例

  • use GridLayout (two rows) for JPanel, but all JButtons will have the same dimension 为JPanel使用GridLayout(两行) ,但是所有JButton的尺寸都相同

  • use GridBagLayout , then each of JButtons will have diferrent size or not too 使用GridBagLayout ,那么每个JButton将具有不同的大小或不是

  • invisible JComponents or empty JLabel can help you in most complicated variations 不可见的JComponents或空的JLabel可以帮助您进行最复杂的变化

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)

What I do is use null as the layout manager and place the components my self. 我要做的是使用null作为布局管理器,并将组件放置在自己的位置。 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 如果您希望在调整窗口大小时中继组件,或者您不知道applet的最终大小,那么经理可以很好地工作

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); 相反,只需使用setBounds(x,y,width,height);将像素按像素放置在网格上即可; Example: 例:

    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 样品

You can also use setBounds(arg1 , arg2 , arg3 , arg4) method. 您还可以使用setBounds(arg1,arg2,arg3,arg4)方法。 Use this for a better help. 使用它以获得更好的帮助。 Doing Without a Layout Manager (Absolute Positioning) 在没有布局管理器的情况下进行操作(绝对定位)

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

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