简体   繁体   中英

Java Swing form menu with hide and show functionality

I'm kind of new on GUI programming, and I've been struggling to figure out how to implement something like the following:

https://i.stack.imgur.com/zvWsH.png

basically I would like to have some text entries, with an arrow on the side. When clicking on the arrow, or on the text, a form would appear under it.

Any suggestion would be appreciated, Thank you in advance.

Conceptually, this is just making a container (like JPanel ) hide or show and letting the layout managers do their job

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test(); 
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(new JLabel("Pecka-boo"));

            JButton btn = new JButton("Show");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    panel.setVisible(!panel.isVisible());
                    btn.setText(panel.isVisible() ? "Hide" : "Show");
                }
            });

            gbc.gridx = 0;
            gbc.gridy = 0;
            add(btn, gbc);

            gbc.gridy++;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.weighty = 1;
            add(panel, gbc);
            panel.setVisible(false);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

Take a look at:

for some more details.

FYI: A more complex UI would have the sub forms as separate classes, this will reduce the complex (of the individual classes) and decouple the code

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