java gui和多线程

怎么样才能用多线程实现同时打开多个同样的gui窗口啊😢有点急

import javax.swing.*;

public class MultiThreadedGUI implements Runnable {
    public void run() {
        // 创建 GUI 窗口
        JFrame frame = new JFrame("GUI 窗口");

        // 设置窗口属性、组件等

        // 显示窗口
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        int numWindows = 5; // 打开的窗口数量

        for (int i = 0; i < numWindows; i++) {
            // 创建新的线程
            Thread thread = new Thread(new MultiThreadedGUI());
            thread.start();
        }
    }
}


gui就应该在主线程里运行,你直接show多个窗口就行了,这跟多线程有什么关系
如果你需要在线程里操作IO,那你在每个窗口的实例里再开线程就行了,不要把gui本身放线程里面去

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7600967
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:Java GUI界面之简易计算器的制作以及非GUI实现(中缀表达式,后缀表达式)
  • 你还可以看下java参考手册中的 java-用Swing创建GUI - 全面介绍在Java平台上创建GUI。-拖放和数据传输》告诉你在你的应用程序中实现数据传输所需的知识。-跌落位置渲染图
  • 除此之外, 这篇博客: Java GUI 连接数据库完成登录注册页面中的 运用GUI连接数据库进行登录注册 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 登录页面
    在这里插入图片描述

    详细代码

    package com.itheima.denglu;
    
    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.LayoutManager;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.itheima.mian.showlist;
    import com.itheima.sql.connectionlain;
    
    public class login {
    	private JFrame frame;
    	private JTextField textField;
    	private JPasswordField passwordField;
    	private JButton quitBt;
    	
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					login window = new login();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public login() {
    		initialize();
    	}
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		
    		frame = new JFrame();
    		frame.setBounds(400, 200, 450, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		frame.setResizable(false);
    		frame.getContentPane().setLayout(null);
    		
    		 JLabel j1=new JLabel("***欢迎登陆学生信息管理系统***");
    		 j1.setBounds(120, 26, 180, 15);
    		 frame.getContentPane().add(j1);
             
    
    		JLabel lblNewLabel = new JLabel("用户名:");
    		lblNewLabel.setBounds(100, 56, 54, 15);
    		frame.getContentPane().add(lblNewLabel);
    
    		JLabel lblNewLabel_1 = new JLabel("密码:");
    		lblNewLabel_1.setBounds(100, 102, 54, 15);
    		frame.getContentPane().add(lblNewLabel_1);
    
    		textField = new JTextField();
    		textField.setBounds(196, 53, 100, 21);
    		frame.getContentPane().add(textField);
    		textField.setColumns(10);
    
    		passwordField = new JPasswordField();
    		passwordField.setBounds(196, 96,100, 21);
    		frame.getContentPane().add(passwordField);
    
    		JButton btnNewButton = new JButton("登录");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    
    				connectionlain c = new connectionlain();
    
    				Connection conn = c.getConnect();
    
    				PreparedStatement ps = null;
    				ResultSet rs = null;
    
    				try {
    					ps = conn
    							.prepareStatement("select *from user where name=? and password=?");
    
    					ps.setString(1, textField.getText());
    					ps.setString(2, passwordField.getText());
    
    					rs = ps.executeQuery();
    					if (rs.next()) {
    						try {
    							new login();
    							frame.removeNotify();
    						} catch (ClassNotFoundException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    						
    
    					} else {
    						JOptionPane pane = new JOptionPane("用户或密码错误");
    	                     JDialog dialog  = pane.createDialog("警告");
    	                     dialog.show();
    					}
    
    				} catch (SQLException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    
    			}
    
    			
    
    			
    		});
    		btnNewButton.setBounds(41, 169, 93, 23);
    		frame.getContentPane().add(btnNewButton);
    
    		JButton btnNewButton_1 = new JButton("注册");
    		btnNewButton_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    
    				new add();
    				frame.removeNotify();
    
    			}
    		});
    		btnNewButton_1.setBounds(183, 169, 93, 23);
    		frame.getContentPane().add(btnNewButton_1);
    		
    		JButton j2 = new JButton("退出");
    		j2.setBounds(323, 169, 93, 23);
            frame.getContentPane().add(j2);
            j2.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO 自动生成的方法存根
                    if(e.getActionCommand().equals("退出")){
                        System.exit(0);
                    }
                }
            });
            
    		
    		
    	}
    
    	private void add(JButton quitBt2) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	private Container getContentPane() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    
    }
    

    注册页面
    在这里插入图片描述

    详细代码

    package com.itheima.denglu;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    
    import java.awt.event.ActionListener;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    
    import com.itheima.sql.connectionlain;
    
    public class add {
    	private JFrame frame;
    	private JTextField textField;
    	private JTextField textField_1;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					add window = new add();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public add() {
    		initialize();
    	}
    	
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(400, 200, 450, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		frame.setResizable(false);
    		frame.getContentPane().setLayout(null);
    
    		JLabel lblNewLabel = new JLabel("用户名:");
    		lblNewLabel.setBounds(100, 56, 54, 15);
    		frame.getContentPane().add(lblNewLabel);
    
    		JLabel lblNewLabel_1 = new JLabel("密码:");
    		lblNewLabel_1.setBounds(100, 102, 54, 15);
    		frame.getContentPane().add(lblNewLabel_1);
    
    		textField = new JTextField();
    		textField.setBounds(196, 53, 100, 21);
    		frame.getContentPane().add(textField);
    		textField.setColumns(10);
    
    		textField_1 = new JTextField();
    		textField_1.setBounds(196, 96,100, 21);
    		frame.getContentPane().add(textField_1);
    		textField_1.setColumns(10);
    
    		JButton btnNewButton = new JButton("注册");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				connectionlain c = new connectionlain();
    
    				Connection conn = c.getConnect();
    
    				PreparedStatement ps = null;
    				try {
    					ps = conn
    							.prepareStatement("insert into user values(default, ?,?)");
    
    					ps.setString(1, textField.getText());
    					ps.setString(2, textField_1.getText());
    					ps.execute();
    
    					new prompt();
    					frame.removeNotify();
    				//  System.out.println("成功");
    
    				} catch (SQLException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    
    			}
    		});
    		btnNewButton.setBounds(81, 169, 93, 23);
    		frame.getContentPane().add(btnNewButton);
    		
    		JButton returnButton = new JButton("返回");
    		returnButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    
    				new login();
    				frame.removeNotify();
    
    			}
    		});
    		returnButton.setBounds(223, 169, 93, 23);
    		frame.getContentPane().add(returnButton);
    
    	}
    
    }
    
    

    页面提示
    在这里插入图片描述

    详细代码

    package com.itheima.denglu;
    
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class prompt {
    	private JFrame frame;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					prompt window = new prompt();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public prompt() {
    		initialize();
    	}
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(400, 200, 450, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    
    		frame.getContentPane().setLayout(null);
    		
    		JLabel lblNewLabel = new JLabel("注册成功 !");
    		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 41));
    		lblNewLabel.setBounds(77, 51, 215, 46);
    		frame.getContentPane().add(lblNewLabel);
    		
    		JButton btnNewButton = new JButton("返回登录");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				new login();
    				frame.removeNotify();
    				
    				
    			}
    		});
    		btnNewButton.setBounds(157, 153, 103, 33);
    		frame.getContentPane().add(btnNewButton);
    	}
    
    }
    
    

    数据库连接

    package com.itheima.sql;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class connectionlain {
    	
    	public Connection getConnect() {
    		Connection conn=null;
    		String url="jdbc:mysql://127.0.0.1:3306/student";
    		String user="root";
    		String password="root";
    		try{
    			Class.forName("com.mysql.jdbc.Driver");
    			conn=DriverManager.getConnection(url, user, password);
    			return conn;
    		}catch(ClassNotFoundException e){
    		    System.out.println("错误");
    		    e.printStackTrace();
    		    return null;
    		}catch(Exception e){
    			e.printStackTrace();
    			return null;
    		}
    		
    	}
    	
    
    }
    
  • 您还可以看一下 张旭老师的【拯救者】javaSwing界面速成GUI(含实战)课程中的 08菜单组件小节, 巩固相关知识点