我已经设法为我的程序构建初始启动屏幕(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;
}
}
}
}
答案 0 :(得分:0)
"错误:不兼容的类型:JFrame无法转换为int"
错误是不言自明的JFrame对象无法转换为int
int n = (int) main;//This line is the root cause
和As JavaDocs说
开关使用byte,short,char和int原始数据 类型。它也适用于枚举类型(在枚举类型中讨论), String类,以及一些特定的包装类 原始类型:字符,字节,短整数和整数(在 数字和字符串)。
所以Switch不适用于JFrame
答案 1 :(得分:0)
正如其他答案和评论所述,您无法从一个帧中获取整数。 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", }, " ");
}
});
或在主方法之外使用适当的getter或shudder,field access。
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
}