所以我一直在寻找如何使它工作的方法,但是我找不到,所以我决定寻求帮助,以下是我的代码的样子,我想做的是在之后显示主菜单用户拒绝继续学习本教程,而我试图[]
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Login { public Login() { String userName; int option; //This will ask user to input the username userName = JOptionPane.showInputDialog(null,"Please enter your name","Welcome", JOptionPane.INFORMATION_MESSAGE); //Display option option =JOptionPane.showOptionDialog(null, "Welcome " + userName + "\n\nWould you like to have a tutorial about this game?", "Welcome", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); //Ok to continue to the tutorial if(option == JOptionPane.OK_OPTION) { //Call the tutorial class }
这是代码出错的地方,我试图用不同的方法解决
else //If select cancel will proceed to the Main menu { //This is the part I can't figure it out, it display different errors when I try different ways that I searched from website MainMenu MainMenuGUI = new MainMenu(); } } }
这是我的主菜单代码
import javax.swing.*;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class MainMenu {
private JButton exitButton;
private JPanel MainMenu;
private JButton startButton;
private JButton historyButton;
public MainMenu() {
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int exitButton = JOptionPane.YES_NO_OPTION;
exitButton = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Warning", JOptionPane.YES_NO_OPTION);
if (exitButton == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
}
//Main Menu GUI setup
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
frame.setContentPane(new MainMenu().MainMenu);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setMinimumSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
}
}
所以我一直在寻找如何使它工作的方法,但是我找不到,所以我决定寻求帮助,以下是我的代码的样子,我想做的是在之后显示主菜单用户...
您当前的代码有两个问题。
摆脱MainMenu类中的public static void main(String args[])
方法。您仅在Java程序中使用一次main方法。而是创建一个类似public void initUI()
的方法,并将您拥有的所有代码放入main()
方法中。
并且在您的Login
类中,在您致电MainMenu MainMenuGUI = new MainMenu();
之后