如何在这种类型的菜单中添加更多按钮(有一张显示菜单类型的图片)?
How do I add more buttons in a menu of this type (there's a pic showing the type of menu)?
我不知道这种菜单的名称,但我想在这种菜单中添加一个项目。
按照 link 的菜单示例:Link:http://postimg.org/image/7izr3zapl/full/
IntelliJ IDEA 已为 Windows 和 OS X 实现了此功能,因此您可以将其用作示例。
这里重点关注Windows,大家可以看看
RecentTasks class for the implementation. To add the recent tasks, the addTasksNativeForCategory
native method gets called. This C++ method is implemented in the following file: jumplistbridge.cpp.
这些是带有图标的菜单项。它们 不是 按钮!
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.URL;
import javax.imageio.ImageIO;
public class MenuWithIcon {
private JComponent ui = null;
String[] urlStrings = {
"http://i.stack.imgur.com/gJmeJ.png",
"http://i.stack.imgur.com/gYxHm.png",
"http://i.stack.imgur.com/F0JHK.png"
};
String[] menuNames = {
"Blue Circle", "Green Triangle", "Red Square"
};
MenuWithIcon() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
Image img = new BufferedImage(300, 150, BufferedImage.TYPE_INT_ARGB);
ui.add(new JLabel(new ImageIcon(img)));
}
public JMenuBar getMenuBar() throws Exception {
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("Menu");
mb.add(menu);
for (int i=0; i<urlStrings.length; i++) {
URL url = new URL(urlStrings[i]);
Image img = ImageIO.read(url);
JMenuItem mi = new JMenuItem(menuNames[i], new ImageIcon(img));
menu.add(mi);
}
return mb;
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
MenuWithIcon o = new MenuWithIcon();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
try {
f.setJMenuBar(o.getMenuBar());
} catch (Exception ex) {
ex.printStackTrace();
}
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
我不知道这种菜单的名称,但我想在这种菜单中添加一个项目。
按照 link 的菜单示例:Link:http://postimg.org/image/7izr3zapl/full/
IntelliJ IDEA 已为 Windows 和 OS X 实现了此功能,因此您可以将其用作示例。
这里重点关注Windows,大家可以看看
RecentTasks class for the implementation. To add the recent tasks, the addTasksNativeForCategory
native method gets called. This C++ method is implemented in the following file: jumplistbridge.cpp.
这些是带有图标的菜单项。它们 不是 按钮!
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.URL;
import javax.imageio.ImageIO;
public class MenuWithIcon {
private JComponent ui = null;
String[] urlStrings = {
"http://i.stack.imgur.com/gJmeJ.png",
"http://i.stack.imgur.com/gYxHm.png",
"http://i.stack.imgur.com/F0JHK.png"
};
String[] menuNames = {
"Blue Circle", "Green Triangle", "Red Square"
};
MenuWithIcon() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
Image img = new BufferedImage(300, 150, BufferedImage.TYPE_INT_ARGB);
ui.add(new JLabel(new ImageIcon(img)));
}
public JMenuBar getMenuBar() throws Exception {
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("Menu");
mb.add(menu);
for (int i=0; i<urlStrings.length; i++) {
URL url = new URL(urlStrings[i]);
Image img = ImageIO.read(url);
JMenuItem mi = new JMenuItem(menuNames[i], new ImageIcon(img));
menu.add(mi);
}
return mb;
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
MenuWithIcon o = new MenuWithIcon();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
try {
f.setJMenuBar(o.getMenuBar());
} catch (Exception ex) {
ex.printStackTrace();
}
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}