我可以在Java中的绘制矩形上放置按钮吗? [英] Can I put buttons on a drawn rectangle in Java?

查看:74
本文介绍了我可以在Java中的绘制矩形上放置按钮吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的游戏创建并实现游戏结束"屏幕.是否可以通过创建一个额外的类"GameOver"来做到这一点?它具有一个新的绘制对象,该对象仅绘制一个设置在屏幕中间的黑色矩形,并具有一些按钮,例如"new game".在绘制的矩形上?

I want to create and implement a Game Over screen to my game. Is it possible to just do this by creating an extra class "GameOver" which has a new draw object which just draws a black rectangle set to the middle of the screen and to have some buttons like "new game" ON the drawn rectangle?

或者最好的方法是什么?

Or what is the best way to go?

我的游戏正在JFrame上运行.

My game is running on a JFrame.

如果您想查看特定的代码,请告诉我,我有很多类,因此很难显示所有内容,并且我认为此问题实际上不是必需的.

If you want to see specific code, let me know, I got a lot of classes and so it would be difficult to show everything and I thought its not actually necessary for this question.

推荐答案

以下是您可能想做的事的一个示例.您应该查看 Java教程了解更多详细信息.尤其是布局管理器自定义绘画用于处理事件的侦听器.我使用了一些匿名类,其中显式类可能更受欢迎.

Here is an example of what you might want to do. You should check The Java Tutorials for more details. Especially layout managers, custom painting, and listeners for handling events. I used some anonymous classes where explicit ones would probably be preferred.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GameScreen {
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new GameScreen().start());
    }
    
    public void start() {
        
        // this was quick and dirty.  Best to have separate classes with proper overrides, etc.
        JPanel panel = new JPanel() {
            public Dimension getPreferredSize() {
                return new Dimension(300, 130);
            }
        };
        panel.setLayout(new FlowLayout());
        JFrame frame = new JFrame("Game") {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(500, 500);
            }
        };
        
        // shared attributes
        panel.setBackground(Color.black);
        JTextField text = new JTextField();
        Color orange = Color.orange;
        Color maroon = new Color(176, 48, 96);
        text.setPreferredSize(new Dimension(270, 80));
        Font font = new Font("Ariel", Font.BOLD, 30);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;  // in the middle
        gbc.gridy = 1;  // of the screen
        
        // game over stuff
        text.setEditable(false);
        text.setForeground(orange);
        text.setBackground(maroon);
        text.setFont(font);
        text.setText("     GAME OVER!");
        JButton quit = new JButton("Quit");
        quit.addActionListener(ae -> System.exit(0)); // more complicated code should an
                                                      // inner class for this.
        
        //new game stuff
        JTextField newGameText = new JTextField();
        newGameText.setText("READY TO RUMBLE!");
        newGameText.setFont(font);
        newGameText.setForeground(orange);
        newGameText.setBackground(maroon);
        JButton newGame = new JButton("New Game");
        
        
        newGame.addActionListener(ae -> {
            panel.setVisible(false);
            frame.add(newGameText, gbc);
        });
        
        panel.add(text);
        panel.add(quit);
        panel.add(newGame);
        
        frame.setLayout(new GridBagLayout());
        frame.add(panel, gbc);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.repaint();
    }
    
}

这篇关于我可以在Java中的绘制矩形上放置按钮吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆