2011-08-19 22 views

cevap

18

Sen JButton örneğinin paintComponent yöntemini geçersiz kılar ve Paint arayüzü uygulamak aşağıdaki sınıflardan biri ile Graphics nesne boya olabilir:


import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.GradientPaint; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 

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

public final class JGradientButtonDemo { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       createAndShowGUI();   
      } 
     }); 
    } 

    private static void createAndShowGUI() { 
     final JFrame frame = new JFrame("Gradient JButton Demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(new FlowLayout()); 
     frame.add(JGradientButton.newInstance()); 
     frame.setSize(new Dimension(300, 150)); // used for demonstration 
     //frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private static class JGradientButton extends JButton { 
     private JGradientButton() { 
      super("Gradient Button"); 
      setContentAreaFilled(false); 
      setFocusPainted(false); // used for demonstration 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      final Graphics2D g2 = (Graphics2D) g.create(); 
      g2.setPaint(new GradientPaint(
        new Point(0, 0), 
        Color.WHITE, 
        new Point(0, getHeight()), 
        Color.PINK.darker())); 
      g2.fillRect(0, 0, getWidth(), getHeight()); 
      g2.dispose(); 

      super.paintComponent(g); 
     } 

     public static JGradientButton newInstance() { 
      return new JGradientButton(); 
     } 
    } 
} 

enter image description here

+1

L & F duyarlı, ama SO – mKorbel

+1

iyi çözüm nasıl biri zaten GUI parçası olan bir düğmeye bu uygulayacak vey güzel olmalı çünkü? Düğmelere eklediğimde eylem dinleyicilerini hiç değiştirmem gerekecek mi? Mümkün mü? Ya da daha iyi bir soru; İlk etapta sormak makul bir soru mu? –

5

mre cevap biraz fazla bir gelişme:

enter image description here

private static final class JGradientButton extends JButton{ 
    private JGradientButton(String text){ 
     super(text); 
     setContentAreaFilled(false); 
    } 

    @Override 
    protected void paintComponent(Graphics g){ 
     Graphics2D g2 = (Graphics2D)g.create(); 
     g2.setPaint(new GradientPaint(
       new Point(0, 0), 
       getBackground(), 
       new Point(0, getHeight()/3), 
       Color.WHITE)); 
     g2.fillRect(0, 0, getWidth(), getHeight()/3); 
     g2.setPaint(new GradientPaint(
       new Point(0, getHeight()/3), 
       Color.WHITE, 
       new Point(0, getHeight()), 
       getBackground())); 
     g2.fillRect(0, getHeight()/3, getWidth(), getHeight()); 
     g2.dispose(); 

     super.paintComponent(g); 
    } 
} 
+0

üzerinde buldum 1 –

+0

Bir GUI zaten bir tuşa sahipse, bu düğmeyi oluşturan bir kod olmalıdır (JButton b = yeni JButton ("her neyse")). Varsayılan düğmeyi değiştirmek için, bunun yerine bir JGradientButton oluşturmalısınız (JButton b = yeni JGradientButton ("her neyse")) ve arka plan rengini istediğiniz gibi ayarlayabilirsiniz (b.setBackground (.. somecolor ...)). Düğmeyi işleyen GUI'deki kalan kod değiştirilmemelidir – luca