简体   繁体   中英

How can I make a for loop of JButtons?

public class BoardView extends JFrame
 {

private JButton board [][];

private GameBoard gameboard;

public static JButton cat1 = new JButton();
public static JButton cat2 = new JButton();
public static JButton car1 = new JButton();
public static JButton car2 = new JButton();
public static JButton dog1 = new JButton();
public static JButton dog2 = new JButton();
public static JButton bike1 = new JButton();
public static JButton bike2 = new JButton();

public BoardView()
{
    Container buttonLayout;
    /**
     * Exits the program when closed is clicked
     */
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    /**
     * Sets the title for the Game
     */
    this.setTitle("Memory Of Humanity Game");
    /**
     * Sets the size for the JFrame
     */
    this.setSize(800, 600);
    /**
     * Makes the Pane into a Grid Layout so the Buttons 
     * Line up
     */
    buttonLayout = getContentPane();
    buttonLayout.setLayout(new GridLayout(7, 6));
    /**
     * This adds each JButton to the Pane of the Game
     */
    buttonLayout.add(cat1);
    buttonLayout.add(cat2);
    buttonLayout.add(car1);
    buttonLayout.add(car2);
    buttonLayout.add(dog1);
    buttonLayout.add(dog2);
    buttonLayout.add(bike1);
    buttonLayout.add(bike2)
  }
}

So instead of having to add every JButton one by one like this, how would I create a for loop to do this automatically for me? I have seen a couple on the internet however I don't understand how to loop the the .add part of the JButton. Thank you!

for(int i = 0; i < 8; i++) {
    buttonLayout.add(new JButton());
}

This will add 8 JButtons to buttonLayout.

If you need to access them later (which you probably do), you might want to use this:

List<JButton> buttonList = new ArrayList<JButton>();
for(int i = 0; i < 8; i++) {
    JButton button = new JButton();
    buttonList.add(button);
    buttonLayout.add(button);
}

If you want to add a single image to all your buttons:

for(int i = 0; i < 8; i++) {
    ImageIcon image = new ImageIcon("C:/path/to/your/image.jpg");
    JButton button = new JButton(image);
    buttonList.add(button);
}

If you want to add different images to your buttons:

String[] paths = {"C:/1.jpg", "C:/2.jpg", "C:/3.jpg", "C:/4.jpg", "C:/5.jpg", "C:/6.jpg", "C:/7.jpg", "C:/8.jpg"};
for(int i = 0; i < 8; i++) {
    ImageIcon image = new ImageIcon(paths[i]);
    JButton button = new JButton(image);
    buttonList.add(button);
}

Of course, edit the paths according to your needs. Note that the paths can be relative, meaning based on the location of the program.

First initialize an array of buttons.

JButton[] buttons = new JButton[8] // instead of having cat1, cat2 ... you have buttons[0], buttons[1] ...

Then do a for loop to initialize and add each button.

for (JButton button : buttons) {
    button = new JButton();
    buttonLayout.add(button);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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