有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在JFrame中获取和更改值

我正在构建一个简单的蛇游戏,我想在游戏开始之前改变游戏菜单(JMenuBar)中蛇的速度(变量“延迟”)

我在更改此变量时遇到问题。我无法在菜单中更改其值,它始终等于设置值(100)。我尝试了很多东西,但都不管用。我快做完了。这是我的密码:

package Snake;

public class Menu {

PlayGame playGame = new PlayGame();
Help help = new Help();
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();

    ImageIcon iconPlay = new ImageIcon("downmouth.png");
    ImageIcon iconHelp = new ImageIcon("help.png");
    ImageIcon iconAbout = new ImageIcon("about.png");
    ImageIcon iconExit = new ImageIcon("exit.png");

    JMenu fileMenu = new JMenu("File");
    JMenu settingsMenu = new JMenu("Settings");
    JMenu helpMenu = new JMenu("Help");

    JMenu speedMenu = new JMenu("Speed");
    JMenu boardMenu = new JMenu("Board");

    JMenuItem faster = new JMenuItem("Faster");
    JMenuItem normal = new JMenuItem("Normal");
    JMenuItem slower = new JMenuItem("Slower");
    JMenuItem theSlowest = new JMenuItem("The Slowest");

    JMenuItem noWall = new JMenuItem("Without the wall");
    JMenuItem wall = new JMenuItem("With wall");

    JMenuItem play = new JMenuItem("Play", iconPlay);

    play.addActionListener((ActionEvent event) -> {
        playGame.openGame();
        playGame.beVisible();
    });

    JMenuItem exit = new JMenuItem("Exit", iconExit);

    exit.addActionListener((ActionEvent event) -> {
        System.exit(0);
    });

    JMenuItem instructions = new JMenuItem("Instructions", iconHelp);

    instructions.addActionListener((ActionEvent event) -> {
        help.instructions();
    });

    JMenuItem about = new JMenuItem("About", iconAbout);

    about.addActionListener((ActionEvent event) -> {
        help.about();
    });

    JMenuItem theFastest = new JMenuItem("The Fastest");

    theFastest.addActionListener((ActionEvent event) -> {
        // ???????
    });

    // and rest of speed variables...

    speedMenu.add(theFastest);
    speedMenu.addSeparator();
    speedMenu.add(faster);
    speedMenu.addSeparator();
    speedMenu.add(normal);
    speedMenu.addSeparator();
    speedMenu.add(slower);
    speedMenu.addSeparator();
    speedMenu.add(theSlowest);

    boardMenu.add(noWall);
    boardMenu.addSeparator();
    boardMenu.add(wall);

    fileMenu.add(play);
    fileMenu.addSeparator();
    fileMenu.add(exit);
    settingsMenu.add(speedMenu);
    settingsMenu.addSeparator();
    settingsMenu.add(boardMenu);
    helpMenu.add(instructions);
    helpMenu.addSeparator();
    helpMenu.add(about);

    menubar.add(fileMenu);
    menubar.add(settingsMenu);
    menubar.add(helpMenu);

    menu.setJMenuBar(menubar);
}}

package Snake;

public class Gameplay extends Paint implements KeyListener, ActionListener {

private Timer timer;
private int q = 0;
private int delay = 100;

public Gameplay() {

    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    timer = new Timer(delay, this);//????????
    timer.start();
}

在这个类(游戏性)中有更多需要定时器的代码

package Snake;

public class PlayGame extends Gameplay 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);

}

@Override
public void beVisible() {
    f2.setVisible(true);
}}

共 (0) 个答案