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();
}
});
}
}
由于篇幅有限,不能把所有代码贴上来:
运行截图
普通用户界面:
登录:
主页面:
购买影片:
在线选座:
我的购票信息:
管理员界面:
影院管理:
场厅、场次管理:
电影管理:
(代码由于太多贴不全,如果有问题或者需要所有源码,可以加博主V交流: Code2Life2)