框架2在框架1内 [英] frame 2 inside frame 1

查看:102
本文介绍了框架2在框架1内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两节课;学生和RegisterStudents,因此有2个不同的main_panel(Class 1)和panel_1(Class 2)。我要做的是,当按下学生界面上的按钮时,整个panel_1应出现在main_panel中。我已经将它们设置为相同的大小。这可能吗?

I have 2 classes; Students and RegisterStudents, and hence 2 different main_panel(Class 1) and panel_1 (Class 2). What I am trying to do is, when a button on the Students Interface is pressed, the whole panel_1 should appear within main_panel. I have set both to same size already. is that possible?

到目前为止,我得到的代码是:

The code i got so far is:

JButton btnNewButton = new JButton("Register Student");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {


Students main_panel = new Students();
RegisterStudent panel_1 = new RegisterStudent();
main_panel.add(panel_1);


}
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);

这不做任何事情吗?它的编译,但panel_1实际上并没有出现在main_panel内。有没有人有任何建议?

This isnt doing anything though? its compiling, but panel_1 is not actually appearing inside the main_panel. Has anyone got any suggestions?

推荐答案

对于这些事情我建议你使用 CardLayout
当您向容器添加内容时,必须调用revalidate()并重绘()在RunTime上实现对它所做的更改的方法。就像在你的情况下你添加 main_panel.add(panel_1); 之后你必须执行

For such things I would suggest you to use CardLayout When you add something to the container, you must call revalidate() and repaint() methods to realize the changes made to it at RunTime. Like in your case you adding main_panel.add(panel_1);now after this you must perform

main_panel.revalidate();
main_panel.repaint();
frame.getRootPane().revalidate(); // for Upto JDK 1.6.
frame.revalidate();  // for JDK 1.7+
frame.repaint();

以便可以看到变化。一个小代码片段,可以帮助您理解我的意思。

so that changes can be seen. A small code snippet to help you understand what I mean.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MultiplePanels extends JFrame
{
    private JPanel registrationPanel, loginPanel, searchPanel;

    private JButton registerButton, loginButton, searchButton;  

    private ActionListener action;

    public MultiplePanels()
    {       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);            

        registrationPanel = new JPanel();
        registrationPanel.setBackground(Color.WHITE);

        loginPanel = new JPanel();
        loginPanel.setBackground(Color.YELLOW);

        searchPanel = new JPanel();
        searchPanel.setBackground(Color.BLUE);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(0, 1));
        buttonPanel.setBackground(Color.DARK_GRAY);

        registerButton = new JButton("REGISTER");       
        loginButton = new JButton("LOGIN");     
        searchButton = new JButton("SEARCH");

        buttonPanel.add(registerButton);
        buttonPanel.add(loginButton);
        buttonPanel.add(searchButton);

        action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JButton button = (JButton) ae.getSource();

                if (button == registerButton)
                {
                    if (!(loginPanel.isShowing()) && !(searchPanel.isShowing()))
                    {
                        add(registrationPanel, BorderLayout.CENTER);
                    }
                    else 
                    {
                        if (loginPanel.isShowing())
                        {
                            remove(loginPanel);
                            add(registrationPanel, BorderLayout.CENTER);
                        }
                        else if (searchPanel.isShowing())
                        {
                            remove(searchPanel);
                            add(registrationPanel, BorderLayout.CENTER);
                        }
                    }
                }
                else if (button == loginButton)
                {
                    if (!(registrationPanel.isShowing()) && !(searchPanel.isShowing()))
                    {
                        add(loginPanel, BorderLayout.CENTER);
                    }
                    else 
                    {
                        if (registrationPanel.isShowing())
                        {
                            remove(registrationPanel);
                            add(loginPanel, BorderLayout.CENTER);
                        }
                        else if (searchPanel.isShowing())
                        {
                            remove(searchPanel);
                            add(loginPanel, BorderLayout.CENTER);
                        }
                    }
                }
                else if (button == searchButton)
                {
                    if (!(loginPanel.isShowing()) && !(registrationPanel.isShowing()))
                    {
                        add(searchPanel, BorderLayout.CENTER);
                    }
                    else 
                    {
                        if (loginPanel.isShowing())
                        {
                            remove(loginPanel);
                            add(searchPanel, BorderLayout.CENTER);
                        }
                        else if (registrationPanel.isShowing())
                        {
                            remove(registrationPanel);
                            add(searchPanel, BorderLayout.CENTER);
                        }
                    }
                }
                // This is what we are doing here to realize the changes
                // made to the GUI.
                revalidate();
                repaint();
            }
        };

        registerButton.addActionListener(action);
        loginButton.addActionListener(action);
        searchButton.addActionListener(action);

        add(buttonPanel, BorderLayout.LINE_START);
        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new MultiplePanels();
            }
        });
    }
}

这篇关于框架2在框架1内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆