Java Swing+Mysql電影購票系統(普通用戶/管理員登錄+充值購票+影院管理)

Java Swing+Mysql電影購票系統

文章目錄

電影購票系統模擬真實購票流程,在線選座,充值購票,影院信息管理。登錄用戶分爲:普通用戶+管理員

數據庫連接

BaseDao類,建立數據庫連接
代碼如下:

package daoimpl;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BaseDao {
	
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String URL = "jdbc:mysql://localhost:3306/moviesystem?useUnicode=true&characterEncoding=UTF-8";
	private static final String USER = "root";
	private static final String PASSWORD = "123456";

	static{
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 與數據庫建立連接
	 * 
	 * @return
	 */
	public static Connection getconn() {
		Connection conn = null;
		
		try {
			conn = DriverManager.getConnection(URL,USER,PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 釋放所有資源
	 * 
	 */
	public static void closeAll(ResultSet rs, PreparedStatement psts, Connection conn) {

		try {
			if (rs != null) {
				rs.close();
			}
			if (psts != null) {
				psts.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 此方法可以完成對數據庫的增刪改操作
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */

	public static boolean operUpdata(String sql, List<Object> params) {
		int res = 0;//返回影響的行數
		Connection conn = null;
		PreparedStatement psts = null;//執行sql語句
		ResultSet rs = null;

		try {
			conn = getconn();// 與數據庫建立連接
			psts = conn.prepareStatement(sql);// 裝載sql語句
			if (params != null) {// 假如有?號,在執行之前把問號替換掉
				for(int i = 0; i < params.size(); i++) {
					psts.setObject(i + 1, params.get(i));
				}
			}
			res = psts.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs, psts, conn);
		}
		return res > 0 ? true : false;
	}

	/*
	 * 實現查的操作
	 * 
	 */
	
	public static <T> List<T> operQuery(String sql, List<Object> p, Class<T> cls)throws Exception {
		Connection conn = null;
		PreparedStatement pste = null;// 預處理語句
		ResultSet rs = null;// 結果集
		List<T> list = new ArrayList<T>();
		conn = getconn();
		try {
			pste = conn.prepareStatement(sql);
			if (p != null) {// 將條件值裝入預處理語句
				for (int i = 0; i < p.size(); i++) {
					pste.setObject(i + 1, p.get(i));
				}
			}
			
			rs = pste.executeQuery();// 執行
			ResultSetMetaData rsmd = rs.getMetaData();
			while (rs.next()) {
				T entity = cls.newInstance();// 反射
				for (int j = 0; j < rsmd.getColumnCount(); j++) {
					// 從數據庫中取得字段名
					String col_name = rsmd.getColumnName(j + 1);
					Object value = rs.getObject(col_name);
					Field field = cls.getDeclaredField(col_name);
					field.setAccessible(true);// 類中的成員變量爲private,故必須進行此操作
					field.set(entity, value);// 給實體類entity的field屬性賦值
				}
				list.add(entity);// 加入list列表
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs, pste, conn);// 關閉連接,釋放資源
		}
		return list;
	
	}

}

主要頁面

登錄頁面:LoginUI

package view;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import entity.User;
import service.UserService;
import serviceimpl.UserServiceImpl;



class Login implements ActionListener {
	private JFrame jf = new JFrame("電影系統");
	private Container con = jf.getContentPane();// 獲得面板

	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	private Dimension sc = toolkit.getScreenSize();// 獲得屏幕尺寸
	private JLabel title = new JLabel("歡迎使用電影購票系統");
	private JLabel name1 = new JLabel("用戶名");
	private JLabel pass1 = new JLabel("密  碼");
	private JTextField textName = new JTextField();
	private JPasswordField textPs = new JPasswordField();// 密碼框

	private JRadioButton choice1 = new JRadioButton("用戶");
	private JRadioButton choice2 = new JRadioButton("管理員");

	private JLabel code1 = new JLabel("驗證碼");
	private JTextField textCode = new JTextField();
	private JLabel code2 = new JLabel();

	private JButton btn_login = new JButton("登錄");
	private JButton btn_rgist = new JButton("註冊");
	
	// 按鈕

	private Font font = new Font("楷體", 1, 28);
	private Font font1 = new Font("楷體", 0, 20);
	private Font font2 = new Font("楷體", 0, 18);
	// 字體,樣式(粗體,斜體),大小

	private ButtonGroup buttongroup = new ButtonGroup();

	private ImageIcon loginbg = new ImageIcon("image/loginbg.jpg");
	
	
	
	public Login() {
		con.setLayout(null);
		jf.setSize(1000, 618);
		jf.setLocation((sc.width - 1000) / 2, (sc.height - 618) / 2);

		jf.setResizable(false);// 窗口大小不可變
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		con.setVisible(true);

		JLabel maxlabel = new JLabel(loginbg);
		
		title.setBounds(375, 10, 370, 30);
		title.setFont(font);
//		title.setForeground(Color.black);
		title.setForeground(Color.white);

		name1.setBounds(140, 140, 85, 30);// 賬號的位置大小
		name1.setFont(font1);// 字體
		name1.setForeground(Color.red);// name1字的顏色

		pass1.setBounds(140, 190, 85, 30);// 密碼的位置大小
		pass1.setForeground(Color.red);
		pass1.setFont(font1);

		textName.setBounds(200, 143, 140, 25);
		textName.setBorder(null);// 邊框
		textName.setFont(font1);
		textPs.setBounds(200, 193, 140, 25);
		textPs.setBorder(null);
		textPs.setEchoChar('*');// 可以將密碼顯示爲* ;默認爲·
		textPs.setFont(font1);
		choice1.setBounds(140, 290, 120, 25);
		choice1.setSelected(true);// 默認普通用戶登錄
		choice2.setBounds(260, 290, 80, 25);

		code1.setBounds(140, 240, 60, 25);
		code1.setFont(font1);
		code1.setForeground(Color.black);
		textCode.setBounds(200, 240, 95, 25);
		textCode.setBorder(null);
		textCode.setFont(font1);
		code2.setBounds(300, 240, 70, 25);
		code2.setFont(font1);
		code2.setText(code());
		code2.setForeground(Color.black);
		code1.setVisible(false);
		code2.setVisible(false);
		textCode.setVisible(false);

		btn_login.setBounds(140, 340, 90, 25);//140, 340, 90, 25
		btn_login.setFont(font2);
		btn_login.addActionListener(this);
		jf.getRootPane().setDefaultButton(btn_login);//回車默認是登錄按鈕
		
		
		btn_rgist.setBounds(250, 340, 90, 25);//250, 340, 90, 25
		btn_rgist.setFont(font2);
		btn_rgist.addActionListener(this);

		
		JLabel bg = new JLabel(loginbg);
		
		maxlabel.add(title);
		maxlabel.add(name1);
		maxlabel.add(pass1);
		maxlabel.add(textName);
		maxlabel.add(textPs);
		maxlabel.add(choice1);
		maxlabel.add(choice2);

		buttongroup.add(choice1);
		buttongroup.add(choice2);

		maxlabel.add(code1);
		maxlabel.add(code2);
		maxlabel.add(textCode);
		maxlabel.add(btn_login);
		maxlabel.add(btn_rgist);
		
		
		maxlabel.setBounds(0, 0, 999, 617);
		con.add(maxlabel);

	}

	private String code() {
		int m = 0;
		for (int i = 0; i < 4; i++) {
			m *= 10;
			m += (int) (Math.random() * 9 + 1);
		}
		return ((Integer) m).toString();
	}

	public void cleanUserInfo() {
		this.textName.setText("");
		this.textPs.setText("");
		this.textCode.setText("");
	}

	public static void winMessage(String str) {// 提示窗口,有多個地方調用
		JOptionPane.showMessageDialog(null, str, "提示",
				JOptionPane.INFORMATION_MESSAGE);
	}

	

	@Override
	public void actionPerformed(ActionEvent ac) {
		
		
		if (ac.getSource() == this.btn_login) {
			String name = textName.getText();
			String pswd = new String(textPs.getPassword());
			
			if (name.equals("") || pswd.equals("")) {
				winMessage("賬號、密碼不能爲空!");
				cleanUserInfo();
				this.code2.setText(code());
			} else {
//				String code1 = textCode.getText();
				
				int code1 = 1;
				
//				String code = code2.getText();
//				if(code1.equals("")){
//					winMessage("驗證碼不能爲空!");
//					return;
//				}
				if (code1==1) {
					int choice;
					if (choice1.isSelected())
						choice = 0;
					else
						choice = 1;
					UserService userBiz = new UserServiceImpl();
					
					User user = userBiz.login(name, pswd, choice);
					if (user == null) {
						winMessage("用戶名或密碼不正確!登錄失敗!");
						cleanUserInfo();
						this.code2.setText(code());
					} else {
							if (user.getType() == 0) {
								new UserUi(user,1);
							} else if (user.getType() == 1) {
								new AdminMainView(user);
							}
							this.jf.dispose();
					}
				} else {
					winMessage("驗證碼不正確!");
					textCode.setText("");
					this.code2.setText(code());
				}
			}
		} else if (ac.getSource() == this.btn_rgist) {
			new RegisterUi();
			this.jf.dispose();// 點擊按鈕時,new一個frame,原先frame銷燬
		}
	}

}

用戶登錄成功後頁面:UserUi

package view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import entity.Hall;
import entity.Movie;
import entity.Session;
import entity.Ticket;
import entity.User;
import service.CinemaService;
import service.HallService;
import service.MovieService;
import service.SessionService;
import service.TicketService;
import service.UserService;
import serviceimpl.CinemaServiceImpl;
import serviceimpl.HallServiceImpl;
import serviceimpl.MovieServiceImpl;
import serviceimpl.SessionServiceImpl;
import serviceimpl.TicketServiceImpl;
import serviceimpl.UserServiceImpl;

public class UserUi implements ActionListener ,Runnable{

	private SessionService session = null;
	private MovieService ms = null;
	private CinemaService cs = null;
	private HallService hs = null;
	 TicketService ts = null;
	private UserService us = null;
	
	private User user;
	private int defaultcard;
	private JFrame jf = new JFrame("電影購票系統");
	private Container con = jf.getContentPane();// 獲得面板

	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	private Dimension sc = toolkit.getScreenSize();// 獲得屏幕尺寸
	private JLabel lb_welcome = null;

	JTable rtb = null;
	JTable rtb2 = null;

	private JButton btn_buy = null;
	private JButton btn_comment = new JButton("評價");
	private JButton btnsearch = new JButton("搜索影片");
	private JButton btn_balance = new JButton("充值/餘額查詢");

	private JMenuBar menuBar = new JMenuBar();

	private JLabel oldjl;
	private JLabel newjl;

	private JLabel card0 = new JLabel();
	private JButton updatepass = new JButton("修改用戶信息");
	private JButton confirmUp = new JButton("確定");
	private JButton cancel = new JButton("取消");
	private JPasswordField oldpass = new JPasswordField();
	private JPasswordField newpass = new JPasswordField();
	private JLabel card1 = new JLabel();
	private JLabel card2 = new JLabel();
	private JLabel card3 = new JLabel();
	private JLabel card4 = new JLabel();

	private Font font = new Font("楷體", 0, 20);
	private Font font0 = new Font("楷體", 0, 25);
	private Font font1 = new Font("楷體", 0, 16);
	private Font font2 = new Font("楷體", 0, 15);
	private ImageIcon userinfobg = new ImageIcon("image/userinfo.jpg");

	private JTable tb = null;

	JButton[] card1_btn = null;
	int x = 0;

	int k = 0;

	List<Movie> mlist = null;
	List<Session> sessionlist = null;
	List<Ticket> ticketByUId = null;

	// 開啓線程使得歡迎標籤動起來
	// 這是單線程
	private class thread implements Runnable {

		@Override
		public void run() {
			while (true) {// 死循環讓其一直移動
				for (int i = 900; i > -700; i--) {
					// for(int i=-100;i<900;i++){
					try {
						Thread.sleep(10);// 讓線程休眠100毫秒
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					lb_welcome.setLocation(i, 5);
				}
			}
		}

	}

	public UserUi(User user) {
		super();
		this.user = user;
	
		session = new SessionServiceImpl();
		ms = new MovieServiceImpl();
		cs = new CinemaServiceImpl();
		hs = new HallServiceImpl();
		ts = new TicketServiceImpl();
		us = new UserServiceImpl();
		showTicketTable(ticketByUId);
		showSessionTable(sessionlist);
	}

	public UserUi(User u, int defaultcard) {
		user = u;
		this.defaultcard = defaultcard;
		session = new SessionServiceImpl();
		ms = new MovieServiceImpl();
		cs = new CinemaServiceImpl();
		hs = new HallServiceImpl();
		ts = new TicketServiceImpl();
		us = new UserServiceImpl();

		mlist = ms.getAllMovielist();
		sessionlist = session.queryAllSession();
		//showTicketTable(ticketByUId);
	//	showSessionTable(sessionlist);
		// con.setLayout(null);
		jf.setSize(1000, 618);
		jf.setLocation((sc.width - 1000) / 2, (sc.height - 618) / 2);

		jf.setResizable(false);// 窗口大小不可變
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		con.setVisible(true);

		btn_buy = new JButton("購買");
		btn_buy.setEnabled(false);
		btn_buy.addActionListener(this);
		btn_buy.setFont(font1);
		btn_comment.addActionListener(this);
		btn_comment.setFont(font1);
		btn_comment.setEnabled(false);
		btn_comment.setVisible(false);
		btn_balance.addActionListener(this);
		btn_balance.setFont(font1);
		btnsearch.setBounds(300, 0, 150, 30);
		btnsearch.setFont(font1);
		btnsearch.addActionListener(this);

		lb_welcome = new JLabel("歡 迎 " + u.getUname() + " 進 入 用 戶 功 能 界 面");
		lb_welcome.setFont(new Font("楷體", Font.BOLD, 34));
		lb_welcome.setForeground(Color.BLUE);

		jf.setJMenuBar(menuBar);
		menuBar.add(btn_buy);
		menuBar.add(btn_balance);
		menuBar.add(btnsearch);
		menuBar.add(btn_comment);
		menuBar.add(lb_welcome);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);// 點擊欄位置
		// 選項卡面板類
		tabbedPane.setFont(font);// 選項欄欄字體,字號

		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);// 每個選項卡滾動模式
		con.add(tabbedPane);

		// tabbedPane.setSelectedIndex(0); // 設置默認選中的card
		// card0 user.getUname()
		
		tabbedPane.addTab(u.getUname()+" 歡迎您", card0);
		JLabel maxlabel = new JLabel();

		JLabel[] card0_label = new JLabel[2];
		for (int i = 0; i < 2; i++) {
			card0_label[i] = new JLabel();
			maxlabel.add(card0_label[i]);
			card0_label[i].setFont(font0);
			card0_label[i].setBounds(40, 70 + (i * 50), 250, 50);
		}

		card0_label[0].setText("您的信息如下: ");
		card0_label[0].setForeground(Color.gray);
		card0_label[1].setText("用戶名 : " + u.getUname());// user.getUname()
		card0_label[1].setForeground(Color.gray);
		updatepass.setBounds(40, 190, 120, 35);
		updatepass.addActionListener(this);
		updatepass.setBackground(Color.gray);
		updatepass.setFont(font);
		updatepass.setForeground(Color.white);
		maxlabel.add(updatepass);

		oldjl = new JLabel("原密碼");
		oldjl.setBounds(40, 250, 90, 30);
		oldjl.setForeground(Color.LIGHT_GRAY);
		oldjl.setFont(font);
		oldjl.setVisible(false);
		maxlabel.add(oldjl);

		oldpass.setBounds(40, 280, 120, 30);
		oldpass.setBackground(Color.GRAY);
		oldpass.setFont(font);
		oldpass.setForeground(Color.white);
		oldpass.setEchoChar('*');
		oldpass.setVisible(false);
		maxlabel.add(oldpass);

		newjl = new JLabel("新密碼");
		newjl.setBounds(40, 350, 90, 30);
		newjl.setFont(font);
		newjl.setForeground(Color.LIGHT_GRAY);
		newjl.setVisible(false);
		maxlabel.add(newjl);

		newpass.setBounds(40, 380, 120, 30);
		newpass.setBackground(Color.GRAY);
		newpass.setForeground(Color.white);
		newpass.setFont(font);
		newpass.setEchoChar('*');
		newpass.setVisible(false);
		maxlabel.add(newpass);

		cancel.setBounds(40, 500, 120, 30);
		maxlabel.add(cancel);
		cancel.setBackground(Color.GRAY);
		cancel.setForeground(Color.white);
		cancel.setFont(font);
		cancel.setVisible(false);
		cancel.addActionListener(this);

		confirmUp.setBounds(40, 445, 120, 30);
		confirmUp.setBackground(Color.GRAY);
		confirmUp.setForeground(Color.white);
		confirmUp.setFont(font);
		maxlabel.add(confirmUp);
		confirmUp.setVisible(false);
		confirmUp.addActionListener(this);

		maxlabel.setIcon(userinfobg);
		maxlabel.setBounds(0, 0, 850, 602);
		card0.add(maxlabel);

		// card1
		tabbedPane.addTab("熱門影片", card1);

		movieShow(mlist);

		// card3

		tabbedPane.addTab("我的購票信息", card3);
		 ticketByUId = ts.queryTicketByUserId(user.getUser_id());

		showTicketTable(ticketByUId);

		// card2未還記錄
		tabbedPane.addTab("場次信息", card2);

		showSessionTable(sessionlist);


		tabbedPane.setSelectedIndex(defaultcard); // 設置默認選中的card

		rtb.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (rtb.getSelectedRow() != -1) {
					btn_buy.setEnabled(true);
				}

			}

		});
		
		rtb2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (rtb2.getSelectedRow() != -1) {
					btn_comment.setEnabled(true);
				}

			}

		});

		//new Thread(UserUi.this).start();
	}

	public void showTicketTable(List<Ticket> ticketByUId) {
		int recordrow = 0;
		if(ticketByUId!=null){
			 recordrow = ticketByUId.size();
		}
		

		String[][] rinfo = new String[recordrow][8];

		for (int i = 0; i < recordrow; i++) {
			for (int j = 0; j < 8; j++)
				rinfo[i][j] = new String();
			int sessionid = ticketByUId.get(i).getSession_id();
			Session session2 = session.querySessionBySessionId(sessionid);// 獲取session對象
			rinfo[i][0] = " " + user.getUname();
			rinfo[i][1] = cs.queryCinemaById(session2.getCinema_id()).getCname();// 獲取電影院名稱
			rinfo[i][2] = " " + hs.queryHallById(session2.getHall_id()).getHname();// 獲取場廳名稱
			rinfo[i][3] = ms.getMovieById(ticketByUId.get(i).getMovie_id()).getMname();
			rinfo[i][4] = " " + session2.getStarttime();
			rinfo[i][5] = " " + ms.getMovieById(session2.getMovie_id()).getDuration() + "分鐘";
			rinfo[i][6] = " " + session2.getPrice() + "元";
			rinfo[i][7] = " " + ticketByUId.get(i).getSeat()+"號";
		}
		String[] tbheadnames = { "用戶", "電影院名", "場廳", "電影名字", "開始時間", "播放時間", "價格", "座位號" };

		rtb2 = new JTable(rinfo, tbheadnames);
		rtb2.repaint();
		rtb2.setRowHeight(30);
		rtb2.setFont(font);
		rtb2.setEnabled(true);
		rtb2.getTableHeader().setFont(new Font("楷體", 1, 20));
		rtb2.getTableHeader().setBackground(Color.red);
		rtb2.getTableHeader().setReorderingAllowed(false); // 不可交換順序
		rtb2.getTableHeader().setResizingAllowed(true); // 不可拉動表格
		
		JScrollPane sPane3 = new JScrollPane(rtb2);
		sPane3.setBounds(0, 0, 849, 580);// 大小剛好
		sPane3.setVisible(true);
		
		card3.add(sPane3);

	
	}

	public void movieShow(List<Movie> list) {

		int row = (list.size() + 3) / 4;
		int k = list.size();

		JLabel[][] btn_label = new JLabel[row][4];
		card1_btn = new JButton[k];
		JLabel[][] dname = new JLabel[row][4];
		JLabel[][] locality_language = new JLabel[row][4];
		JLabel[][] type_grade = new JLabel[row][4];

		JLabel allinfo = new JLabel();

		String[] cnames = { "", "", "", "" };
		// JTable tb = new JTable(btn_label,cnames);
		tb = new JTable(btn_label, cnames);

		allinfo.setSize(840, 402 * row);
		tb.setRowHeight(400);

		ImageIcon img = null;
		ImageIcon defaultImg = new ImageIcon("image/img.jpg");

		for (int i = 0; i < row; i++) {
			for (int j = 0; j < 4 && x < k; j++) {
				// list.get(i).getImg()
				if (list.get(x).getImg() != null) {
					img = new ImageIcon("image/" + list.get(x).getImg());
				} else {
					img = defaultImg;
				}
				card1_btn[x] = new JButton(img);
				card1_btn[x].setBounds(0, 0, 205, 300);

				card1_btn[x].addActionListener(this);

				dname[i][j] = new JLabel("片名:" + list.get(x).getMname());
				dname[i][j].setBounds(0, 310, 208, 30);
				dname[i][j].setFont(font);

				locality_language[i][j] = new JLabel(
						"類型:" + list.get(x).getType() + "  時長:" + list.get(x).getDuration() + " 分鐘");
				locality_language[i][j].setBounds(0, 340, 208, 30);
				locality_language[i][j].setFont(font1);

				type_grade[i][j] = new JLabel("描述:" + list.get(x).getDetail());
				type_grade[i][j].setBounds(0, 370, 208, 30);
				type_grade[i][j].setFont(font2);

				btn_label[i][j] = new JLabel();
				btn_label[i][j].setBounds(210 * j + 1, 400 * i, 208, 400);
				btn_label[i][j].add(dname[i][j]);
				btn_label[i][j].add(locality_language[i][j]);
				btn_label[i][j].add(type_grade[i][j]);
				btn_label[i][j].add(card1_btn[x]);

				allinfo.add(btn_label[i][j]);

				x++;
			}
		}
		tb.add(allinfo);
		tb.setEnabled(false);
		JScrollPane sPane = new JScrollPane(tb);

		sPane.setBounds(0, 0, 849, 550);
		sPane.setVisible(true);
		card1.add(sPane);

	}

	public void showSessionTable(List<Session> sessionlist) {
		int recordrow = 0;
		if(sessionlist!=null){
			 recordrow = sessionlist.size();
		}

		String[][] rinfo = new String[recordrow][6];

		for (int i = 0; i < recordrow; i++) {
			for (int j = 0; j < 6; j++)
			rinfo[i][j] = new String();
			rinfo[i][0] = cs.queryCinemaById(sessionlist.get(i).getCinema_id()).getCname();
			rinfo[i][1] = hs.queryHallById(sessionlist.get(i).getHall_id()).getHname();
			rinfo[i][2] = ms.getMovieById(sessionlist.get(i).getMovie_id()).getMname();
			rinfo[i][3] = "  " + sessionlist.get(i).getStarttime();
			rinfo[i][4] = "  " + sessionlist.get(i).getPrice();
			rinfo[i][5] = "  " + sessionlist.get(i).getRemain();
		}
		String[] tbheadnames = { "電影院名稱", "場廳名字", "電影名字", "開始時間", "價格", "剩餘座位數" };
		rtb = new JTable(rinfo, tbheadnames);
		rtb.setRowHeight(40);
		rtb.setFont(font);
		rtb.setEnabled(true);
		rtb.getTableHeader().setFont(new Font("楷體", 1, 20));
		rtb.getTableHeader().setBackground(Color.orange);
		rtb.getTableHeader().setReorderingAllowed(false); // 不可交換順序
		rtb.getTableHeader().setResizingAllowed(false); // 不可拉動表格

		
		rtb.repaint();
		JScrollPane sPane2 = new JScrollPane(rtb);
		
		// sPane.add()加按鈕等一些控件時,要鼠標滑過,纔會顯示出來,直接在構造方法中傳參則正常
		sPane2.setBounds(0, 0, 849, 580);// 大小剛好
		sPane2.setVisible(true);
		card2.add(sPane2);
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		for (int i = 0; i < mlist.size(); i++) {
			if (e.getSource() == this.card1_btn[i]) {

				new MovieInfoView(mlist.get(i), user);

			}
		}

		if (e.getSource() == this.btn_buy) {// 購買監聽事件

			int row = rtb.getSelectedRow();
			List<Session> thisSession = session.queryAllSession();

			if (row != -1) {
				int remain = thisSession.get(row).getRemain() - 1;// 購票後,剩餘座位需要減一
				int hall_id = thisSession.get(row).getHall_id();
				double balance = user.getBalance() - thisSession.get(row).getPrice();
			
				int flag = JOptionPane.showConfirmDialog(jf, "確認是否購買?", "確認信息", JOptionPane.YES_NO_OPTION);
				if (flag == JOptionPane.YES_OPTION) {

					if (remain < 0) {
						JOptionPane.showMessageDialog(jf, "無坐了,請更換影院或者場廳!");
					} else if (balance < 0) {
						JOptionPane.showMessageDialog(jf, "餘額不足,請先去充值!");
					} else {

						new ZuoWeiFrame(user, hall_id, thisSession.get(row));
					}

				}

			}

		} else if (e.getSource() == this.btn_comment) {//評論
			
			int row = rtb2.getSelectedRow();
			
			new CommentView(ticketByUId.get(row));
			
			
			
		} else if (e.getSource() == this.btn_balance) {
			new ChargeView(user);
		} else if (e.getSource() == this.btnsearch) {
			new SearchMovieUi();
		} else if (e.getSource() == this.updatepass) {
			this.oldjl.setVisible(true);
			this.oldpass.setVisible(true);
			this.newjl.setVisible(true);
			this.newpass.setVisible(true);
			this.cancel.setVisible(true);
			this.confirmUp.setVisible(true);
		} else if (e.getSource() == this.confirmUp) {
			// 修改密碼
			String oldpassw = new String(this.oldpass.getPassword());
			String newpassw = new String(this.newpass.getPassword());
			if ("".equals(oldpassw) || "".equals(newpassw)) {
				Login.winMessage("信息不完整!");
			} else if (!oldpassw.equals(user.getPasswd())) {
				Login.winMessage("原密碼錯誤,無法更改!");
			} else {
				// this.user.setPasswd(newpassw);
				if (us.updataUser(user.getUser_id(), newpassw)) {
					Login.winMessage("密碼修改成功!");
					this.oldjl.setText("");
					this.oldpass.setText("");
					this.newjl.setText("");
					this.newpass.setText("");

					this.oldjl.setVisible(false);
					this.oldpass.setVisible(false);
					this.newjl.setVisible(false);
					this.newpass.setVisible(false);

					this.cancel.setVisible(false);
					this.confirmUp.setVisible(false);
				} else {
					Login.winMessage("密碼修改失敗!");
				}
			}

		} else if (e.getSource() == this.cancel) {
			this.oldjl.setText("");
			this.oldpass.setText("");
			this.newjl.setText("");
			this.newpass.setText("");

			this.oldjl.setVisible(false);
			this.oldpass.setVisible(false);
			this.newjl.setVisible(false);
			this.newpass.setVisible(false);

			this.cancel.setVisible(false);
			this.confirmUp.setVisible(false);
		}
	}

	@Override
	public void run() {
		while(true){
			try {
				
				rtb.validate();
				rtb2.validate();
				
				rtb.updateUI();
				rtb2.updateUI();
				
				System.out.println("Thread sleeping····");
				
				Thread.sleep(1000);
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	
}

電影信息頁面:MovieInfoView

package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import entity.Comment;
import entity.Movie;
import entity.User;
import service.CommentService;
import service.MovieService;
import service.UserService;
import serviceimpl.CommentServiceImpl;
import serviceimpl.MovieServiceImpl;
import serviceimpl.UserServiceImpl;

public class MovieInfoView extends JFrame {

	private JPanel contentPane;
	private JTable table;
	JScrollPane scrollPane = null;

	Movie movie = null;
	User user = null;
	MovieService ms = null;
	CommentService cs = null;
	UserService us = null;
	
	
	public MovieInfoView(Movie movie,User user) {
		this.movie = movie;
		this.user = user;
		ms = new MovieServiceImpl();
		cs = new CommentServiceImpl();
		us = new UserServiceImpl();
		setTitle("用戶選票界面");
		setBounds(260, 130, 620, 600);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon("image/"+movie.getImg()));
		
		JLabel label = new JLabel("正在熱映···");
		
		JLabel lblNewLabel_1 = new JLabel("影片名:");
		lblNewLabel_1.setFont(new Font("楷體", Font.BOLD, 18));
		
		JLabel label_1 = new JLabel("類型:");
		
		JLabel label_2 = new JLabel("時長:");
		
		JLabel label_3 = new JLabel("電影詳情:");
		
		JLabel label_4 = new JLabel(movie.getMname());
		label_4.setFont(new Font("楷體", Font.BOLD, 18));
		
		JButton btnNewButton = new JButton("購買");
		btnNewButton.setForeground(Color.BLUE);
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new UserUi(user,3);
				movie.getMovie_id();
				List<Movie> movieByName = ms.getMovieByName(movie.getMname());
				
				for (Movie movie2 : movieByName) {
					System.out.println(movie2);
				}				
				MovieInfoView.this.dispose();
				
			}
		});
		
		JButton button = new JButton("取消");
		button.setForeground(Color.RED);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MovieInfoView.this.dispose();
			}
		});
		
		scrollPane = new JScrollPane();
		scrollPane.setEnabled(false);
		scrollPane.setVisible(false);
		
		JButton button_1 = new JButton("查看評論");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				scrollPane.setVisible(true);
				showComment();
				table.repaint();
				
			}
		});
		
		button_1.setForeground(Color.MAGENTA);
		
		
		
		JLabel lblNewLabel_2 = new JLabel("歡迎來到電影詳情界面");
		lblNewLabel_2.setFont(new Font("新宋體", Font.BOLD, 20));
		lblNewLabel_2.setForeground(Color.BLACK);
		
		JLabel label_5 = new JLabel(movie.getType());
		
		JLabel label_6 = new JLabel(movie.getDuration()+"分鐘");
		
		JLabel label_7 = new JLabel(movie.getDetail());
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(218)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(label_3)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(label_7, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(label_2)
										.addGroup(gl_contentPane.createSequentialGroup()
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(label_1)))
									.addGap(4)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(label_6, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
										.addComponent(label_5, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(btnNewButton)
									.addGap(18)
									.addComponent(button, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(button_1))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(36)
							.addComponent(label))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(170)
							.addComponent(lblNewLabel_2))
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(84)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 464, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(46, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(46)
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblNewLabel_2)
							.addGap(58)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_1)
								.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(label_1)
								.addComponent(label_5))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(label_2)
								.addComponent(label_6))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(label_3)
								.addComponent(label_7))
							.addGap(125)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnNewButton)
								.addComponent(button)
								.addComponent(button_1))))
					.addGap(28)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE))
		);
		
		showComment();
		scrollPane.setViewportView(table);
		contentPane.setLayout(gl_contentPane);
		this.setVisible(true);
	}
	
	public void showComment(){
		List<Comment> commlist = cs.getAllCommentByMovieId(movie.getMovie_id());
		int recordrow = 0;
		
		if(commlist!=null){
			 recordrow = commlist.size();
		}
		String[][] rinfo = new String[recordrow][3];

		SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd hh:mm");
		for (int i = 0; i < recordrow; i++) {
			for (int j = 0; j < 3; j++){
				rinfo[i][j] = new String();
			
			rinfo[i][0] = us.queryUserById(commlist.get(i).getUser_id()).getUname();
			rinfo[i][1] = commlist.get(i).getContent();
			rinfo[i][2] = sdf.format(commlist.get(i).getDatetime());
			}
		}
		
		String[] tbheadnames = { "用戶名", "評論內容", "評論時間" };

		table = new JTable(rinfo, tbheadnames);
		table.setBorder(null);
		table.setRowHeight(20);
		table.setEnabled(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(30);
		table.getTableHeader().setFont(new Font("楷體", 1, 20));
		table.getTableHeader().setBackground(Color.CYAN);
		table.getTableHeader().setReorderingAllowed(false); // 不可交換順序
		table.getTableHeader().setResizingAllowed(true); // 不可拉動表格

		scrollPane.add(table);
		scrollPane.setBorder(null);

		table.repaint();
		
	}
}

管理員頁面:AdminUi

package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


import entity.User;


public class AdminMainView extends JFrame {
	private JPanel main_panel = null;
	private JPanel fun_panel = null;
	private JDesktopPane fundesk = null;
	
	private JButton oper_User = null;
	private JButton oper_Cinema = null;
	private JButton oper_Hall = null;
	private JButton oper_Session = null;
	private JButton oper_Movie = null;
	private JButton back = null;
	
	private JLabel lb_welcome = null;
	private JLabel lb_image = null;
	private User admin = null;
	
	public AdminMainView() {
		init();
		RegisterListener();
	}
	
	public AdminMainView(User admin) {
		this.admin = admin;
		init();
		RegisterListener();
	}

	private void init() {
		main_panel = new JPanel(new BorderLayout());
		fun_panel = new JPanel(new GridLayout(8, 1, 0, 18));
		oper_User = new JButton("對用戶進行操作");
		oper_Cinema = new JButton("對影院進行操作");
		oper_Hall = new JButton("對場廳進行操作");
		oper_Session = new JButton("對場次進行操作");
		oper_Movie = new JButton("對電影進行操作");
		back = new JButton("返回");
		
		fun_panel.add(new JLabel());
		fun_panel.add(oper_User);
		fun_panel.add(oper_Cinema);
		fun_panel.add(oper_Hall);
		fun_panel.add(oper_Session);
		fun_panel.add(oper_Movie);
		fun_panel.add(back);
		fun_panel.add(new JLabel());
		
		//設置面板外觀
				fun_panel.setBorder(BorderFactory.createTitledBorder(
						BorderFactory.createRaisedBevelBorder(), "功能區"));
						
				lb_welcome = new JLabel("歡 迎 " + admin.getUname() + " 進 入 管 理 員 功 能 界 面");
				lb_welcome.setFont(new Font("楷體", Font.BOLD, 34));
				lb_welcome.setForeground(Color.BLUE);
		
		fundesk = new JDesktopPane();
		ImageIcon img = new ImageIcon(ClassLoader.getSystemResource("image/beijing2.jpg"));
		lb_image = new JLabel(img);
		lb_image.setBounds(10, 10, img.getIconWidth(), img.getIconHeight());
		fundesk.add(lb_image, new Integer(Integer.MIN_VALUE));
		
		main_panel.add(lb_welcome, BorderLayout.NORTH);
		main_panel.add(fun_panel, BorderLayout.EAST);
		main_panel.add(fundesk, BorderLayout.CENTER);
				
		//爲了不讓線程阻塞,來調用線程
		//放入隊列當中
		EventQueue.invokeLater(new Runnable() {			
		
			public void run() {
				new Thread(new thread()).start();				
			}
		});
		
		this.setTitle("管理員功能界面");
		this.getContentPane().add(main_panel);
		this.setSize(880, 600);
		this.setResizable(false);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	
	//開啓線程使得歡迎標籤動起來
	//這是單線程
	private class thread implements Runnable{

		@Override
		public void run() {
			while(true){//死循環讓其一直移動
				for(int i=900;i>-700;i--){
					//for(int i=-100;i<900;i++){
					try {
						Thread.sleep(10);//讓線程休眠100毫秒
					} catch (InterruptedException e) {						
						e.printStackTrace();
					}
					lb_welcome.setLocation(i, 5);
				}
			}
		}
		
	}
	
	private void RegisterListener() {
		oper_User.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				operUserView ouv = new operUserView();
				fundesk.add(ouv);
				ouv.toFront();
			}
		});
		
		oper_Cinema.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				operCinemaView ocv = new operCinemaView();
				fundesk.add(ocv);
				ocv.toFront();
			}
		});
		
		oper_Hall.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				operHallView ohv = new operHallView();
				fundesk.add(ohv);
				ohv.toFront();
			}
		});
		
		
		oper_Session.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				operSessionView osv = new operSessionView();
				fundesk.add(osv);
				osv.toFront();
			}
		});
		
		oper_Movie.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				operMovieView omv = new operMovieView();
				fundesk.add(omv);
				omv.toFront();
			}
		});
		back.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new Login();
				AdminMainView.this.dispose();
			}
		});
	}
}

由於篇幅有限,不能把所有代碼貼上來:
在這裏插入圖片描述

運行截圖

普通用戶界面:

登錄:
在這裏插入圖片描述
主頁面:
在這裏插入圖片描述
購買影片:
在這裏插入圖片描述
在線選座:
在這裏插入圖片描述
我的購票信息:
在這裏插入圖片描述
管理員界面:
影院管理:
在這裏插入圖片描述
場廳、場次管理:
在這裏插入圖片描述
在這裏插入圖片描述
電影管理:
在這裏插入圖片描述
(代碼由於太多貼不全,如果有問題或者需要所有源碼,可以加我Q: 3459067873)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章