如何在 Java 中创建自定义 JButton 类? [英] How to create a custom JButton class in Java?

查看:34
本文介绍了如何在 Java 中创建自定义 JButton 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

恐怕我没有任何代码,因为我真的不知道自己在做什么.我正在尝试通过创建一个基本游戏来学习一些 Java 并想要制作一个菜单.我想要做的是有一个带有自定义 JButton 的类(基本上是一个特定大小的 JButton 和背景上的图片,特殊格式等),它可以从另一个类调用,并在调用时给出自定义文本.我的问题是,如何创建可以在外部调用的自定义按钮?

I'm afraid I don't have any code because I don't really know what I'm doing. I'm trying to learn some Java by creating a basic game and want to make a menu. What I want to do is have a class with a custom JButton (basically a JButton of a certain size and a picture on the background, formatted specially, etc.) which can be called from another class and given custom text when it is called. My question is, how do I create the custom button which can be called externally?

推荐答案

创建一个类并将其扩展到 JButton,如下所示,您可以在那里更改与 JButton 相关的所有类型.

create a class and extend it to JButton like below and there you can change all sort of things that has to do with JButton.

import javax.swing.JButton;

public class CustomJButton extends JButton {

    public CustomJButton() {
        this.setText("Custom JButton");
        // initialize
    }

    // add your own methods or override JButton methods
    public void myFunc(){

    }
}

这篇关于如何在 Java 中创建自定义 JButton 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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