Switch语句没有正确递增

时间:2014-11-13 03:29:44

标签: java image events button

我有一个名为nextAd的按钮,每次点击时都会显示一组不同的图像,但是当我点击它时,没有任何图像出现。

问题:为什么我的switch声明不会导致显示不同的图片?

最好不使用数组

  public class AdvertisementPanel extends JPanel {

        Advertisement apple = new Advertisement("Apple", new ImageIcon("./src/AppleAdvertisement.jpg"), new ImageIcon ("./src/AppleLogo.jpg"));
        Advertisement IBM = new Advertisement("IBM", new ImageIcon("./src/IBMAdvertisement.jpg"), new ImageIcon ("./src/IBMLogo.jpg"));
        Advertisement microsoft = new Advertisement("Microsoft", new ImageIcon("./src/MicrosoftAdvertisement.jpg"), new ImageIcon ("./src/MicrosoftLogo.jpg"));
        Advertisement samsung = new Advertisement("Samsung", new ImageIcon("./src/SamsungAdvertisement.jpg"), new ImageIcon ("./src/SamsungLogo.jpg"));

        JLabel appleTitle = new JLabel(apple.getTitle());
        JLabel IBMTitle = new JLabel(IBM.getTitle());
        JLabel microsoftTitle = new JLabel(microsoft.getTitle());
        JLabel samsungTitle = new JLabel(samsung.getTitle());

        JLabel appleAdPic = new JLabel(apple.getPicture1());
        JLabel IBMAdPic = new JLabel(IBM.getPicture1());
        JLabel microsoftAdPic = new JLabel(microsoft.getPicture1());
        JLabel samsungAdPic = new JLabel(samsung.getPicture1());

        JLabel appleLogo = new JLabel(apple.getPicture2());
        JLabel IBMLogo = new JLabel(IBM.getPicture2());
        JLabel microsoftLogo = new JLabel(microsoft.getPicture2());
        JLabel samsungLogo = new JLabel(samsung.getPicture2());

        JButton nextAd = new JButton("Click for the next advertisement");

        public AdvertisementPanel() 
        {

            //set up panel
            setPreferredSize(new Dimension(1200,1000));
            setBackground(Color.gray);

            //adds action listener to button
            nextAd.addActionListener(new buttonListener());

            //for Apple
            add(appleTitle);
            add(appleAdPic);
            add(appleLogo);

            //for IBM
            add(IBMTitle);
            add(IBMAdPic);
            add(IBMLogo);
            IBMTitle.setVisible(false);
            IBMAdPic.setVisible(false);
            IBMLogo.setVisible(false);

            //for Microsoft
            add(microsoftTitle);
            add(microsoftAdPic);
            add(microsoftLogo);
            microsoftTitle.setVisible(false);
            microsoftAdPic.setVisible(false);
            microsoftLogo.setVisible(false);

            //for Samsung
            add(samsungTitle);
            add(samsungAdPic);
            add(samsungLogo);
            samsungTitle.setVisible(false);
            samsungAdPic.setVisible(false);
            samsungLogo.setVisible(false);

            //for Button
            add(nextAd);

        private class buttonListener implements ActionListener

        {
            private int i = 0;

            @Override
            public void actionPerformed(ActionEvent e) 
            {
            if(e.getSource() == nextAd);    
            {
                i++;    
            }
            switch (i){

            case '1': {
                appleTitle.setVisible(false);
                appleAdPic.setVisible(false);
                appleLogo.setVisible(false);
                IBMTitle.setVisible(true);
                IBMAdPic.setVisible(true);
                IBMLogo.setVisible(true);
                break;
            }
            case '2':{
                IBMAdPic.setVisible(false);
                IBMLogo.setVisible(false);
                microsoftAdPic.setVisible(true);
                microsoftLogo.setVisible(true);
                break;
            }
            case '3':{

                microsoftAdPic.setVisible(false);
                microsoftLogo.setVisible(false);
                samsungAdPic.setVisible(true);
                samsungLogo.setVisible(true);
                break;
            }
    }

2 个答案:

答案 0 :(得分:1)

  1. case '1':您正在与char(文字ASCII字符1)进行比较,而不是数字。 1在ASCII中为49,因此您并没有真正与您的想法进行比较。

  2. case foo:之后的大括号是不必要的。 break语句实际上是从case中删除,因此您应该删除{}

  3. 您在点击侦听器中设置private int = 0 ,因此它始终设置为0,然后递增为1,这样做无效。您需要在侦听器的外部声明它。

答案 1 :(得分:0)

在你的情况下你有冒号后的开口括号,不需要那些,应该是案例1:东西;打破;

也使你的int i成为一个私有的实例变量,在你的类中而不是在方法中声明,因为每次单击按钮,方法都会运行,并且我再次等于0

示例

switch (int) {
 case 1:
  stuff;
  moreStuff;
  break;
 case 2:
  stuff;
  moreStuff;
  break;
 case 3:
  stuff;
  moreStuff;
  break;
 default:
  default behavior;
  break;

}