【发布时间】:2021-02-26 08:29:58
【问题描述】:
我正在尝试创建一个具有不同颜色和圆形边框的自定义 JPopupMenu。我尝试了以下代码,但 PopupMenu 的外观没有任何变化。
JButton button = new JButton("Test");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
TPopupMenu popup = new TPopupMenu();
JMenuItem item1 = new JMenuItem("Item 1");
JMenuItem item2 = new JMenuItem("Item 2");
popup.add(item1);
popup.add(item2);
}
}
自定义弹出菜单
public class TPopupMenu extends JPopupMenu{
public TPopupMenu(){
super();
super.setOpaque(false);
init();
}
private void init(){
setBackground(Color.green);
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.pink);
int w = getWidth();
int h = getHeight();
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTILIAS_ON);
g2.fillRoundRect(0,0,w-1, h-1, 10, 10);
g2.drawRoundRect(0,0,w-1, h-1, 10, 10);
g2.setBackground(Color.red);
g2.setColor(Color.green);
}
}
这就是我希望圆形弹出菜单的样子:
我在我的paintComponent 方法中做错了吗?
【问题讨论】:
标签: java jpopupmenu