0

我有2节课;学生和注册学生,因此有 2 个不同的 main_panel(1 类)和 panel_1(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 中。有没有人有任何建议?

4

2 回答 2

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

您正在初始化 newmain_panel和 newpanel_1并添加panel_1到,main_panel但是您没有对 new 做任何事情main_panel

另外,我强烈建议以其他方式命名您的变量 - 这些名称非常不直观。

于 2012-02-17T04:07:33.310 回答
1

对于这样的事情我建议你使用CardLayout 当你向容器中添加东西时,你必须调用 revalidate() 和 repaint() 方法来实现在运行时对其所做的更改。就像在你的情况下你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();
            }
        });
    }
}
于 2012-02-17T05:08:09.783 回答