使用Jigloo Swing在同一位置的2个JPanel

时间:2013-05-05 13:40:27

标签: java swing user-interface

我的问题是我需要创建一个GUI,在启动时显示登录屏幕,然后,当用户成功登录时,会显示不同的屏幕。我已经访问过这个板子和其他人的其他问题,而且就所有这些问题而言,普遍的共识是,我应该在同一个JFrame中使用2个JPanel,而不是使用两个不同的JFrame。当用户登录时,第一个要求登录详细信息的JFrame将其可见性设置为false,第二个JFrame的可见性将变为True。我在这里遇到的问题是我似乎无法将2个JPanel放在同一个位置。我正在使用Jigloo来开发Swing。每当我放置第二个JPanel并将其可见性设置为false时,它的大小就变为0,0。我尝试在第二个面板上放置组件,然后设置我的首选大小,然后将可见性切换为false,但是在执行期间两个面板都消失了,尽管第一帧的可见性仍然是真的并且是正确的大小。求救!

1 个答案:

答案 0 :(得分:2)

我已回答了一个类似的问题,其中您在单个JFrame中有多个面板..并且基于用户操作执行的面板被替换

Can't seem to get .remove to work

根据您的查询为程序设置外观:

public class Main extends JFrame implements ActionListener
{
    private JPanel componentPanel = null;   
    private JPanel loginPanel = null;    
    private JLabel loginLabel = null;    
    private JPanel optionPanel = null;
    private JLabel optionLabel = null; 
    private JButton loginButton = null;

    public JPanel getComponentPanel()
    {
       if(null == componentPanel)
       {
           componentPanel = new JPanel();
           GridBagLayout gridBagLayout = new GridBagLayout();
           componentPanel.setLayout(gridBagLayout);

           GridBagConstraints constraint = new GridBagConstraints();
           constraint.insets = new Insets(10, 10, 10, 10);

           loginPanel = new JPanel();           
           constraint.gridx = 0;
           constraint.gridy = 0;
           loginPanel.setMinimumSize(new Dimension(100, 50));
           loginPanel.setPreferredSize(new Dimension(100, 50));
           loginPanel.setMaximumSize(new Dimension(100, 50));
           loginPanel.setBorder(
                   BorderFactory.createLineBorder(Color.RED));

           loginLabel = new JLabel("Login Panel");
           loginPanel.add(loginLabel);
           componentPanel.add(loginPanel, constraint);

           optionPanel = new JPanel();         
           constraint.gridx = 0;
           constraint.gridy = 0;
           optionPanel.setMinimumSize(new Dimension(100, 50));
           optionPanel.setPreferredSize(new Dimension(100, 50));
           optionPanel.setMaximumSize(new Dimension(100, 50));
           optionPanel.setBorder(
                   BorderFactory.createLineBorder(Color.BLUE));

           optionLabel = new JLabel("Option Panel");
           optionPanel.add(optionLabel);
           componentPanel.add(optionPanel, constraint);

           loginButton = new JButton("Login");
           constraint.gridx = 0;
           constraint.gridy = 1;
           loginButton.addActionListener(this);
           componentPanel.add(loginButton, constraint);
       }       
       return componentPanel;
    }

    public void actionPerformed (ActionEvent evt) 
    {
        loginPanel.setVisible(false);
        loginButton.setEnabled(false);
        optionPanel.setVisible(true);
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        Main main = new Main();

        frame.setTitle("Simple example");
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);

        frame.setContentPane(main.getComponentPanel());

        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}