Create multiple icon menus in Swing

I am working on a Swing application in which I need to create several menus when I click on a menu. Another menu should be added or removed from the container.

I need to use the icon above and below the text in the menu, but I did not find a suitable constructor for it.

Is it possible to have a back and forward button in JMenu that should work when the back and forward buttons work in a browser? They should display the previous view of the window, preferably without multiple displays.

+4
source share
3 answers

Do you really mean the menu bar? Your buttons sound more like an instrument.

However, JMenu is a special type of button, so maybe you can get your own menu class from this to paint the top-level menu differently. For the main menu, some platforms (Mac OS in particular) will probably not process custom drawing code, since this menu bar is processed by the native OS code. But for the second menu bar, which is painted with a swing, I would not expect any problems. After all, JMenuBar is just Component .

+1
source

If you go with multiple instances of JToolBar , here, you can add each of them to a different CardLayout panel, shown here .

+1
source

Just use two JPanels.

Here are a few codes that I wrote a few minutes ago to start the store’s GUI.

 public Shop() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); contentPane.add(panel, BorderLayout.NORTH); JPanel panel_1 = new JPanel(); contentPane.add(panel_1, BorderLayout.CENTER); { JMenuBar menu = new JMenuBar(); final JTextField coins = new JTextField("Coins: " + TerrainDemo.money); coins.setForeground(Color.red); menu.add(coins); panel.add(menu); } { JMenuBar menu = new JMenuBar(); final JTextField coins = new JTextField("Coins: " + TerrainDemo.money); coins.setForeground(Color.red); menu.add(coins); panel_1.add(menu); } } 

Result:

enter image description here

0
source

Source: https://habr.com/ru/post/1436749/


All Articles