我已经设法为程序构建了初始启动屏幕(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;
}
}
}
}
最佳答案
"error: incompatible types: JFrame cannot be converted to int"
错误是自我解释的JFrame对象无法转换为int
int n = (int) main;//This line is the root cause
和JavaDocs说
A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).
因此Switch不适用于
JFrame
关于java - 如何使我的交换机保护套在JFrame上起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28979868/