简体   繁体   中英

How to make a Jbutton array of images

I'm trying to build an array of JButton images. These will have a toggle (viewable/not viewable) thus why I chose to use JButton s.

These buttons also have a background image. When I place one button to the pane, this is in Java obviously, it works. But when I load the buttons in to an array and try to print them to the pane, nothing....I would appreciate the help. Here's what I have.

JButton card = new JButton();

JButton[] deck = new JButton[9];
int xLoc=20, yLoc=5;

card.setIcon(new ImageIcon("Back.jpg"));
card.setSize(200,250);
card.setVisible(true);

for(int i=0; i<9;i++)
{
    deck[i]=card;
}

for(int i=1;i<10;i++)
{
    deck[i-1].setLocation(xLoc,yLoc);
    pane.add(deck[i - 1]);
    validate();

    xLoc+=220;

    if(i%3==0)
    {
        yLoc+=265;

    }

In my mind, I am creating a card object with a size and a background and visible, and then loading the same card over and over in to my array, then adding it to the pane, that has a background. It's not causing any errors or exceptions, but it's not placing anything but the background to the pane.

Thanks in advance. I will be honest and say this is a homework assignment, but I am exceeding the expectations by going this route. I know I can create individual buttons and put them on the screen. I know how, and can do it. What I want to do is not covered in the scope of the class.

This is a project, not just an assignment, and the instructor encouraged learning new things on our own and expanding the project. So, by helping me you are not helping me cheat, but helping me learn something more than the class teaches. Thanks!

Your basic problem comes down to the fact that a component can only reside within a single parent...

// You create a card...
JButton card = new JButton();
// You create an array of buttons
JButton[] deck = new JButton[9];

int xLoc=20, yLoc=5;

// You set the icon
card.setIcon(new ImageIcon("Back.jpg"));
// This is not a good idea...
card.setSize(200,250);
// JButton is visible by default...
card.setVisible(true);

// Start your loop...
for(int i=0; i<9;i++)
{
    // Assign the card reference to an element in the array...
    deck[i]=card;
    // Add the card, via the array to the component...here's your problem...
    pane.add(deck[i - 1]);

In adding card to pane , it is first removed from pane , as it can only have one parent. What you need to do, is assign a unique instance of JButton to each element in the array

// You create an array of buttons
JButton[] deck = new JButton[9];

// Start your loop...
for(int i=0; i<9;i++)
{
    // Assign the card reference to an element in the array...
    deck[i]=new JButton();

    // You set the icon
    deck[i].setIcon(new ImageIcon("Back.jpg"));
    // This is not a good idea...
    deck[i].setSize(200,250);
    // JButton is visible by default...
    deck[i].setVisible(true);
    // Add the card, via the array to the component...here's your problem...
    pane.add(deck[i]);

Now, I can't see from your code snippet, but it would appear you are trying to use a null layout, this is highly inadvisable. Instead, make the time to learn and understand how to use appropriate layout managers .

If you're not using a null layout or don't know what I'm talking about, then things like setSize and setLocation won't work the way you expect them to...

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