简体   繁体   English

使用多个 JPanel 类时如何在 JFrame 中打开另一个 JPanel

[英]How to open another JPanel in JFrame when using multiple JPanel classes

I have been looking for this for days now and I haven't found a solution so I just made my account.我一直在寻找这个几天,但我还没有找到解决方案,所以我只是创建了我的帐户。

I want to figure out how should I open another JPanel from the current JPanel in my JFrame.我想弄清楚我应该如何从我的 JFrame 中的当前 JPanel 中打开另一个 JPanel。 I have made three classes till now in the view package: mainView (JFrame), loginAs (JPanel), loginPanel (JPanel).到目前为止,我已经在 package 视图中创建了三个类:mainView (JFrame)、loginAs (JPanel)、loginPanel (JPanel)。 What I want to do is, I want to display loginAs Panel as my first panel where user selects whether he is an Admin or Staff and then after clicking the desired option, it should open the loginPanel where they can log in. I'm stuck here as I don't know how I can give back the information to mainView that the user has selected Admin or Staff and that mainView should now move to the next panel that is loginPanel.我想要做的是,我想将 loginAs 面板显示为我的第一个面板,用户可以在其中选择他是管理员还是员工,然后单击所需的选项后,它应该打开他们可以登录的 loginPanel。我被卡住了在这里,因为我不知道如何将用户选择了 Admin 或 Staff 的信息返回给 mainView,并且 mainView 现在应该移动到下一个面板,即 loginPanel。

Here is the coding I did:这是我做的编码:

mainView主视图

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

}

loginAs登录身份

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

loginPanel登录面板

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

    }
}

Here is the visual representation:这是视觉表示:

这是显示的内容,用户可以在其中选择 Admin 或 Staff

这是接下来应该出现的内容

I took your code, cleaned it up, and added an ActionListener for the JButtons on the first page.我拿走了你的代码,清理了它,并在第一页为JButtons添加了一个ActionListener

Here's the GUI first page.这是 GUI 的第一页。

第一页

Here's the GUI second page.这是 GUI 第二页。

第二页

The first thing I did was to get rid of the absolute positioning.我做的第一件事就是摆脱绝对定位。 I used a GridBagLayout on both JPanels .我在两个JPanels上都使用了GridBagLayout

The next thing I did was to factor out the button coloring on a mouse in and mouse out.接下来我做的是将鼠标移入和移出的按钮颜色分解出来。

I created an application model to hold the variables I've seen.我创建了一个应用程序 model 来保存我看到的变量。 Eventually, this model will hold all the variables for the entire application.最终,这个 model 将保存整个应用程序的所有变量。

I made all the classes inner classes so I can copy and paste this code into one block.我把所有的类都做成了内部类,这样我就可以把这段代码复制粘贴到一个块中。 You can and should separate my inner classes into separate classes.您可以并且应该将我的内部类分成单独的类。

Here's the complete runnable code.这是完整的可运行代码。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM