在Java程序中打开另一个面板后,面板将不会处理

时间:2019-03-05 12:57:17

标签: java swing

我正在用Java开发药房管理系统,并使用窗口构建器创建了一个注册面板。在该窗口构建器中,有一个文本显示单击此处登录。当我单击该文本时,它将打开登录面板,但不丢弃注册面板。我只想要一个可以告诉我如何在单击登录面板时处理注册面板的人。这是我编写的代码。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.UIManager;
import java.awt.SystemColor;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class SignUp extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;

int xx;
int xy;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SignUp frame = new SignUp();
                frame.setUndecorated(false);
                frame.setResizable(false);
                frame.setLocation(200, 10);
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    }

     public SignUp() {
    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            int x = e.getXOnScreen();
            int y = e.getXOnScreen();
            SignUp.this.setLocation(x-xx, y-xy);

        }
    });
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            xx=e.getX();
            xy=e.getY();
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            SignUp frame = new SignUp();
            frame.setResizable(false);
            frame.setVisible(false);
            frame.dispose();
        }
    });
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1000, 700);
    contentPane = new JPanel();
    contentPane.setForeground(Color.GREEN);
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    //contentPane.setUndecorated(true)
    contentPane.setVisible(true);
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBackground(Color.GREEN);
    panel.setBounds(0, 0, 389, 681);
    contentPane.add(panel);
    panel.setLayout(null);

    JLabel lblNewLabel_3 = new JLabel("");
    lblNewLabel_3.setBounds(57, 182, 256, 256);
    panel.add(lblNewLabel_3);


     JLabel lblNewLabel_4 = new JLabel("Login by clicking here");
     lblNewLabel_4.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            Login log = new Login();
            log.setResizable(false);
            log.show();
            log.setLocation(200, 10);
            log.setVisible(true);

            dispose();  
        }
        });


    lblNewLabel_4.setForeground(Color.WHITE);
    lblNewLabel_4.setFont(new Font("Arial", Font.BOLD, 17));
    lblNewLabel_4.setBounds(85, 604, 192, 20);
    panel.add(lblNewLabel_4);

    JButton btnNewButton = new JButton("Sign Up");
    btnNewButton.setForeground(Color.WHITE);
    btnNewButton.setBackground(new Color(0, 255, 0));
    btnNewButton.setBounds(519, 608, 436, 34);
    contentPane.add(btnNewButton);
    //Main_Page frame = new Main_Page();

    textField = new JTextField();
    textField.setBounds(519, 150, 426, 34);
    contentPane.add(textField);
    textField.setColumns(10);

    JLabel lblUsername = new JLabel("Username");
    lblUsername.setFont(new Font("Arial", Font.BOLD, 10));
    lblUsername.setBounds(519, 130, 60, 14);
    contentPane.add(lblUsername);

    textField_1 = new JTextField();
    textField_1.setBounds(519, 250, 426, 34);
    contentPane.add(textField_1);
    textField_1.setColumns(10);

    JLabel lblEmail = new JLabel("Email");
    lblEmail.setFont(new Font("Arial", Font.BOLD, 10));
    lblEmail.setBounds(519, 230, 46, 14);
    contentPane.add(lblEmail);

    textField_2 = new JTextField();
    textField_2.setBounds(519, 350, 426, 34);
    contentPane.add(textField_2);
    textField_2.setColumns(10);

    JLabel lblNewLabel = new JLabel("Password");
    lblNewLabel.setFont(new Font("Arial", Font.BOLD, 10));
    lblNewLabel.setBounds(519, 330, 60, 14);
    contentPane.add(lblNewLabel);

    textField_3 = new JTextField();
    textField_3.setBounds(519, 450, 426, 34);
    contentPane.add(textField_3);
    textField_3.setColumns(10);

    JLabel lblNewLabel_1 = new JLabel("Confirm Password");
    lblNewLabel_1.setFont(new Font("Arial", Font.BOLD, 10));
    lblNewLabel_1.setBounds(519, 430, 101, 14);
    contentPane.add(lblNewLabel_1);

    JLabel lblNewLabel_2 = new JLabel("New label");
    lblNewLabel_2.setBounds(583, 28, 284, 74);
    contentPane.add(lblNewLabel_2);
}
}

1 个答案:

答案 0 :(得分:0)

您正在创建 new SignUp,将其设置为不可见并进行处理。这是没有用的,因为您想要隐藏(或处置)现有的 ,可见的SignUp。无需创建新方法,而需要在现有方法上调用适当的方法。您也许可以通过MouseEvent的来源来实现这一点,但是否则,您需要安排其他方法让侦听器获取它。

更新

此问题中已经提供了更完整的代码,很明显,所涉及的MouseListenerSignUp的内部类。假设要处置的SignUp实例始终是它自己的外部实例,它可以直接(似乎)在该实例上调用方法。属于“收听者获得它的其他方式”。