简体   繁体   English

如何在没有布局的情况下将按钮添加到新行?

[英]How can i add button to new line without layout?

This is my first gui project so if it's a dumb question please forgive me. 这是我的第一个gui项目,因此,如果这是一个愚蠢的问题,请原谅我。 I should add 25 buttons to panel in 5 rows with 5 buttons each row. 我应该在5行中向面板添加25个按钮,每行5个按钮。 It must be like 5X5 matrix. 它必须像5X5矩阵。 But I can't use layout in this project. 但是我不能在这个项目中使用布局。 So I have to do this without layout. 因此,我必须在没有布局的情况下执行此操作。

Here is my code for adding the buttons: 这是我添加按钮的代码:

for(int i=0;i<25;i++)
        {   
        b=new JButton();
        b.setLocation(0, 0);
        b.setPreferredSize(new Dimension(40, 40));
        b.setEnabled(true);
        panel.add(b);
        }

EDIT: Updated to a working MCVE 编辑:更新为工作的MCVE

The easiest would be to treat the rows and columns separately, with two nested for-loops. 最简单的方法是使用两个嵌套的for循环分别处理行和列。 This is done in the method addButtonsA in the example below. 在下面的示例中,通过方法addButtonsA完成此操作。

Alternatively, you can compute the current row and column from the loop variable (which runs from 0 to 25), as it is done in the addButtonsB method. 另外,您可以根据循环变量(从0到25)计算当前行和列,就像在addButtonsB方法中所做的那样。

Note that, in any case, when you are not using a layout manager, you have to specify the size of the buttons by calling setSize . 请注意,无论如何,当使用布局管理器时,必须通过调用setSize来指定按钮的大小 (Usually, when you are using a layout manager, you have to use setPreferredSize , but not in this case). (通常,在使用布局管理器时,必须使用setPreferredSize ,但在这种情况下则不能使用)。

The example: 这个例子:

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ManualButtonGrid
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(null);

        int numRows = 5;
        int numCols = 5;

        //addButtonsA(panel, numRows, numCols);
        addButtonsB(panel, numRows, numCols);

        f.getContentPane().add(panel);
        f.setSize(500,500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static void addButtonsA(JPanel panel, int numRows, int numCols)
    {
        int size = 60;
        for (int r = 0; r < numRows; r++)
        {
            for (int c = 0; c < numCols; c++)
            {
                JButton b = new JButton(String.valueOf(r*numCols+c));
                b.setLocation(c*size, r*size);
                b.setSize(new Dimension(size, size));
                panel.add(b);
            }
        }
    }

    private static void addButtonsB(JPanel panel, int numRows, int numCols)
    {
        int size = 60;
        for (int i = 0; i < 25; i++)
        {
            JButton b = new JButton(String.valueOf(i));
            int r = i / numCols;
            int c = i % numCols;
            b.setLocation(c*size, r*size);
            b.setSize(new Dimension(size, size));
            panel.add(b);
        }
    }

}

Here is a code to help you. 这是帮助您的代码。

panel.setLayout(null);
Dimension size = new Dimenstion(40, 40);
for(int i=0;i<5;i++){
    for(int j=0; j<5; j++){   
        b=new JButton();
        b.setBounds(size.width * i, size.height *j, size.width, size.height);
        b.setEnabled(true);
        panel.add(b);
    }
}
panel.setVisible(true);

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

相关问题 如何向按钮添加新位置? - How can I add new location to a button? 如何使用 onclick 按钮侦听器将 textview 添加到线性布局? - How can I add a textview to a linear layout with an onclick button listener? 如何在约束布局中添加浮动按钮 - How can i add floating button in constraint layout 如何使用Javafx中的按钮在TableView中添加新行 - How can I add a new row in tableview using a button in Javafx Android Studio-每次单击按钮时如何在线性布局上生成新的textview - Android Studio - How can I generate a new textview on my linear layout every time i click a button 如何在单击按钮时将自定义布局添加到线性布局 - how do I add a custom layout to a Linear Layout on the click of a button 如何在文件中添加新行而不覆盖当前行? - How to add a new line in a file without overwriting current line? 如何在不创建布局资源文件的情况下为微调器控件的文本添加空间? - How can I add space to the text of my spinner control without creating a layout resource file? 如何在命令行java程序中显示一行而不显示新行? - How can I make the display of a line in my command-line java program change without displaying a new line? 如何在相对布局或任何布局中动态添加视图 - How can I add Views dynamically in a Relative layout or in any Layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM