2015-10-17 146 views
0

我有一个Java应用程序,我想将按钮格式化为Active或Inactive(也可能是悬停方法)。向JButton类添加自定义方法

,因为我想实现它的代码:

//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();

我的问题,我想实现的是:它是很容易的实现我所期待的,或我是否必须每次都手动设置所有属性?

+0

我不知道是什么ü意味着,但你可以用U希望 – Zion

+0

请你更加精确的每一个方法延长您TabButton类关于你想要什么?我不太明白。 –

回答

2

你在帖子已经描述了什么可以做这样的:

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); 
    } 
} 
+0

应该这样实现吗? 'home.setActive()' 我已经测试过了,我得到一个错误,说该方法不存在。 –

+0

是的,它应该像那样实现,你使用的是IDE吗? –

+0

NetBeans。但如果我运行我得到错误'线程中的异常“主”java.lang.RuntimeException:不可编译的源代码 - 错误的sym类型:javax.swing.JButton.setActive' –