JPanel的自定义绘画 [英] 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.
这就是交易.在我的应用程序中,有一个背景JPanel,上面绘制有图像.然后是一个小的JPanel,我正在尝试为其创建自定义绘画.我想让JPanel具有圆角和半透明的背景,所以我修改了paintComponent方法来填充半透明的圆角矩形.但是,当我像JComboBox这样放置组件时,出现项目列表,然后单击其他位置以将其关闭.JPanel会以原始方式绘制自身,使其半透明,但带有以原始灰色背景色绘制的小矩形.我看到它必须在其parrent或paintChildren上调用paintComponent来做一些事情,但是我不知道如何组织这些方法或将它们放在何处.我也遇到透明色相互重叠的问题.
So here's the deal. In my application there is background JPanel with Image drawn over it. Then there is a small JPanel which I'm trying to create custom painting for. I wanted to have JPanel with rounded corners and semi-transparent background so I modified paintComponent method to fill semi-transparent rounded rectangle. 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. 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. I also have proble with transparent colors overlaping each other.
这是示例源代码:
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
我发现,如果弹出菜单与包含JComboBox并具有自定义paintComponent方法的JPanel重叠,则JComboBox(及其弹出菜单)可以正确绘制.
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.
推荐答案
尝试一下:
RoundedPanel rp = new RoundedPanel(50);
rp.setOpaque(false);
这篇关于JPanel的自定义绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!