作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我一直在想办法如何使这个工作,但我找不到,所以我决定寻求帮助,下面是我的代码的样子,我想做的是显示主菜单在用户拒绝继续教程并且我尝试
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);
}
}
最佳答案
您当前的代码有几个问题。
通过在 main
方法中创建 JFrame
容器,您可以防止在实例化 MainMenu
时显示 UI - 也就是说,只有当 JVM 调用 main
方法时,UI 才会显示任何内容。为了解决这个问题,我已将 JFrame
实例化/设置移至 MainMenu
的构造函数中。
在 MainMenu
类中,mainMenu
JPanel
永远不会被实例化。这意味着您当前的代码实际上并未在 JFrame 上绘制任何内容 - 您需要实例化 mainMenu
并将 GUI 控件添加到 mainMenu
。
下面的代码解决了这两个问题。
public class MainMenu
{
private JButton exitButton;
private JPanel mainMenu;
private JButton startButton;
private JButton historyButton;
public MainMenu()
{
JFrame frame = new JFrame("Main Menu");
///// mainMenu IS ALWAYS NULL WITHOUT THE NEXT LINE!!!!
this.mainMenu = new JPanel();
frame.setContentPane(this.mainMenu);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setMinimumSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
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)
{
new MainMenu();
}
关于java - 如何从另一个 Java 类调用 GUI 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58957183/
我是一名优秀的程序员,十分优秀!