swing自带的弹出菜单不太美观,在需要使用右键弹出菜单的时候怎么创建一个美观的右键菜单呢?
直接贴上代码。
先是边界类,他将绘制灰色的边界:
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.Border;
//自定义边界类,将绘制灰色矩形边框
public class GrayRectBorder implements Border {
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
g.setColor(new Color(150, 150, 150));
g.drawRect(0, 0, w - 1, h - 1);
}
public Insets getBorderInsets(Component c) {
return new Insets(1, 1, 1, 1);
}
public boolean isBorderOpaque() {
return true;
}
}
接着就是自定义的PopupMenu
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class MyPopupMenu extends JPopupMenu {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyPopupMenu() {
setBorder(new GrayRectBorder());
JMenuItem m1 = new MyMenuItem("邀请好友");
JMenuItem m2 = new MyMenuItem("删除");
JMenuItem m3 = new MyMenuItem("发送消息");
add(m1);
add(m2);
add(m3);
setBackground(new Color(241, 241, 241));
}
public Dimension getPreferredSize() {
return new Dimension(182, 79);
}
public class MyMenuItem extends JMenuItem {
boolean mouseIn = false;
String name;
/**
*
*/
private static final long serialVersionUID = 1L;
public MyMenuItem() {
}
public MyMenuItem(String name) {
this.name = name;
setBorder(null); // 覆盖父类的初始化
addMouseListener(new MouseListener() {
public void mouseExited(MouseEvent e) {
mouseIn = false;
repaint();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
mouseIn = true;
repaint();
}
public void mousePressed(MouseEvent e) {
}
});
}
public Dimension getPreferredSize() { // 调整为期望的合适大小
return new Dimension(180, 25);
}
public void paintComponent(Graphics g) {
removeAll();
g.setColor(new Color(241, 241, 241));
g.fillRect(0, 0, getWidth(), getHeight());
if (mouseIn) {
g.setColor(new Color(234, 237, 244));
g.fillRoundRect(2, 1, getWidth() - 4, getHeight() - 3, 3, 3);
g.setColor(new Color(189, 205, 231));
g.drawRoundRect(2, 1, getWidth() - 4, getHeight() - 3, 3, 3);
}
g.setColor(new Color(227, 227, 227));
g.drawLine(30, 0, 30, getHeight());
g.setColor(new Color(255, 255, 255));
g.drawLine(31, 0, 31, getHeight());
if (mouseIn) {
g.setColor(new Color(189, 205, 231));
g.drawLine(30, 1, 31, 1);
g.drawLine(30, getHeight() - 2, 31, getHeight() - 2);
}
JLabel l = new JLabel(name);
l.setFont(new Font("微软雅黑", Font.PLAIN, 12));
l.setBounds(35, 0, 100, getHeight());
l.setForeground(new Color(44, 44, 44));
add(l);
}
}
}
运行的效果如下: