最近,我尝试将按钮添加到 JLabels 数组中,这些按钮被放入 JPanel(位于 JFrame 中)中。出于某种原因,(我正在运行 eclipse)我得到一个编译器错误,该错误在这一行被切断:me[f].add(new JButton(" "));。我无法将一组按钮放入面板(panel1)。谁能告诉我我做错了什么?
package Grid;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Grid {
private static int width;
private static int length;
public static JFrame frame = new JFrame();
public static JPanel panel1 = new JPanel();
private static JButton but[] = new JButton[width*length];
private static JFrame window = new JFrame();
public static void Gridify(int width,int length){
//frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setEnabled(true);
panel1.setLayout(new GridLayout(width,length));
panel1.setVisible(true);
panel1.setBackground(Color.GREEN);
frame.add(panel1);
}
public void manualPanels(int num){
for(int f=0; f < num;f++){
JPanel me[] = new JPanel[f];
me[f].add(new JButton(" "));
me[f].setVisible(true);
panel1.add(me[f]);
}
frame.setVisible(true);
}
}