【问题标题】:2 JPanels in the same location using Jigloo Swing使用 Jigloo Swing 在同一位置 2 个 JPanel
【发布时间】:2013-04-29 09:41:04
【问题描述】:

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

【问题讨论】:

  • “普遍共识..当用户登录时,第一个要求登录详细信息的 JFrame 将其可见性设置为 false,第二个 JFrame 的可见性将变为 True。” 这不是任何值得关注的网站的普遍共识。使用CardLayout。顺便说一句..你有问题吗?这是什么?

标签: java swing user-interface


【解决方案1】:

我已经回答了一个类似的问题,其中您在单个 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);
    }
}

【讨论】:

  • 基本上,一旦用户执行登录操作,您要做的就是将登录面板的可见性设置为 false(如果以后不在您的程序中使用,则删除此面板)并设置选项面板的可见性为真.. 请记住一件事,这两个面板(登录和选项面板)的位置(即 gridx = 0 和 gridy = 0)是相同的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 2014-05-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多