简体   繁体   中英

Constant expression required with switch statement

I am just trying to get a source of event by switch statement but I can't. I'm getting:

Constant expression required

What am I doing wrong? Is it even possible?

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame implements ActionListener {

    JButton poteguj;
    JLabel podstawa, wykladnik, wynik;
    JTextField tPodstawa, tWykladnik;
    JCheckBox odwroc;
    int liczba1, liczba2;
    long wynikLiczb;

    public Main() {
        setTitle("Poteegpowanie");
        setSize(400, 250);
        setLayout(null);

        podstawa = new JLabel("Wprowadz podstawe:");
        podstawa.setBounds(20, 40, 150, 20);
        add(podstawa);

        tPodstawa = new JTextField("");
        tPodstawa.setBounds(170, 40, 150, 20);
        add(tPodstawa);

        wykladnik = new JLabel("Wprowadz wykladnik:");
        wykladnik.setBounds(20, 70, 150, 20);
        add(wykladnik);

        tWykladnik = new JTextField("");
        tWykladnik.setBounds(170, 70, 150, 20);
        add(tWykladnik);

        poteguj = new JButton("Poteguj!");
        poteguj.setBounds(130, 120, 100, 40);
        add(poteguj);
        poteguj.addActionListener(this);

        wynik = new JLabel("Wynik: ");
        wynik.setBounds(0, 160, 400, 20);
        add(wynik);

        odwroc = new JCheckBox("Odwroc dzialanie");
        odwroc.setBounds(130, 90, 150, 30);
        add(odwroc);
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        liczba2 = Integer.parseInt(tWykladnik.getText());
        liczba1 = Integer.parseInt(tPodstawa.getText());
        switch(source){
            case poteguj:
                if(!odwroc.isSelected()) {
                    wynikLiczb = (long) Math.pow(liczba1, liczba2);
                    wynik.setText(liczba1 + " do potegi " + liczba2 + " jest rowne: " + wynikLiczb);
                }
                else if(odwroc.isSelected()){
                    wynikLiczb = (int) Math.pow(liczba2, liczba1);
                    wynik.setText(liczba2 + " do potegi " + liczba1 + " jest rowne: " + wynikLiczb);
                }
            }
        }
    }

The problem is that poteguj is not a constant. You can only use switch on constant values. You're stuck with the if , I'm afraid.

You could, perhaps, put your sources into a List and extract a constant value from that.

List<JButton> sourceList = new ArrayList<>();
// add your sources in a known order

int sourceIndex = sourceList.indexOf(source);
switch (sourceIndex) {
case 0: // poteguj
    // do something
    break;
case 1: // ???

But this sacrifices clarity for dogma. if-else if is not bad when that's what the problem looks like. Your code should ideally reflect your problem.

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