简体   繁体   English

Java应用程序中具有分层特性的组件?

[英]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. 我有一个具有表单的Java应用程序,用户必须填写它,但是我想使用多层技术来填充大表单作为第三部分,单击“确定”,第一部分必须不可见,第二部分将可见,第三部分也保持不可见。

What must I use, jpanel , jLayeredPane or what, and how to do it with netbeans? 我必须使用jpaneljLayeredPane或什么,以及如何使用netbeans?

You might want to look at CardLayout , here is a tutorial, How to Use CardLayout. 您可能想看一下CardLayout ,这是一个教程, 如何使用CardLayout。

Another option will be using multiple JDialogs. 另一个选择是使用多个JDialogs。

...and how to do it with netbeans? ...以及如何使用netbeans?

If you mean using the GUI builder, I'd suggest you to learn java instead of learning the IDE :) 如果您打算使用GUI构建器,建议您学习Java而不是学习IDE :)

For any more advanced Swing application I would strongly recommend going with Netbeans RCP. 对于任何更高级的Swing应用程序,我强烈建议您使用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 您有一个现成的向导组件可以满足您的需求: 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();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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