簡體   English   中英

向JButton類添加自定義方法

[英]Adding Custom Method to JButton class

我有一個Java應用程序,我想將按鈕設置為“活動”或“不活動”(也可能是一種懸停方法)。

我想實現的代碼:

//Home Tab - Active by default
home = new TabButton();
home.setSize(new Dimension(tabWidth, tabHeight));
home.setFont(getLauncherFont(34));
home.setForeground(Color.white);
home.setText("HOME");
home.setBounds(160, 0, tabWidth, tabHeight);
home.setActive(); --> This Method is what I would like to create

我已經有一個為選項卡創建JButton的類:

package com.anarcist.minemodloaderv1.skin.components;

import java.awt.Color;
import javax.swing.JButton;

/**
 *
 * @author anarcist
 */
public class TabButton extends JButton {
    public TabButton() {
        this.setBorderPainted(false);
        this.setFocusPainted(false);
        this.setContentAreaFilled(true);
        this.setBackground(Color.blue);
    }
}

我研究了抽象類。 但是我的TabButton類已經擴展了JButton。

我想要一個這樣的方法:

public void setActive(){
    this.setBackground(Color.red);
    //Any other changes a want to make regularly
}

可以像home.setActive();這樣簡單地實現home.setActive();

我想我的問題是:是否很容易實現我要尋找的東西,還是我必須走很長的路才能每次都手動設置所有屬性?

您在帖子中描述的內容可以這樣完成:

package com.anarcist.minemodloaderv1.skin.components;

import java.awt.Color;
import javax.swing.JButton;

/**
 *
 * @author anarcist
 */
public class TabButton extends JButton {

    public TabButton() {// initialize

        this.setBorderPainted(false);
        this.setFocusPainted(false);
        this.setContentAreaFilled(true);
        this.setBackground(Color.blue);

    }

    // add your own methods or override JButton methods
    public void setActive(){
       //Add code
       //example: setEnabled(true);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM