Bạn phải đặt setOpaque (true) thành true khác nền sau sẽ không được vẽ lên biểu mẫu. Tôi nghĩ rằng từ đọc rằng nếu nó không được thiết lập là đúng là nó sẽ vẽ một số hoặc không phải bất kỳ điểm ảnh nào của nó vào biểu mẫu. Nền là minh bạch theo mặc định mà có vẻ kỳ lạ với tôi ít nhất nhưng trong cách lập trình bạn phải đặt nó thành true như hình dưới đây.
JLabel lb = new JLabel("Test");
lb.setBackground(Color.red);
lb.setOpaque(true); <--This line of code must be set to true or otherwise the
Từ JavaDocs
setOpaque
public void setOpaque(boolean isOpaque)
If true the component paints every pixel within its bounds. Otherwise,
the component may not paint some or all of its pixels, allowing the underlying
pixels to show through.
The default value of this property is false for JComponent. However,
the default value for this property on most standard JComponent subclasses
(such as JButton and JTree) is look-and-feel dependent.
Parameters:
isOpaque - true if this component should be opaque
See Also:
isOpaque()
Nguồn
2014-05-05 20:36:14
+1 label.setOpaque (true); – bizzr3