简体   繁体   中英

Actionlistener and cycling through JLabels with a JButton: wrap around?

I've got this mostly figured out: the assignment is to make four different advertisements that cycle through when you click the next button, and when you reach number 4, wraps around to number 1. I'm not sure why it's not wrapping back around?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class advertisementPanel extends JPanel {

public advertisementPanel() throws IOException{

    setPreferredSize(new Dimension(500, 200));
    URL url = new URL("http://www.geneca.com/wp-content/uploads/2013/03/mcdonalds-small-logo1.png");
    URL url2 = new URL("http://www.eatthis.com/uploads/wendys.jpg");
    URL url3 = new URL("http://www.scaredmonkeys.com/fun-images/KFC_20old_small.jpg");
    URL url4 = new URL("http://www.canaltagroup.com/images/aw_logo.gif");



    Image image = ImageIO.read(url);
    ImageIcon myIcon = new ImageIcon(image);
    JLabel adPicture1 = new JLabel(myIcon);
    Image image2 = ImageIO.read(url2);
    ImageIcon myIcon2 = new ImageIcon(image2);
    JLabel adPicture2 = new JLabel(myIcon2);
    Image image3 = ImageIO.read(url3);
    ImageIcon myIcon3 = new ImageIcon(image3);
    JLabel adPicture3 = new JLabel(myIcon3);
    Image image4 = ImageIO.read(url4);
    ImageIcon myIcon4 = new ImageIcon(image4);
    JLabel adPicture4 = new JLabel(myIcon4);

    JLabel myLabel1 = new JLabel("McDonald's");
    JLabel myLabel2 = new JLabel("20% off!");
    JLabel myLabel3 = new JLabel("Wendy's");
    JLabel myLabel4 = new JLabel("50% off!");
    JLabel myLabel5 = new JLabel("KFC's");
    JLabel myLabel6 = new JLabel("90% off!");
    JLabel myLabel7 = new JLabel("A&W's");
    JLabel myLabel8 = new JLabel("10% off!");

    JButton button = new JButton("next");

    setBackground(Color.white);
    add(adPicture1);
    add(myLabel1);
    add(myLabel2);


    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent myEvent) {


            if (getBackground() == Color.white) {
                setBackground(Color.green);
                myLabel1.setVisible(false);
                myLabel2.setVisible(false);
                adPicture1.setVisible(false);
                add(myLabel3);
                add(myLabel4);
                add(adPicture2);


            } else if (getBackground() == Color.green) {

                setBackground(Color.red);
                myLabel3.setVisible(false);
                myLabel4.setVisible(false);
                adPicture2.setVisible(false);
                add(myLabel5);
                add(myLabel6);
                add(adPicture3);

            } else if (getBackground() == Color.red) {

                setBackground(Color.pink);
                myLabel5.setVisible(false);
                myLabel6.setVisible(false);
                adPicture3.setVisible(false);
                add(myLabel7);
                add(myLabel8);
                add(adPicture4);

            } else if (getBackground() == Color.pink) {

                myLabel7.setVisible(false);
                myLabel8.setVisible(false);
                adPicture4.setVisible(false);
                add(myLabel1);
                add(myLabel2);
                add(adPicture1);
                setBackground(Color.white);

            }

            }


        });
        add(button);

    }

}

You probably don't want to add() labels and pictures each time the button is clicked. That said, they are probably being added but the old ones aren't removed. So they are added to the panel outside of its visible region.

You can change the text and icon of a label when a button is clicked instead of adding new ones.

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