单击按钮时更改Java中的窗口[关闭]

问题描述 投票:-1回答:1

我正在尝试在程序中单击按钮时更改窗口,但是我已经遍历了无数有关如何使用动作侦听器和动作事件的教程,但我仍然充满了错误。到目前为止,我的程序可以按预期运行,但是我正在尝试使当前窗口成为欢迎菜单,并且该按钮指向另一个窗口。这是下面的代码:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Button;
import javax.swing.JFrame;



public class Game extends JFrame
{
    private static final int width = 800;
    private static final int height = 800;
    public Game()
    {
        JFrame window = new JFrame("Jumping Man");
        window.setSize(width, height);
        window.setVisible(true);
        window.setBackground(Color.BLACK);
        window.setResizable(false);
        Button button = new Button("Click here");
        window.setLayout( new GridBagLayout() );
        window.add(button, new GridBagConstraints());
        button.setSize(100, 100);

        window.add(button);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
      public void actionPerformed(ActionEvent e) 
      {


      }
}

我的跑步者文件在这里:

import javax.swing.JFrame;
public class GameRunner extends JFrame
{
    public static void game()
    {
        Game game = new Game();
    }

    public static void main(String[] args)
    {
        game();
    }
}
java button actionlistener
1个回答
0
投票

一个最小的例子。最初显示带有JFrame的小JButton。当您单击JButton时,JFrame消失,并显示另一个JFrame。顺便说一句,我推荐教程Creating a GUI With JFC/Swing

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

public class Game implements ActionListener, Runnable {
    private JFrame welcomeWindow;
    private JFrame gameWindow;

    @Override // java.awt.ActionListener
    public void actionPerformed(ActionEvent event) {
        gameWindow = new JFrame("Game");
        gameWindow.setSize(400, 450);
        gameWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        welcomeWindow.dispose();
        gameWindow.setLocationRelativeTo(null);
        gameWindow.setVisible(true);
    }

    @Override // java.lang.Runnable
    public void run() {
        createAndShowWelcomeWindow();
    }

    private void createAndShowWelcomeWindow() {
        welcomeWindow = new JFrame("Welcome");
        JLabel label = new JLabel("<html>Welcome to my game!<p>Press 'Play'");
        welcomeWindow.add(label, BorderLayout.PAGE_START);
        JButton playButton = new JButton("Play");
        playButton.addActionListener(this);
        welcomeWindow.add(playButton, BorderLayout.PAGE_END);
        welcomeWindow.pack();
        welcomeWindow.setLocationRelativeTo(null);
        welcomeWindow.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Game());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.