简体   繁体   中英

a component for having layered characteristic in java app?

I have a Java application that has a form, user must fill it, but i want to use Multi-layered technique for filling the big form as thre parts, clicking OK and the first part must be invisible and 2nd part will be visible, 3rd part stay invisible too.

What must I use, jpanel , jLayeredPane or what, and how to do it with netbeans?

You might want to look at CardLayout , here is a tutorial, How to Use CardLayout.

Another option will be using multiple JDialogs.

...and how to do it with netbeans?

If you mean using the GUI builder, I'd suggest you to learn java instead of learning the IDE :)

For any more advanced Swing application I would strongly recommend going with Netbeans RCP. YOu have a wizard component working out of the box which should satisfy your needs: http://platform.netbeans.org/tutorials/nbm-wizard.html

Try your hands on this code :

import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;

public class Form 
{
    private static void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Form");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

        final JPanel firstPanel = new JPanel();
        firstPanel.setBackground(Color.DARK_GRAY);      
        JLabel label = new JLabel("I AM PANEL FIRST");
        label.setForeground(Color.WHITE);
        firstPanel.add(label);

        final JPanel secondPanel = new JPanel();
        secondPanel.setBackground(Color.YELLOW);        
        label = new JLabel("I AM PANEL SECOND");
        label.setForeground(Color.BLACK);
        secondPanel.add(label);

        final JPanel thirdPanel = new JPanel();
        thirdPanel.setBackground(Color.BLUE);       
        label = new JLabel("I AM PANEL THIRD");
        label.setForeground(Color.WHITE);
        thirdPanel.add(label);

        JButton button = new JButton("NEXT");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (firstPanel.isShowing())
                {
                    firstPanel.setVisible(false);
                    secondPanel.setVisible(true);
                }
                else if (secondPanel.isShowing())
                {
                    secondPanel.setVisible(false);
                    thirdPanel.setVisible(true);
                }
            }
        });

        mainPanel.add(firstPanel);
        mainPanel.add(secondPanel);
        mainPanel.add(thirdPanel);
        mainPanel.add(button);

        secondPanel.setVisible(false);
        thirdPanel.setVisible(false);

        frame.setContentPane(mainPanel);        
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndDisplayGUI();
            }
        });
    }
}

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