- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个具有不同颜色和圆形边框的自定义 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 方法中做错了什么?
最佳答案
您的弹出窗口永远不可见,调用 show (Component invoker, int x, int y)
方法来显示 JPopupMenu
类。
Displays the popup menu at the position x,y in the coordinate space of the component invoker...
JPopupMenu popup = new JPopupMenu();
JMenuItem item = new JMenuItem("Item");
popup.add(item);
popup.show(frame, frame.getWidth()/2, frame.getHeight()/2);
或者您也可以调用 JPopupMenu.setVisible(boolean b)
方法。
参见 JPopupMenu#show , JPopupMenu#setVisible
对于圆角边框,可以使用new LineBorder(Color.black, 2, true)
。 LineBorder doc
如果你想要大的自定义,我建议你使用/写一个外观。参见 tutorial|uiswing
这是我的测试:
public class Test {
private static void createAndShowGUI() {
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton button = new JButton("Test");
JPopupMenu popup = new JPopupMenu();
popup.setBorder(new LineBorder(Color.black, 2, true));
{
JMenuItem item = new JMenuItem("Cut");
item.setForeground(Color.ORANGE);
popup.add(item);
}
{
JMenuItem item = new JMenuItem("Copy");
item.setBackground(Color.RED);
popup.add(item);
}
{
JMenuItem item = new JMenuItem("Paste");
popup.add(item);
}
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
popup.show(frame, frame.getWidth() / 2, frame.getHeight() / 2);
System.out.println("perfome");
}
});
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
关于java - 修改 JPopupMenu/JMenuItem 外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66382489/
我有这个代码: JMenuItem itemA; JMenuItem itemB; JMenuItem itemC; JMenuItem[] items = {itemA, itemB, itemC}
如何防止 JMenuItem 在单击 JMenuItem 时关闭菜单? JMenuItem 已启用。 这就是场景,我有 3 个 JMenuItems: JMenuItem: A, B, C; C 显示
我有一个 JPopupMenu 并在其中插入一些 JMenuItems。我需要的是一个看起来与普通母鹿相同但不可单击的 JMenuItem。 我尝试过: JPopupMenu popmen = new
我有一个 JMenu,其中填充有来自具有监听器的数据库中的 JMenuItems,其中之一是从数据库中删除条目(如果选择)。发生这种情况时,JMenuItem 应该从菜单中消失。这是一个简短的例子 f
我正在尝试在 JMenuBar 上创建一个帮助按钮。目前我可以使用这个使其向右对齐 helpItem.setComponentOrientation(ComponentOrientation.RIGH
我正在尝试通过单击文件菜单栏中的菜单项来加载特定文件 (C://myfile.txt),我需要帮助来完成代码的操作部分 JMenuBar menuBar = new JMenuBar(); JMenu
是否可以创建一个包含复选框列表的自定义 JMenuItem?示例是 Excel 过滤器弹出菜单: 到目前为止,我所做的是这样的: JPopupMenu headerPopup = new JPopup
此代码将用户最喜欢的歌曲从 ArrayMap 添加到 JmenuItem public void actionPerformed(ActionEvent evt) { Stri
如何将 JMenuItem (newItem) 添加到 JMenu (menuUsers)?是否有合适的 ActionListener 用于此目的?有一部分代码执行将菜单项添加到菜单的操作。它在引发某
我的 inv 终于可以工作了! :D 但是,你知道...现在...这很愚蠢。我希望它在我右键单击时显示“使用项目 1”或其他内容,所以我这样做: if (actItemx == "It
我有一个带有 JMenuBar 的框架。当我按下 JMenuItems 之一时,我想向框架添加一个 JLabel ,但它不起作用。我不明白……这是同一个类(class)的。 public class
JMenuItem 具有以下构造函数:(来源: GrepCode ) public JMenuItem(Action a) { this(); setAction(a); } 但是,当
我创建了一个带有颜色的 3x3 内存游戏。程序运行并显示颜色,但 JMenuBar 无法正常工作。新棋盘、游戏和结束游戏按钮不起作用。我的 Actionlistener 有问题吗? 这就是代码应该如何
我将菜单添加到工具栏,如下所示: JMenuBar menu = new JMenuBar(); JMenu actions = new JMenu("Aktionen");
我想为颜色列表添加每个菜单项行,但有类似的错误 "at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)" 在我的
所以我必须在 Swing 中为我的 Java 类创建一个简单的 GUI,我偶然发现了这个小的装饰性问题。 我有以下代码: JMenuItem mntmQuit = new JMenuItem(
我试图添加一些菜单,想知道如何在第二个 menuItem 之后添加边框 就这样 Image Here 这是我的代码 jmb = new JMenuBar(); jmb.setPreferredSize
我想为 JMenuItem 设置快捷键。 我现在是这样设置的 openFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionE
我自动生成一组 JMenuItem 并将它们添加到每个右键单击事件的 JPopUpMenu 中。问题是当我在所选菜单上按回车键时,我为菜单项设置的操作没有运行。我可以使用向上/向下键在菜单上导航,但是
我有一个带有标准项和快捷方式的 JMenuBar。但是我注意到快捷方式描述是左对齐的,看起来很难看。有办法右对齐吗? PS:“Umschalt”表示转变。有没有办法强制它说 shift 而不是 Ums
我是一名优秀的程序员,十分优秀!