【问题标题】:Switch statement not incrementing correctlyswitch 语句没有正确递增
【发布时间】:2015-01-10 02:59:13
【问题描述】:

我有一个名为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;
            }
    }

【问题讨论】:

  • ./src/ 在构建程序后将不存在,因此无法找到图像。 ImageIcon(String) 需要一个文件资源,而您的图像不是...哦,我为什么要打扰,您显然没有在听,因为您已经删除了有关同一代码的上一个问题...
  • 不抱歉,我在听文件位置,但我主要关心的是找出为什么 switch 语句不能正确执行,这是我在这里发现的。非常感谢您指出这一点,并将努力解决这个问题。
  • 别担心我,我只是度过了糟糕的一天,很抱歉把它拿给你;)
  • 发生在我们最好的人身上。编程愉快!

标签: java image events button


【解决方案1】:
  1. case '1': 您正在与 char(文字 ASCII 字符 1)进行比较,而不是数字。 1 在 ASCII 中是 49,所以你并没有真正与你认为的相比。

  2. case foo: 后面的大括号是不必要的。 break 语句实际上是跳出块而不是跳出case,因此您应该删除{}s。

  3. 您将private int = 0 inside 设置为单击侦听器,因此它始终设置为 0,然后递增为 1,这没有任何作用。您需要在侦听器之外声明它

【讨论】:

  • 那我怎样才能让它注册为“一键点击”呢?
  • @JordanPurinton 呃,你是什么意思?注册什么?
  • 所以如果我点击一次按钮,它会执行'Case'1':',如果我第二次点击它,它会执行'Case'2':'等
  • @JordanPurinton 如果您修复了答案中提到的问题,这正是它会做的事情。
  • 哦,哎呀。想通了那部分,谢谢。那我如何让它循环回到第一个广告出现并重复序列的原始起始位置?
【解决方案2】:

在你的情况下,冒号后面有左大括号,不需要那些,应该是 case 1 : stuff;休息;

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

例子

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多