2012-02-03 42 views
1

我有一个具有表单的Java应用程序,用户必须填写它,但我想使用多层技术填充大表单作为三部分,单击确定,第一部分必须不可见,第二部分将可见,第三部分也不可见。在java应用程序中具有分层特性的组件?

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

+1

这个问题可以张贴到http://forums.netbeans.org/ – mKorbel 2012-02-03 11:01:21

回答

5

你可能想看看CardLayout,这里是一个教程,How to Use CardLayout.

另一种选择将使用多个JDialogs。

...以及如何使用netbeans做到这一点?

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

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(); 
      } 
     }); 
    } 
} 
相关问题