框架2在框架1内

时间:2012-02-17 03:39:01

标签: java swing jframe cardlayout

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

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

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中。有没有人有任何建议?

2 个答案:

答案 0 :(得分:1)

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);
       panel.add(main_panel); // ADD THIS LINE
   }
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);

您正在初始化新的main_panel和新的panel_1,并将panel_1添加到main_panel但是您没有对新的{{1}做任何事情}。

此外,我高度建议在其他方面命名您的变量 - 这些名称非常不直观。

答案 1 :(得分:1)

对于这些事情我建议你使用CardLayout 向容器添加内容时,必须调用revalidate()和repaint()方法以实现在RunTime中对其进行的更改。就像在你的情况下你现在添加main_panel.add(panel_1);一样,你必须执行

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

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

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