2014-10-07 41 views
0

我想創建我自己的自定義按鈕,具有定義的大小和定義的顏色。 爲此,我使用Custom Component創建了一個擴展JButton的類。 不幸的是我意識到,當我覆蓋paintComponent()並在方法結束時調用super.paintComponent(g)時,它會導致顏色設置的重寫。但是,如果我不調用超級方法,該按鈕不再具有可點擊的功能。擺動創建一個定製的Jbutton問題

任何建議,如果我的代碼有什麼問題,或缺少什麼?任何建議實現我的目標?

+0

「但是,如果我沒有調用超級方法,該按鈕不再具有可點擊的功能」您能更具體地瞭解嗎?你究竟是什麼意思?你爲什麼要在你的方法的**結尾**調用super.paintComponent()?請注意,您可以調用各種設置函數來指定自定義顏色,無需擴展JButton。 – 2014-10-07 18:55:23

+1

爲了儘快獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)。 – 2014-10-07 19:17:16

+0

*「..不再具有可點擊的功能」*它可能會但不顯示焦點和激活(按下)。在任何情況下,我都傾向於使用未裝飾的按鈕,每個基本顏色有兩個或多個圖標,包括「正常圖標」,「聚焦圖標」,「按下圖標」等。比擴展按鈕更簡單。 – 2014-10-07 19:21:53

回答

2
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Shape; 
import java.awt.geom.Ellipse2D; 

import javax.swing.JButton; 
import javax.swing.JFrame; 

public class CreateRoundButton extends JButton { 
    public CreateRoundButton(String label) { 
    super(label); 
    Dimension size = getPreferredSize(); 
    size.width = size.height = Math.max(size.width,size.height); 
    setPreferredSize(size); 
    //added to remove a border of the text in jbutton 
    setFocusPainted(false); 
    setContentAreaFilled(false); 
    } 

    protected void paintComponent(Graphics g) { 
    if (getModel().isArmed()) { 
     g.setColor(Color.lightGray); 
    } else { 
     g.setColor(getBackground()); 
    } 
    g.fillOval(0, 0, getSize().width-1,getSize().height-1); 

    super.paintComponent(g); 
    } 

    protected void paintBorder(Graphics g) { 

    g.setColor(getForeground()); 
    g.drawOval(0, 0, getSize().width-1,  getSize().height-1); 
    } 

    Shape shape; 
    public boolean contains(int x, int y) { 
    if (shape == null || 
     !shape.getBounds().equals(getBounds())) { 
     shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); 
    } 
    return shape.contains(x, y); 
    } 

    public static void main(String[] args) { 
    JButton button = new CreateRoundButton("Click"); 
    button.setBackground(Color.gray); 

    JFrame frame = new JFrame(); 
    frame.getContentPane().add(button); 
    frame.getContentPane().setLayout(new FlowLayout()); 
    frame.setSize(150, 150); 
    frame.setVisible(true); 
    } 
} 

從這裏http://www.roseindia.net/tutorial/java/swing/createRoundButton.html

+0

不是文本不可見,super.paintComponent(g)應該是JButton自定義繪畫的最後一行代碼(僅適用於)。 – mKorbel 2014-10-08 06:57:06

+0

是的,我剛剛意識到。我將編輯答案。 – 2014-10-09 18:00:20

+0

:-) ........................ – mKorbel 2014-10-10 07:08:19

0

我採取這種方式嘗試爲好,但把調用的超級方法在一開始會導致文本按鈕沒有任何看得到更多! 實際上,在這兩種情況下,我都會調用超級方法,我設置的屬性已被覆蓋。

我的目標si改變了按鈕的尺寸,形狀和顏色,使其他所有Jbutton變形器都變形。 在這裏我的代碼。



    protected void paintComponent(Graphics g) { 
     Graphics2D g2d = (Graphics2D) g; 
     if (getModel().isArmed()) { 
      g2d.setColor(Color.LIGHT_GRAY); 
     } else { 
      g2d.setColor(this.colorwell); 
     } 
     g2d.fillOval(0, 0, getSize().width-1,getSize().height-1); 
     g.dispose(); 
     } 

     protected void paintBorder(Graphics g) { 
     g.setColor(getForeground()); 
     g.drawOval(0, 0, getSize().width-1,  getSize().height-1); 
     }