[英]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);
}
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.