3
我不擅长这一点,我希望得到一些理解这个问题的人的更多帮助。JPanel的自定义绘画
所以这里是交易。在我的应用程序中有背景JPanel与图像绘制在它上面。然后有一个小的JPanel,我正在试图创建自定义绘画。我想让JPanel具有圆角和半透明背景,所以我修改了paintComponent方法来填充半透明的圆角矩形。但是当我像JComboBox那样放置组件时,项目列表出现,并且我点击其他地方关闭它JPanel以原始方式绘制它自己,使它半透明,但是用原始灰色背景颜色绘制小矩形。我发现它必须在其parrent或paintChildren上调用paintComponent,但我不知道如何组织这些方法或将它们放在哪里。我也有问题,透明的颜色相互重叠。
下面是一个例子代码:
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);
}
}
我希望有些会帮我:-)感谢
编辑:我发现的JComboBox(和它的弹出菜单)平如果弹出式菜单与包含JComboBox的JPanel外部重叠且具有自定义paintComponent方法,则会正确地执行此操作。
耶稣,我没有尝试这个,因为我认为RoundedPanel不会画画可言,我看不到它:-)非常感谢你,先生! – Martin 2010-09-17 13:34:36