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

时间:2012-02-03 10:58:30

标签: java swing netbeans

我有一个带有表单的Java应用程序,用户必须填写它,但是我想使用多层技术将大表单填充为thre部分,单击OK,第一部分必须是不可见的,第二部分将是可见,第三部分也看不见。

我必须使用什么,jpaneljLayeredPane或什么,以及如何使用netbeans?

3 个答案:

答案 0 :(得分:5)

您可能需要查看CardLayout,这是一个教程,How to Use CardLayout.

另一种选择是使用多个JDialog。

  

...以及如何使用netbeans进行操作?

如果您的意思是使用GUI构建器,我建议您学习Java而不是学习IDE:)

答案 1 :(得分:2)

对于任何更高级的Swing应用程序,我强烈建议使用Netbeans RCP。你有一个开箱即用的向导组件,可以满足你的需求:http://platform.netbeans.org/tutorials/nbm-wizard.html

答案 2 :(得分:1)

尝试使用此代码:

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();
            }
        });
    }
}