框架 2 位于框架 1 内

发布于 2025-01-06 15:07:48 字数 615 浏览 2 评论 0原文

我有2节课; Students 和 RegisterStudents,因此有 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 中。有人有什么建议吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的绿洲 2025-01-13 15:07:49
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,但随后您没有使用新的 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);
       panel.add(main_panel); // ADD THIS LINE
   }
});
btnNewButton.setBounds(0, 162, 167, 37);
panel.add(btnNewButton);

You were initializing the new main_panel, and new panel_1, and adding panel_1 to main_panel but then you weren't doing anything with the new main_panel.

Also, I highly suggest naming your variables otherwise - these names are very non-intuitive.

山川志 2025-01-13 15:07:49

对于这样的事情,我建议您使用 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();
            }
        });
    }
}

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();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文