简体   繁体   中英

Swing - Android like Drop-Down-Menu

How can i realize this Android-Drop-Down-Menu in Java-Swing? I can only find tutorials for Android (with XML-Layout) but nothing for Swing.

Screenshot:

http://www11.pic-upload.de/16.01.15/15s13n8z9jw.png

I already have this code:

JMenu menu = new JMenu();
        BufferedImage image = null;
        try {

            image = ImageIO.read(new URL("https://cdn0.iconfinder.com/data/icons/very-basic-android-l-lollipop-icon-pack/24/menu2-32.png"));

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();
        }
        menu.setIcon(new ImageIcon(image));

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBackground(Color.WHITE);
        menuBar.setBorderPainted(false);
        menuBar.add(menu);

        JMenuItem item = new JMenuItem("Test Item");
        item.setBorderPainted(false);
        item.setBackground(Color.WHITE);
        menu.add(item);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(500, 550);

        JPanel panel_1 = new JPanel();
        panel_1.add(menuBar);
        frame.add(panel_1);

        frame.setVisible(true);

But as you can see it doesn't look exactly like in the Screenshot:

http://www11.pic-upload.de/16.01.15/n7536l3voi1s.jpg

Does anybody know how to achieve this?

I think you're just looking for the JCheckBoxMenuItem. You can add it to any JMenu. http://docs.oracle.com/javase/7/docs/api/javax/swing/JCheckBoxMenuItem.html

(If I understood your question correctly then you already have this implemented in Android and are interested in porting it to a Swing application).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM