我想用Swing來實現這種類型的菜單。有沒有現成的解決方案(免費和/或商業)?如何使用Java Swing創建此特殊菜單欄?
4
A
回答
14
假設你想在菜單上的形象,爲什麼不這樣呢?
BufferedImage image = ImageIO.read(url);
yourJMenu.setHorizontalTextPosition(SwingConstants.CENTER);
yourJMenu.setVerticalTextPosition(SwingConstants.BOTTOM);
yourJMenu.setIcon(new ImageIcon(image));
編輯:好像你問從頭開始。
請參閱:How to Use Menus閱讀本答案之前。
編輯2:這裏是一個SSCCE,
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
public class MenuTest {
public static void main(String[] argv) throws Exception {
// Create the menu bar
JMenuBar menuBar = new JMenuBar();
String imageURL = "http://blogs.discovermagazine.com/" +
"drone360/wp-content/themes/discoverblog/images/" +
"gear_icon.png";
// Create a menu
JMenu menu = new JMenu("Menu");
BufferedImage image = ImageIO.read(new URL(imageURL));
menu.setHorizontalTextPosition(SwingConstants.CENTER);
menu.setVerticalTextPosition(SwingConstants.BOTTOM);
menu.setIcon(new ImageIcon(image));
menuBar.add(menu);
// Create a menu item
JMenuItem item = new JMenuItem("Test Item");
menu.add(item);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menuBar);
frame.setSize(500, 550);
frame.setVisible(true);
}
}
0
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
/* MenuLookDemo.java requires images/middle.gif. */
/*
* This class exists solely to show you what menus look like.
* It has no menu-related event handling.
*/
public class MenuLookDemo {
JTextArea output;
JScrollPane scrollPane;
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItemMenuItem;
JCheckBoxMenuItem cbMenuItem;
//Create the menu bar.
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("A Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("Av text-only menu item", KeyEvent.VK_T);
//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
menu.add(menuItem);
ImageIcon icon = createImageIcon("middle.gif");
menuItem = new JMenuItem("Both text and icon", icon);
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem = new JMenuItem(icon);
menuItem.setMnemonic(KeyEvent.VK_D);
menu.add(menuItem);
//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic(KeyEvent.VK_O);
group.add(rbMenuItem);
menu.add(rbMenuItem);
//a group of check box menu items
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
cbMenuItem.setMnemonic(KeyEvent.VK_C);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Another one");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
menu.add(cbMenuItem);
//a submenu
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem("Another item");
submenu.add(menuItem);
menu.add(submenu);
//Build second menu in the menu bar.
menu = new JMenu("Another Menu");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
menuBar.add(menu);
return menuBar;
}
public Container createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
//Create a scrolled text area.
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
//Add the text area to the content pane.
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = MenuLookDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MenuLookDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
MenuLookDemo demo = new MenuLookDemo();
frame.setJMenuBar(demo.createMenuBar());
frame.setContentPane(demo.createContentPane());
//Display the window.
frame.setSize(450, 260);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
相關問題
- 1. 如何創建此邊欄菜單?
- 2. 動態創建Java Swing子菜單
- 3. 在Swing中使用圖標創建多個菜單欄
- 4. 如何使用菜單欄在grails中創建菜單
- 5. 如何使用子菜單創建水平菜單欄MVC4
- 6. 如何使用JSF創建菜單欄和子菜單
- 7. 創建菜單欄
- 8. 菜單欄右側的Swing菜單項
- 9. 創建菜單欄
- 10. 如何在java swing中創建一個下拉菜單
- 11. 如何使用Swing創建簡單的Java表單?
- 12. 創建HTML菜單欄
- 13. 如何爲pygame創建tkinter菜單欄
- 14. 如何創建擴展邊欄菜單
- 15. 如何在python3.5中創建菜單欄
- 16. 如何在JSF中創建菜單欄
- 17. 頂部菜單欄創建
- 18. 如何創建的Java Swing
- 19. 如何使用Java Swing操作在Netbeans中創建菜單項和工具欄項目
- 20. 我創建菜單欄
- 21. Java Swing Ribbon菜單
- 22. 無法創建菜單欄
- 23. 用於創建此菜單欄的工具
- 24. 如何在Java Swing中構建Google-chrome選項卡和菜單欄界面?
- 25. 如何使用vb.net中的下拉菜單創建垂直菜單欄
- 26. 如何在Swing中創建此組件?
- 27. 在Swing中創建等距菜單
- 28. 如何使用4.0.3 SDK在活動底部創建菜單欄
- 29. 如何使用FragmentActivity創建標題欄/設置菜單?
- 30. 如何在Access中創建一個菜單欄使用VBA
嗨桑傑,我需要的是某種形式的下拉按鈕,但與精確的外觀和截圖(圖標下的文字)。 –
@ user1377575查看圖標下的文字,查看答案中的編輯。 – COD3BOY
圖像主要是'應用程序資源',它們應該通過'URL'而不是'File'來訪問,所以恕我直言,編寫'ImageIO.read(url);'將會是一個非常WISER的方法:-) –