[英]Custom painting of JPanel
I'm not very good at this and I hope to get some help from people who understands the issue a lot more that I do. 我不是很擅长这一点,我希望从那些了解这个问题的人那里得到一些帮助。
So here's the deal. 所以这是交易。 In my application there is background JPanel with Image drawn over it.
在我的应用程序中,有背景JPanel,上面有Image。 Then there is a small JPanel which I'm trying to create custom painting for.
然后有一个小JPanel,我正在尝试为其创建自定义绘画。 I wanted to have JPanel with rounded corners and semi-transparent background so I modified paintComponent method to fill semi-transparent rounded rectangle.
我希望JPanel具有圆角和半透明背景,因此我修改了paintComponent方法以填充半透明的圆角矩形。 But when I place components inside like say JComboBox, the list of items appears and I click somewhere else to close it JPanel paints itself in original way making it semitransparent all around but with small rectangle painted with original grey background color.
但是当我把组件放在里面时就像说JComboBox一样,项目列表出现了,我点击其他地方关闭它JPanel以原始的方式绘制自己,使其半透明,但是用原始灰色背景颜色绘制的小矩形。 I see that it has to do something with invoking paintComponent on its parrent or paintChildren but I don't know how to organize those methods or where to put them.
我看到它必须在其parrent或paintChildren上调用paintComponent,但我不知道如何组织这些方法或在哪里放置它们。 I also have proble with transparent colors overlaping each other.
我也有透明色相互叠加的问题。
Here is an example source code: 这是一个示例源代码:
public class RoundedPanel extends JPanel {
private final int radius;
public RoundedPanel(int cornerRadius) {
radius=cornerRadius;
}
public void paintComponent(Graphics g) {
Color bg = getBackground();
g.setColor(new Color(bg.getRed(),bg.getGreen(),bg.getBlue(),40));
g.fillRoundRect(0,0, getWidth()-1, getHeight()-1, radius, radius);
g.setColor(new Color(0,0,0,70));
g.drawRoundRect(0,0, getWidth()-1, getHeight()-1, radius, radius);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 300);
frame.setLocation(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = new JPanel();
JPanel wl = new JPanel();
JPanel el = new JPanel();
JPanel sl = new JPanel();
JPanel nl = new JPanel();
RoundedPanel rp = new RoundedPanel(50);
JComboBox combobox = new JComboBox();
frame.setContentPane(content);
content.setBackground(Color.red);
content.setLayout(new BorderLayout());
wl.add(new JButton("west"));
el.add(new JButton("east"));
sl.add(new JButton("south"));
nl.add(new JButton("north"));
content.add(wl,BorderLayout.WEST);
content.add(el,BorderLayout.EAST);
content.add(nl,BorderLayout.NORTH);
content.add(sl,BorderLayout.SOUTH);
content.add(rp,BorderLayout.CENTER);
rp.setBackground(Color.BLACK);
combobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Třída 1.B", "Třída 1.C", "Třída 2.C" }));
rp.add(combobox);
frame.setVisible(true);
}
}
I hope some of will help me out :-) thanks 我希望有些人会帮助我:-)谢谢
EDIT: I found out that JComboBox (and its pop-up menu) draws correctly if pop-up menu overlaps outside the JPanel that contains JComboBox and has the custom paintComponent method. 编辑:我发现如果弹出菜单在包含JComboBox的JPanel外部并且具有自定义paintComponent方法,则JComboBox(及其弹出菜单)会正确绘制。
Try this: 尝试这个:
RoundedPanel rp = new RoundedPanel(50);
rp.setOpaque(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.