Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取和更改JFrame中的值_Java_Swing_Jframe - Fatal编程技术网

Java 获取和更改JFrame中的值

Java 获取和更改JFrame中的值,java,swing,jframe,Java,Swing,Jframe,我正在构建一个简单的蛇游戏,我想在游戏开始之前改变游戏菜单(JMenuBar)中蛇的速度(变量“延迟”) 我在更改此变量时遇到问题。我无法在菜单中更改其值,它始终等于设置值(100)。我试过很多东西,但都不管用。我快做完了。这是我的密码: package Snake; public class Menu { PlayGame playGame = new PlayGame(); Help help = new Help(); JFrame menu = new JFrame(); void

我正在构建一个简单的蛇游戏,我想在游戏开始之前改变游戏菜单(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);
}}

根据我所看到的,你需要某种方法来改变
定时器的
延迟
,因此他的建议是你在
游戏
中需要一种方法来获取新的值,然后你需要通过方法将其提供给
定时器
,当然,但是如何。。。。我不知道