2015-10-17 96 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' –

相關問題