如何实现JAVA的页面跳转

//我的第一个界面
package UserDaoService;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import User.dao.UserDao;
import UserDaoimpl.UserDaoimpl;
import bymyself.User;

import javax.print.DocFlavor.STRING;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class MainUI extends JFrame implements ActionListener{

private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private userservice userService = new userservice();

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

/**
 * Create the application.
 */
public MainUI() {
	initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
	
	
	frame = new JFrame();
	frame.getContentPane().setBackground(new Color(255, 182, 193));
	frame.setBounds(100, 100, 286, 325);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(null);
	
	JLabel lblNewLabel = new JLabel("\u53F7\u7801");
	lblNewLabel.setBounds(34, 27, 72, 18);
	frame.getContentPane().add(lblNewLabel);
	
	textField = new JTextField();
	textField.setBounds(126, 24, 86, 24);
	frame.getContentPane().add(textField);
	textField.setColumns(10);
	
	JLabel lblNewLabel_1 = new JLabel("\u540D\u5B57");
	lblNewLabel_1.setBounds(34, 80, 72, 18);
	frame.getContentPane().add(lblNewLabel_1);
	
	textField_1 = new JTextField();
	textField_1.setBounds(126, 77, 86, 24);
	frame.getContentPane().add(textField_1);
	textField_1.setColumns(10);
	
	JLabel lblNewLabel_2 = new JLabel("\u5E74\u9F84");
	lblNewLabel_2.setBounds(34, 147, 72, 18);
	frame.getContentPane().add(lblNewLabel_2);
	
	textField_2 = new JTextField();
	textField_2.setBounds(126, 144, 86, 24);
	frame.getContentPane().add(textField_2);
	textField_2.setColumns(10);
	
	JButton button = new JButton("登录");
	button.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			String s1=textField.getText();
			int S1=Integer.parseInt(s1);
			String s2=textField_1.getText();
			String s3=textField_2.getText();
			int S3=Integer.parseInt(s3);
			User user=new User(S3, s3, S3);
			user.setNumber(S1);
			user.setName(s2);
			user.setAge(S3);
			try {
				userService.login(user);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			setVisible(false);
			diercengjiemian dier=new diercengjiemian();
			
		}
	});
	button.setBounds(126, 212, 113, 27);
	frame.getContentPane().add(button);
}

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

}
其中关键两行代码是:
setVisible(false);
diercengjiemian dier=new diercengjiemian();
//下面是我的第二个界面
package UserDaoService;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class diercengjiemian extends JFrame{

private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;

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

/**
 * Create the application.
 */
public diercengjiemian() {
	initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

	
	frame = new JFrame();
	frame.setBounds(100, 100, 624, 530);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(null);
	
	JButton button = new JButton("\u589E\u52A0");
	button.setBounds(274, 71, 113, 27);
	frame.getContentPane().add(button);
	
	JButton button_1 = new JButton("\u5220\u9664");
	button_1.setBounds(274, 137, 113, 27);
	frame.getContentPane().add(button_1);
	
	JButton button_2 = new JButton("\u4FEE\u6539");
	button_2.setBounds(274, 207, 113, 27);
	frame.getContentPane().add(button_2);
	
	JButton button_3 = new JButton("\u67E5\u8BE2");
	button_3.setBounds(274, 272, 113, 27);
	frame.getContentPane().add(button_3);
	
	JLabel lblNewLabel = new JLabel("\u53F7\u7801:");
	lblNewLabel.setBounds(14, 75, 72, 18);
	frame.getContentPane().add(lblNewLabel);
	
	JLabel lblNewLabel_1 = new JLabel("\u59D3\u540D");
	lblNewLabel_1.setBounds(14, 141, 72, 18);
	frame.getContentPane().add(lblNewLabel_1);
	
	JLabel lblNewLabel_2 = new JLabel("\u5E74\u9F84");
	lblNewLabel_2.setBounds(14, 211, 72, 18);
	frame.getContentPane().add(lblNewLabel_2);
	
	textField = new JTextField();
	textField.setBounds(112, 74, 86, 24);
	frame.getContentPane().add(textField);
	textField.setColumns(10);
	
	textField_1 = new JTextField();
	textField_1.setBounds(112, 138, 86, 24);
	frame.getContentPane().add(textField_1);
	textField_1.setColumns(10);
	
	textField_2 = new JTextField();
	textField_2.setBounds(112, 208, 86, 24);
	frame.getContentPane().add(textField_2);
	textField_2.setColumns(10);
	
	textField_3 = new JTextField();
	textField_3.setBounds(240, 328, 328, 96);
	frame.getContentPane().add(textField_3);
	textField_3.setColumns(10);
	
	JLabel lblNewLabel_3 = new JLabel("\u6570\u636E\u5E93\u4FE1\u606F:");
	lblNewLabel_3.setBounds(127, 331, 99, 27);
	frame.getContentPane().add(lblNewLabel_3);
	frame.setVisible(true);
}

}
把try里面 diercengjiemian window = new diercengjiemian();
后一条语句window.frame.setVisable(true);剪切粘贴到
initialize函数的末尾并改成frame.setVisable(ture);

以上两步即可实现页面跳转。

你可能感兴趣的:(java实现页面跳转)