【问题标题】:How can I get my switch case to act on JFrame?如何让我的开关盒作用于 JFrame?
【发布时间】:2015-03-11 06:19:49
【问题描述】:

我已经设法为我的程序构建了初始启动屏幕 (JFrame),但我似乎无法让按钮进入我想要的屏幕。我尝试构建它时遇到的错误是:

error: incompatible types: JFrame cannot be converted to int

这是我的代码:

    import javax.swing.*;
    import javax.swing.JFrame;
    public class MovieGenerator extends JFrame
    {//Buttons
    JButton titleButton;
    JButton actorButton;
    JButton genreButton;
    JButton runtimeButton;
    JButton ratingButton;
    JButton addButton;
    JButton randomButton;
    //Main GUI
    public MovieGenerator() 
    {
        super("Main Menu");
        JFrame main = new JFrame();
        setLookAndFeel();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000,200);
        GridLayout grid = new GridLayout(2,4);
        setLayout(grid);
        titleButton = new JButton("Sort by Title");
        actorButton = new JButton("Find by Actor");
        genreButton = new JButton("Sort by Genre");
        runtimeButton = new JButton("Sort by Runtime");
        ratingButton = new JButton("Sort by Rating");
        addButton = new JButton("Add a new movie to collection");
        randomButton = new JButton("Random movie");
        //Containers
        add(titleButton);
        add(actorButton);
        add(genreButton);
        add(runtimeButton);
        add(ratingButton);
        add(addButton);
        add(randomButton);
        setVisible(true);
    }
    public static void main(String[] args) throws IOException
    {
        MovieGenerator mov = new MovieGenerator();
        Scanner input = new Scanner(System.in);
        JFrame main = new JFrame();
        switch (main)
        {
            case 0:
            {
                int titleButton = JOptionPane.showOptionDialog(null, "How do you want to sort?", "Main Menu",JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
            new Object[] {"Alphabetically Descending", "Alphabetically Ascending",}, " ");
            }
            //  break;

            case 1:
            {
                int genreButton = JOptionPane.showOptionDialog(null, "Set genre", "Sort by Genre",JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
            new Object[] {"Drama", "Comedy","Action/Adventure","Horror","Romance","SciFi/Fantasy","Western","Foreign","Animation"}, " ");
            }

            //  break;
            case 2:
            {       
                int runtimeButton = JOptionPane.showOptionDialog(null, "Set runtime range", "Sort by Runtime",JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
            new Object[] {"90-110 minutes", "111-130 minutes","131-150 minutes","151-170 minutes","171+ minutes"}, " ");
            }
            //  break;
            case 3:
            {
                int ratingButton = JOptionPane.showOptionDialog(null, "Set ratings range", "Sort by Ratings",JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
            new Object[] {"Search 0-10%", "Search 11-20%","Search 21-30%", "Search 31-40%","Search 41-50%", 
                          "Search 51-60%", "Search 61-70%","Search 71-80%", "Search 81-90%","Search 91-100%"}, " ");
            }
            //  break;
            case 4:
            {
                int randomButton = JOptionPane.showOptionDialog(null, "Randomize your selection", "Random!",JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
            new Object[] {"Randomize!"}, " ");
            //break;
            }           

        }   
    }
}

【问题讨论】:

标签: java swing compiler-errors jframe switch-statement


【解决方案1】:

“错误:不兼容的类型:JFrame 无法转换为 int”

错误是不言自明的 JFrame 对象无法转换为 int

int n = (int) main;//This line is the root cause 

正如JavaDocs 所说

开关适用于 byte、short、char 和 int 原始数据 类型。它也适用于枚举类型(在枚举类型中讨论), String 类,以及一些包装某些特定类的特殊类 原始类型:Character、Byte、Short 和 Integer(在 数字和字符串)。

所以 Switch 不能与 JFrame 一起使用

【讨论】:

  • 是的,你会得到因为 Switch case 不支持 JFrame Object ,阅读我更新的答案
  • 我强烈建议你阅读Event Handling
【解决方案2】:

正如其他答案和 cmets 所述,您无法从帧中获取整数。 Swing 根本不是那样工作的。

相反,您可以向按钮添加“动作侦听器”。这些是在按下按钮时调用的简单“回调”。

这可以像这样在 MovieGenerator 构造函数中完成

titleButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        int titleButton = JOptionPane.showOptionDialog(null, "How do you want to sort?", "Main Menu", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                new Object[] { "Alphabetically Descending", "Alphabetically Ascending", }, " ");

    }
});

或在 main 方法的外部,使用适当的 getter 或不寒而栗的字段访问。

public static void main(String[] args) throws IOException {
    MovieGenerator mov = new MovieGenerator();
    mov.getTitleButton().addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int titleButton = JOptionPane.showOptionDialog(null, "How do you want to sort?", "Main Menu", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    new Object[] { "Alphabetically Descending", "Alphabetically Ascending", }, " ");

        }
    });
    // etc
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2021-09-15
    • 2019-06-22
    • 1970-01-01
    相关资源
    最近更新 更多