【问题标题】:How to open another JPanel in JFrame when using multiple JPanel classes使用多个 JPanel 类时如何在 JFrame 中打开另一个 JPanel
【发布时间】:2021-05-03 09:29:18
【问题描述】:

我已经找了好几天了,但我还没有找到解决方案,所以我只是创建了我的帐户。

我想知道如何从我的 JFrame 中的当前 JPanel 打开另一个 JPanel。到目前为止,我在视图包中创建了三个类:mainView (JFrame)、loginAs (JPanel)、loginPanel (JPanel)。我想要做的是,我想将 loginAs Panel 显示为我的第一个面板,用户可以在其中选择他是管理员还是员工,然后单击所需的选项后,它应该打开他们可以登录的 loginPanel。我被卡住了在这里,因为我不知道如何将用户选择了 Admin 或 Staff 的信息返回给 mainView,而 mainView 现在应该移动到下一个面板,即 loginPanel。

这是我做的编码:

主视图

package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.Color;

public class mainView extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainView frame = new mainView();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public mainView() {
        setTitle("COVID'19 Management System");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 550, 410);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new CardLayout(0, 0));
        
        JPanel p1 = new loginAs();
        contentPane.add(p1, "t1");
        
        JPanel p2 = new loginPanel();
        contentPane.add(p2, "t2");
        
        JPanel p3 = new JPanel();
        contentPane.add(p3, "t3");
        
        JPanel p4 = new JPanel();
        contentPane.add(p4, "t4");
    }

}

登录身份

package view;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import view.mainView;

public class loginAs extends JPanel {

    /**
     * Create the panel.
     */
    public loginAs() {
        setBackground(Color.BLACK);
        setLayout(null);
        
        JLabel lblNewLabel = new JLabel("COVID'19 Management System");
        lblNewLabel.setForeground(Color.WHITE);
        lblNewLabel.setFont(new Font("Calibri", Font.BOLD, 18));
        lblNewLabel.setBounds(140, 27, 262, 28);
        add(lblNewLabel);
        
        JLabel lblNewLabel_1 = new JLabel("LOGIN AS");
        lblNewLabel_1.setForeground(Color.WHITE);
        lblNewLabel_1.setFont(new Font("Calibri", Font.BOLD, 16));
        lblNewLabel_1.setBounds(35, 87, 97, 28);
        add(lblNewLabel_1);
        
        JButton btnAdmin = new JButton("ADMIN");
        btnAdmin.setForeground(Color.BLACK);
        btnAdmin.setBackground(Color.WHITE);
        btnAdmin.setBounds(35, 134, 97, 39);
        add(btnAdmin);
        btnAdmin.setBorderPainted(false);
        btnAdmin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnAdmin.setBackground(Color.GREEN);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnAdmin.setBackground(Color.WHITE);
            }

            public void mouseClicked(java.awt.event.MouseEvent e)
            {
                
            }
        });
        
        JButton btnStaff = new JButton("STAFF");
        btnStaff.setForeground(Color.BLACK);
        btnStaff.setBackground(Color.WHITE);
        btnStaff.setBounds(35, 200, 97, 39);
        add(btnStaff);
        btnStaff.setBorderPainted(false);
        btnStaff.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnStaff.setBackground(Color.GREEN);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnStaff.setBackground(Color.WHITE);
            }

            public void mouseClicked(java.awt.event.MouseEvent e)
            {
                
            }
        });
    }
}

登录面板

package view;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;

public class loginPanel extends JPanel {
    private JTextField txt_uname;
    private JTextField txt_pass;

    /**
     * Create the panel.
     */
    public loginPanel() {
        setBackground(Color.BLACK);
        setLayout(null);
        
        JLabel lblNewLabel = new JLabel("COVID'19 Management System");
        lblNewLabel.setForeground(Color.WHITE);
        lblNewLabel.setFont(new Font("Calibri", Font.BOLD, 18));
        lblNewLabel.setBounds(140, 27, 262, 28);
        add(lblNewLabel);
        
        JLabel lblNewLabel_1 = new JLabel("LOGIN PAGE");
        lblNewLabel_1.setForeground(Color.WHITE);
        lblNewLabel_1.setFont(new Font("Calibri", Font.BOLD, 16));
        lblNewLabel_1.setBounds(35, 87, 97, 28);
        add(lblNewLabel_1);
        
        JLabel lblNewLabel_2 = new JLabel("Username");
        lblNewLabel_2.setFont(new Font("Calibri", Font.BOLD, 14));
        lblNewLabel_2.setForeground(Color.WHITE);
        lblNewLabel_2.setBounds(35, 142, 74, 14);
        add(lblNewLabel_2);
        
        txt_uname = new JTextField();
        txt_uname.setBounds(123, 137, 147, 20);
        add(txt_uname);
        txt_uname.setColumns(10);
        
        JLabel lblNewLabel_2_1 = new JLabel("Password");
        lblNewLabel_2_1.setForeground(Color.WHITE);
        lblNewLabel_2_1.setFont(new Font("Calibri", Font.BOLD, 14));
        lblNewLabel_2_1.setBounds(35, 188, 74, 14);
        add(lblNewLabel_2_1);
        
        txt_pass = new JTextField();
        txt_pass.setColumns(10);
        txt_pass.setBounds(123, 183, 147, 20);
        add(txt_pass);
        
        JButton btn_login = new JButton("Login");
        btn_login.setBounds(123, 229, 89, 23);
        add(btn_login);

    }
}

这是视觉表示:

【问题讨论】:

  • 您还应该将您的用户界面与您的业务逻辑分开。成模型视图模式。如果您有一个包含所有信息的类(模型),这甚至可能是一个单例,并且它通过构造函数传递给下一个JPanel(视图),或者如果它是一个单例,您只需使用它的实例获取它访问者。通过这种方式,您可以在应用程序上的各个 JPanels 之间共享信息。
  • 哦,谢谢,这将有助于信息共享。但是现在当我有了这些信息时,我该如何切换 JPanel 呢?它卡在 loginAs JPanel 上,而现在我想显示 loginPanel。
  • 回答您切换 JPanel 的问题,您会看到我已将您的问题作为重复项关闭。链接(在标题下的问题顶部)应该向您展示如何在单个 jframe 上的各种 jpanel 之间切换
  • 不确定我是否完全理解,但它应该像这样工作。 JFrame 有 LoginPanel,用户单击 LoginPanel 上的登录按钮,然后面板应该与 JFrame 通信,告诉它切换到下一个面板。如果您实现MVC pattern,这会更容易,但这不是必需的,您可以简单地使用mainView topFrame = (mainView) SwingUtilities.getWindowAncestor(this); 获取将LoginPanel 添加到的JFrame 实例,然后调用您想要的任何方法来使用CardLayout 切换面板跨度>
  • 我将重新打开这个问题,因为作为副本关闭可能是不正确的,也就是说我会强烈考虑使用我链接的 MVC 模式在视图之间切换,这也将有所帮助您在面板中保存信息

标签: java swing jframe jpanel jbutton


【解决方案1】:

我拿走了你的代码,清理了它,并在第一页为JButtons 添加了ActionListener

这是 GUI 的第一页。

这是 GUI 第二页。

我做的第一件事就是摆脱绝对定位。我在两个JPanels 上都使用了GridBagLayout

接下来我要做的是将鼠标移入和移出的按钮颜色分解出来。

我创建了一个应用程序模型来保存我看到的变量。最终,该模型将保存整个应用程序的所有变量。

我将所有类都设置为内部类,这样我就可以将这段代码复制并粘贴到一个块中。您可以而且应该将我的内部类分成单独的类。

这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Covid19GUI extends JFrame {

    private static final long serialVersionUID = 1L;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Covid19GUI();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    private Covid19Model model;

    private JPanel contentPane;

    /**
     * Create the frame.
     */
    public Covid19GUI() {
        this.model = new Covid19Model();
        
        setTitle("COVID'19 Management System");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new CardLayout(5, 5));

        JPanel p1 = new LoginAs(this, model);
        contentPane.add(p1, "t1");

        JPanel p2 = new LoginPanel(this, model);
        contentPane.add(p2, "t2");

        JPanel p3 = new JPanel();
        contentPane.add(p3, "t3");

        JPanel p4 = new JPanel();
        contentPane.add(p4, "t4");

        add(contentPane, BorderLayout.CENTER);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }
    
    public JPanel getCardLayoutPanel() {
        return contentPane;
    }

    public class LoginAs extends JPanel {

        private static final long serialVersionUID = 1L;

        /**
         * Create the panel.
         */
        public LoginAs(Covid19GUI frame, Covid19Model model) {      
            setBackground(Color.BLACK);
            setLayout(new GridBagLayout());
            
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(5, 5, 5, 5);
            gbc.gridx = 0;
            gbc.gridy = 0;

            JLabel lblNewLabel = new JLabel("COVID'19 Management System");
            lblNewLabel.setForeground(Color.WHITE);
            lblNewLabel.setFont(new Font("Calibri", Font.BOLD, 18));
            add(lblNewLabel, gbc);

            gbc.gridy++;
            JLabel lblNewLabel_1 = new JLabel("LOGIN AS");
            lblNewLabel_1.setForeground(Color.WHITE);
            lblNewLabel_1.setFont(new Font("Calibri", Font.BOLD, 16));
            add(lblNewLabel_1, gbc);
            
            ButtonListener listener = new ButtonListener(frame, model);

            gbc.gridy++;
            JButton btnAdmin = new JButton("ADMIN");
            btnAdmin.addActionListener(listener);
            btnAdmin.addMouseListener(new ColorListener(btnAdmin));
            btnAdmin.setForeground(Color.BLACK);
            btnAdmin.setBackground(Color.WHITE);
            btnAdmin.setBorderPainted(false);
            add(btnAdmin, gbc);
            
            gbc.gridy++;
            JButton btnStaff = new JButton("STAFF");
            btnStaff.addActionListener(listener);
            btnStaff.addMouseListener(new ColorListener(btnStaff));         
            btnStaff.setForeground(Color.BLACK);
            btnStaff.setBackground(Color.WHITE);
            btnStaff.setBorderPainted(false);
            add(btnStaff, gbc);
        }
    }
    
    public class ColorListener extends MouseAdapter {
        
        private JButton button;
        
        public ColorListener(JButton button) {
            this.button = button;
        }

        @Override
        public void mouseEntered(MouseEvent evt) {
            button.setBackground(Color.GREEN);
        }

        @Override
        public void mouseExited(MouseEvent evt) {
            button.setBackground(Color.WHITE);
        }
    }
    
    public class ButtonListener implements ActionListener {
        
        private Covid19GUI frame;
        
        private Covid19Model model;

        public ButtonListener(Covid19GUI frame, Covid19Model model) {
            this.frame = frame;
            this.model = model;
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            if (button.getText().equals("ADMIN")) {
                model.setAdmin(true);
            } else {
                model.setAdmin(false);
            }
            
            JPanel panel = frame.getCardLayoutPanel();
            CardLayout layout = (CardLayout) panel.getLayout();
            layout.show(panel, "t2");
        }
        
    }

    public class LoginPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        
        private Covid19GUI frame;
        
        private Covid19Model model;

        private JTextField txt_uname;
        private JTextField txt_pass;

        /**
         * Create the panel.
         */
        public LoginPanel(Covid19GUI frame, Covid19Model model) {
            this.frame = frame;
            this.model = model;
            
            setBackground(Color.BLACK);
            setLayout(new GridBagLayout());
            
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(5, 5, 5, 5);
            gbc.gridwidth = 2;
            gbc.gridx = 0;
            gbc.gridy = 0;

            JLabel lblNewLabel = new JLabel("COVID'19 Management System");
            lblNewLabel.setForeground(Color.WHITE);
            lblNewLabel.setFont(new Font("Calibri", Font.BOLD, 18));
            add(lblNewLabel, gbc);

            gbc.gridy++;
            JLabel lblNewLabel_1 = new JLabel("LOGIN PAGE");
            lblNewLabel_1.setForeground(Color.WHITE);
            lblNewLabel_1.setFont(new Font("Calibri", Font.BOLD, 16));
            add(lblNewLabel_1, gbc);

            gbc.gridwidth = 1;
            gbc.gridy++;
            JLabel lblNewLabel_2 = new JLabel("Username");
            lblNewLabel_2.setFont(new Font("Calibri", Font.BOLD, 14));
            lblNewLabel_2.setForeground(Color.WHITE);
            add(lblNewLabel_2, gbc);

            gbc.gridx++;
            txt_uname = new JTextField();
            txt_uname.setColumns(10);
            add(txt_uname, gbc);
            
            gbc.gridx = 0;
            gbc.gridy++;
            JLabel lblNewLabel_2_1 = new JLabel("Password");
            lblNewLabel_2_1.setForeground(Color.WHITE);
            lblNewLabel_2_1.setFont(new Font("Calibri", Font.BOLD, 14));
            add(lblNewLabel_2_1, gbc);

            gbc.gridx++;
            txt_pass = new JTextField();
            txt_pass.setColumns(10);
            add(txt_pass, gbc);

            gbc.gridwidth = 2;
            gbc.gridx = 0;
            gbc.gridy++;
            JButton btn_login = new JButton("Login");
            add(btn_login, gbc);
        }
        
        public String getUserName() {
            return txt_uname.getText().trim();
        }
        
        public String getPassword() {
            return txt_pass.getText().trim();
        }
        
    }
    
    public class Covid19Model {
        
        private boolean isAdmin;
        
        private String userName;
        private String password;
        
        public boolean isAdmin() {
            return isAdmin;
        }
        
        public void setAdmin(boolean isAdmin) {
            this.isAdmin = isAdmin;
        }
        
        public String getUserName() {
            return userName;
        }
        
        public void setUserName(String userName) {
            this.userName = userName;
        }
        
        public String getPassword() {
            return password;
        }
        
        public void setPassword(String password) {
            this.password = password;
        }
        
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多