2011-08-19 23 views

Trả lời

18

Bạn có thể ghi đè lên các phương pháp paintComponent của JButton dụ và vẽ đối tượng Graphics của nó với một trong các lớp sau đó thực hiện các giao diện Paint:


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

từ nên L & F nhạy cảm, nhưng vey đẹp 1 – mKorbel

+1

giải pháp tốt nhất tôi đã tìm thấy trên SO –

5

Một cải tiến ít hơn MRE câu trả lời:

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

Làm thế nào người ta sẽ thực hiện điều này với một nút đó đã là một phần của một giao diện? Tôi có cần phải thay đổi người nghe hành động chút nào nếu tôi đã thêm nút này vào các nút không? Điều đó có thể không? Hoặc, câu hỏi hay hơn; nó là một câu hỏi hợp lý để hỏi ngay từ đầu? –

+0

Nếu GUI đã có nút, phải có một số mã tạo nút đó (JButton b = new JButton ("bất cứ điều gì")). Để thay thế nút mặc định, bạn cần phải tạo một nút JGradientButton (JButton b = new JGradientButton ("bất cứ điều gì")) và thiết lập màu nền cho những gì bạn thích (b.setBackground (.. somecolor ...)). Mã còn lại trong GUI xử lý nút phải được giữ nguyên – luca

Các vấn đề liên quan