Java Swing

在使用Java Swing进行界面跳转时遇到一个问题,首次跳转正常,但当返回原界面后,文本框检测出现失效情况。寻求解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java Swing 跳转界面,控件失效检测不了是怎么回事?

这是原界面

package com.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.JDBC.DB.JDBC;
import com.dao.UserDao;
import com.util.User;

public class LoginView extends JFrame {

	private static JPanel contentPane = new JPanel();
	private static JLabel idLabel, pwdLabel;
	private static JTextField idField;
	private static JPasswordField pwdField;
	private static JButton loginButton, resetButton;

	public LoginView() {
		// TODO Auto-generated constructor stub
		initcomponents();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setBackground(Color.white);
		contentPane.setLayout(null);
		setContentPane(contentPane);
		setTitle("登陆界面");
		setSize(400, 250);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	private void initcomponents() {
		// TODO Auto-generated method stub

		idLabel = new JLabel("账    号:");
		idLabel.setBounds(60, 30, 90, 30);
		contentPane.add(idLabel);

		idField = new JTextField();
		idField.setBounds(150, 30, 180, 30);
		contentPane.add(idField);

		pwdLabel = new JLabel("密    码:");
		pwdLabel.setBounds(60, 90, 90, 30);
		contentPane.add(pwdLabel);

		pwdField = new JPasswordField();
		pwdField.setBounds(150, 90, 180, 30);
		contentPane.add(pwdField);

		loginButton = new JButton("登    录");
		loginButton.setBounds(90, 160, 90, 30);
		loginButton.setFocusPainted(false);
		loginButton.setContentAreaFilled(false);
		loginButton.addActionListener(new loginButtonActionListener());
		contentPane.add(loginButton);

		resetButton = new JButton("重    置");
		resetButton.setBounds(220, 160, 90, 30);
		resetButton.setFocusPainted(false);
		resetButton.setContentAreaFilled(false);
		resetButton.addActionListener(new resetButtonActionListener());
		contentPane.add(resetButton);

		// 字体设置
		Font font = new Font("微软雅黑", Font.PLAIN, 18);
		idLabel.setFont(font);
		pwdLabel.setFont(font);
		loginButton.setFont(font);
		resetButton.setFont(font);
	}

	private class loginButtonActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String userID = idField.getText();
			String userPwd = String.valueOf(pwdField.getPassword());
			if (userID.length() == 0 && userID.equals("")) {
				JOptionPane.showMessageDialog(null, "账号不能为空");
				return;
			}
			if (userPwd.length() == 0 && userPwd.equals("")) {
				JOptionPane.showMessageDialog(null, "密码不能为空");
				return;
			}
			if (!userID.equals("user") && !userPwd.equals("password")) {
				JOptionPane.showMessageDialog(null, "登陆失败");
			}

			dispose();
			JOptionPane.showMessageDialog(null, "登录成功");
			new UserView();
		}

	}

	private class resetButtonActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			idField.setText(null);
			pwdField.setText(null);
		}

	}

	public static void main(String[] args) {
		new LoginView();
	}

}

这是要跳转的界面

package com.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class UserView extends JFrame {
	
	private static JPanel contentPane = new JPanel();
	private static JMenuBar menuBar;
	private static JMenu menu;
	private static JMenuItem logingMenu;
	
	public UserView() {
		initcomponents();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setBackground(Color.white);
		contentPane.setLayout(null);
		setContentPane(contentPane);
		setTitle("主界面");
		setSize(400, 250);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	private void initcomponents() {
		// TODO Auto-generated method stub
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		menu = new JMenu("系  统");
		menuBar.add(menu);
		
		logingMenu = new JMenuItem("注销");
		logingMenu.addActionListener(new loginmenuActionListener());
		menu.add(logingMenu);
	}
	
	private class loginmenuActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			dispose();
			new LoginView();
		}
		
	}
	
}

这些是运行后的图片
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
第一次跳转能正常运行,然而,再跳转回到原来的界面
在这里插入图片描述
在这里插入图片描述
文本框检测失效了。。
求助各位大佬,帮忙解决一下,感激不尽。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值