我对JOptionPane有疑问。我创建了构造函数,我在其中定义了JOptionPane。我需要在另一个创建JFrame的类中创建该类的对象。问题是JOptionPane在JFrame之前打开,我希望它恰恰相反。
每当我用JOptionPane创建一个类的对象时,它就会出现。
我只是想让JOptionPane显示并更改"延迟"只有当我点击"播放"在我的JMenu上。
public class Gameplay extends Paint implements KeyListener, ActionListener {
private Timer timer;
private int q = 0;
private int delay;
public Gameplay() {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay=Integer.parseInt(JOptionPane.showInputDialog("set Speed")), this);
timer.start();
}
public class PlayGame implements IbeVisible {
JFrame f2 = new JFrame("Snake");
Gameplay gameplay = new Gameplay();
public void openGame() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
f2.setSize(900, 700);
f2.setResizable(false);
f2.setLocation(dim.width / 2 - f2.getWidth() / 2, dim.height / 2 - f2.getHeight() / 2);
f2.add(gameplay);
}
public void beVisible() {
f2.setVisible(true);
}
public class Menu {
PlayGame playGame = new PlayGame();
JFrame menu = new JFrame();
void create() throws IOException {
createMenuBar();
menu.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("logo.png")))));
menu.setTitle("Menu");
menu.setVisible(true);
menu.setSize(355, 400);
menu.setResizable(false);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createMenuBar() {
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem play = new JMenuItem("Play");
play.addActionListener((ActionEvent event) -> {
playGame.openGame();
playGame.beVisible();
});