2016-02-25 87 views
0

所以我有一些代碼來改變一個按鈕的背景顏色,但是當我使用代碼時,它會設置背景顏色和邊框顏色。用setBackground()設置JButton邊框?

Picture

有沒有辦法不使這種情況發生?

謝謝!

代碼:

public void highlight(ArrayList<JButton> buttons){ 
    for (JButton j : buttons) { 
    j.setBorder(new JButton().getBorder()); 
    j.setBackground(Color.GREEN); 
    j.setForeground(Color.WHITE); 
    j.setOpaque(true); 
    j.setBorderPainted(false); 
    j.setFocusPainted(false); 
    j.setBorderPainted(false); 
    } 
} 
+0

好吧,你的期望是什麼,如何代碼不符合他們(即你想填補按鈕內的區域,仍然有它的邊界......我可能會添加將是幾乎不可能沒有很多工作) – MadProgrammer

+0

我想讓它填充按鈕,但仍然有一個邊框@MadProgrammer – TheGuyWhoCodes

+0

好吧,任務(幾乎)不可能。內容區域由外觀委託人填充,通常會忽略所有顏色屬性。嘗試爲所有預期的平臺生成一個外觀和感覺委託,這將使您能夠實現預期的結果,這非常複雜。您可以編寫自己的自定義外觀並感受代表,儘管如此,但這仍然有點工作要實現 – MadProgrammer

回答

2

好了,這是被攻擊的版本,是基於要維持目前的「外觀和感覺」按鈕,但要使用不同的填充想法顏色

這只是適用在按鈕 「高亮」 顏色...

Highlighted buttons

import java.awt.AlphaComposite; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GraphicsConfiguration; 
import java.awt.GraphicsEnvironment; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.RenderingHints; 
import java.awt.Transparency; 
import java.awt.image.BufferedImage; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import org.kaizen.core.ui.ImageUtilities; 

public class Main { 

    public static void main(String[] args) { 
     new Main(); 
    } 

    public Main() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      HighlightButton btn = new HighlightButton("Help"); 
      btn.setMargin(new Insets(20, 20, 20, 20)); 
      btn.setHighlight(new Color(255, 0, 0, 64)); 
      add(btn); 

      btn = new HighlightButton("Help"); 
      btn.setMargin(new Insets(20, 20, 20, 20)); 
      btn.setHighlight(new Color(0, 255, 0, 64)); 
      add(btn); 

      btn = new HighlightButton("Help"); 
      btn.setMargin(new Insets(20, 20, 20, 20)); 
      btn.setHighlight(new Color(0, 0, 255, 64)); 
      add(btn); 

      btn = new HighlightButton("Help"); 
      btn.setMargin(new Insets(20, 20, 20, 20)); 
      add(btn); 
     } 

    } 

    public class HighlightButton extends JButton { 

     private Color highlight; 

     public HighlightButton() { 
      setOpaque(false); 
     } 

     public HighlightButton(String text) { 
      super(text); 
      setOpaque(false); 
     } 

     public void setHighlight(Color color) { 
      if (color != highlight) { 
       Color old = highlight; 
       this.highlight = color; 
       firePropertyChange("highlight", old, highlight); 
       repaint(); 
      } 
     } 

     public Color getHighlight() { 
      return highlight; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      Color highlight = getHighlight(); 
      if (highlight != null) { 
       BufferedImage img = createCompatibleImage(getWidth(), getHeight(), Transparency.TRANSLUCENT); 
       Graphics2D g2d = img.createGraphics(); 
       super.paintComponent(g2d); 
       g2d.dispose(); 

       BufferedImage mask = generateMask(img, getHighlight(), 1f); 
       g.drawImage(img, 0, 0, this); 
       g.drawImage(mask, 0, 0, this); 
      } else { 
       super.paintComponent(g); 
      } 
     } 

    } 

    public static BufferedImage createCompatibleImage(int width, int height, int transparency) { 
     BufferedImage image = getGraphicsConfiguration().createCompatibleImage(width, height, transparency); 
     image.coerceData(true); 
     return image; 
    } 

    public static GraphicsConfiguration getGraphicsConfiguration() { 
     return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); 
    } 

    public static BufferedImage generateMask(BufferedImage imgSource, Color color, float alpha) { 

     int imgWidth = imgSource.getWidth(); 
     int imgHeight = imgSource.getHeight(); 

     BufferedImage imgMask = createCompatibleImage(imgWidth, imgHeight, Transparency.TRANSLUCENT); 
     Graphics2D g2 = imgMask.createGraphics(); 
     applyQualityRenderingHints(g2); 

     g2.drawImage(imgSource, 0, 0, null); 
     g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, alpha)); 
     g2.setColor(color); 

     g2.fillRect(0, 0, imgSource.getWidth(), imgSource.getHeight()); 
     g2.dispose(); 

     return imgMask; 

    } 

    public static void applyQualityRenderingHints(Graphics2D g2d) { 
     g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
     g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 
     g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 
     g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
     g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 
    } 

} 

基本上,這是做什麼的,是繪製一個「蒙面」的圖像,在按鈕上方突出顯示顏色。這點很重要。顏色的alpha值越高,看到文本的可能性就越小。

我沒有在Windows上測試過,所以我不能保證結果。

內容填充是由外觀和感覺代表執行的,並且通常會忽略該類的顏色屬性(是的,我知道,太棒了),所以如果您想嘗試做一些更健壯的事情,那麼您需要定義你自己的外觀和感覺,並接管繪畫過程,而不是簡單的任務。

當然,您可以放棄外觀和感覺委託使用的內容填充和邊框,並繪製自己的圖案(覆蓋paintComponent方法),但這樣做不會利用外觀和感覺設置,所以這就是一個平衡的行爲,你需要決定

+0

感謝您的幫助!我迫不及待地想要實現這一點 – TheGuyWhoCodes