2009-04-22 40 views
9

Tôi đang cố gắng tạo vòng JButton ở tọa độ (x, y) của (150,210). Tôi muốn nút là hình bầu dục có kích thước (40,40). Và các nút nền màu đỏ. Nó không cần văn bản, vì vậy tôi sẽ nhận được một nút để kích thước bất cứ điều gì tôi muốn, phải không?Tạo một nút tròn

Tôi đang sử dụng một bảng điều khiển, và tôi đặt setLayout để null:

setLayout(null) 

Tôi đã kiểm tra Google, và nó mang lại cho tôi đây. Tôi có thể làm cái này như thế nào?

Trả lời

12

JDC Tech Tips: 26 tháng tám 1999: Creating Round Swing Buttons

import java.awt.*; 
import java.awt.geom.*; 
import javax.swing.*; 

public class RoundButton extends JButton { 
    public RoundButton(String label) { 
    super(label); 

// These statements enlarge the button so that it 
// becomes a circle rather than an oval. 
    Dimension size = getPreferredSize(); 
    size.width = size.height = Math.max(size.width, 
     size.height); 
    setPreferredSize(size); 

// This call causes the JButton not to paint 
    // the background. 
// This allows us to paint a round background. 
    setContentAreaFilled(false); 
    } 

// Paint the round background and label. 
    protected void paintComponent(Graphics g) { 
    if (getModel().isArmed()) { 
// You might want to make the highlight color 
    // a property of the RoundButton class. 
     g.setColor(Color.lightGray); 
    } else { 
     g.setColor(getBackground()); 
    } 
    g.fillOval(0, 0, getSize().width-1, 
     getSize().height-1); 

// This call will paint the label and the 
    // focus rectangle. 
    super.paintComponent(g); 
    } 

// Paint the border of the button using a simple stroke. 
    protected void paintBorder(Graphics g) { 
    g.setColor(getForeground()); 
    g.drawOval(0, 0, getSize().width-1, 
     getSize().height-1); 
    } 

// Hit detection. 
    Shape shape; 
    public boolean contains(int x, int y) { 
// If the button has changed size, 
    // make a new shape object. 
    if (shape == null || 
     !shape.getBounds().equals(getBounds())) { 
     shape = new Ellipse2D.Float(0, 0, 
     getWidth(), getHeight()); 
    } 
    return shape.contains(x, y); 
    } 

// Test routine. 
    public static void main(String[] args) { 
// Create a button with the label "Jackpot". 
    JButton button = new RoundButton("Jackpot"); 
    button.setBackground(Color.green); 

// Create a frame in which to show the button. 
    JFrame frame = new JFrame(); 
    frame.getContentPane().setBackground(Color.yellow); 
    frame.getContentPane().add(button); 
    frame.getContentPane().setLayout(new FlowLayout()); 
    frame.setSize(150, 150); 
    frame.setVisible(true); 
    } 
} 
+2

Tính năng này không hoạt động trên OS X 10.7.5. Nó xuất hiện như một nút hình vuông lớn với một vòng tròn bên trong nó. Điều này hoạt động tốt hơn nhiều: http://java-swing-tips.blogspot.com/2008/07/create-round-image-jbutton.html – d512

0

Nếu thực hiện một nút tròn sau cách này, tôi nghĩ rằng nó quá phức tạp. Có lẽ bạn có thể tạo OvalButton theo một cách khác.

+2

nếu bạn không quan tâm đến giới hạn thực sự có thể nhấp của hình bầu dục (giữ nó để hình chữ nhật chứa), sau đó sử dụng hình ảnh png và setContentAreaFilled (sai); công trinh –

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