登录界面,采用空布局在左侧设置图片位置放入登录的图片,右侧是用户名和密码标签,然后是用户名文本框和密码框,还有标题在右上方,下方是登录、注册和退出按钮,登录和退出按钮加上图标。这里的图片都是从网上找的。
在登录、注册和退出按钮设置监听器。退出关闭本界面,登录按钮先判断账号属于什么账号,如果是游客账号,就进入游客功能主界面,如果登录账号是管理员账号,就进入管理员功能界面。游客和管理员从同一界面登录账号,有判断语句分开账号,如果是游客进入游客界面,如果是管理员进入管理员界面,界面内容不同。
public class LoginFrame extends JFrame {JPanel pnlMain; //面板JLabel lblLogo,lblTitle,lblUserName,lblPassWord;//标签,纯显示,后期不需要变化JTextField txtUserName; //单行文本框JPasswordField pswPassWord; //密码框JButton btnLogin,btnExit,zhu; //按钮static String sname;Dimension dsSIZE;Toolkit toolkit = Toolkit.getDefaultToolkit(); //获取电脑屏幕大小public LoginFrame(){pnlMain = new JPanel(); //创建面板对象pnlMain.setLayout(null);//设置空布局lblLogo = new JLabel(new ImageIcon("images/登录.jpg"));lblTitle = new JLabel("梦"+" "+"幻"+" "+"游"+" "+"乐"+" "+"场");lblUserName = new JLabel("账 号:");lblPassWord = new JLabel("密 码:");txtUserName = new JTextField(20);//指定列数pswPassWord = new JPasswordField(20);//btnLogin = new JButton("登录",new ImageIcon("images/左.png"));btnExit = new JButton("退出",new ImageIcon("images/右.png"));zhu = new JButton("注册");Font font1 = new Font("隶书", Font.BOLD, 30);lblTitle.setFont(font1);Font font2 = new Font("隶书", Font.PLAIN, 23);lblUserName.setFont(font2);lblPassWord.setFont(font2);btnLogin.setFont(font2);btnExit.setFont(font2);zhu.setFont(font2);lblLogo.setBounds(30, 30, 500, 400);lblTitle.setBounds(560, 50, 300, 50);lblUserName.setBounds(570, 150, 100, 50);txtUserName.setBounds(650, 160, 150, 30);lblPassWord.setBounds(570, 250, 100, 50);pswPassWord.setBounds(650, 260, 150, 30);btnLogin.setBounds(540, 350, 100, 50);zhu.setBounds(650, 350, 70, 50);btnExit.setBounds(730, 350, 100, 50);pnlMain.add(lblLogo);//将组件加载到面板上pnlMain.add(lblTitle);pnlMain.add(lblUserName);pnlMain.add(lblPassWord);pnlMain.add(txtUserName);pnlMain.add(pswPassWord);pnlMain.add(btnLogin);pnlMain.add(btnExit);pnlMain.add(zhu);btnLogin.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==btnLogin) {String id = txtUserName.getText();String password = pswPassWord.getText();if (UserDao.login(id, password)) {JOptionPane.showMessageDialog(null, "登录成功");try {UserBean youke = UserDao.select(id);sname=youke.getYoukename();} catch (SQLException ex) {ex.printStackTrace();}new YouKeFrame();dispose();} else if (txtUserName.getText().equals("admin") && pswPassWord.getText().equals("123456")){new MainFrame();LoginFrame.super.dispose();} else {JOptionPane.showMessageDialog(null, "用户名或密码输入不正确!");}}}});zhu.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new ZhuCe();}});btnExit.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});this.setContentPane(pnlMain);//将面板加到窗体上this.setTitle("欢迎来到梦幻游乐场!");//设置标题this.setVisible(true);//让窗口可视this.setSize(850, 500);//设置窗口大小dsSIZE = toolkit.getScreenSize();//获取显示器大小,必须写在设置窗口大小下面this.setLocation(dsSIZE.width/2-this.getWidth()/2, dsSIZE.height/2-this.getHeight()/2);//将窗口居中Image img = toolkit.getImage("images/标题图.png");this.setIconImage(img);//换头标题图像this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭方式}public static void main(String[] args) {try {BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();} catch (Exception e) {e.printStackTrace();}Thread t = new YouKeFrame.MyThread(YouKeFrame.jl);Thread m = new MainFrame.MyThread(MainFrame.jl);m.start(); t.start();new LoginFrame();}
}
创建类用来连接mysql数据库,以及对数据库进行查询和更改操作,将所得到的值放到集合,传递给程序使用,实现程序与数据库之间的数据共享。加载数据库驱动,并连接数据库。然后可以对数据库的表进行使用和访问。
public class DB {private static String driverName = "com.mysql.cj.jdbc.Driver";private static String dbURL = "jdbc:mysql://localhost:3306/youlechang?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true";private static String USERname = "admin";private static String PASSWORD = "123456";public static Connection getConnection() {Connection conn = null;try {//加载jdbc驱动Class.forName(driverName);conn = DriverManager.getConnection(dbURL, USERname, PASSWORD);} catch (ClassNotFoundException e) {System.out.println("驱动加载异常!");} catch (SQLException throwables) {System.out.println("数据库连接失败!");}return conn;}}
购票界面,购票按钮设置监听器,点击进入购票界面,有一个多选框,可以选择购票类型,一个文本框再输入你想要购买的票数。
点击确认,在确认按钮设置监听器,设置get方法得到多选框内容和文本框中的票数,有if语句判断,然后通过if语句来调整票的价格,在下方结算文本框set得到的价格,由于get文本框得到的数据类型为String,想要计算票价需要把get到的String类型强制转换成int类型,然后运用int类型的数据进行计算,因为计算之后需要被文本框获取,文本框是String类型数据,如果文本框需要获取计算结果,那就需要把int类型数据强制转换成String类型数据被文本框获得,显示在文本框处使用户看到。界面就会在应付金额处显示对应的票价,如果不符合买票条件,会有消息提示框弹出,可以在左上角查询,然后重新购买。
在付款按钮设置监听器,点击付款按钮,会弹出二维码付款窗口,窗口显示一些语句和购票人,还有购票时间和应付金额。在选择票的种类中,先选择票的种类,再输入票的数量,系统会通过if语句来判断购票数量来判断游客购票种类是否符合票价,并给出相应提示去左上角查询购票规则,了解清楚之后再来购票界面买相应的票。然后点击付款按钮扫码支付应付金额。
public class GouPiao extends JPanel{JLabel xuanze,jiage,tupian,piaoshu,shengyu,shengyushu;JComboBox goupiao;static JTextField money,shuliang;JButton fukuan,yes;public static JFrame mainFrame;public GouPiao(){mainFrame=(JFrame) this.getParent();this.setLayout(null);this.setPreferredSize(new Dimension(860, 480));GUITools.setCenter(this);tupian = new JLabel(new ImageIcon("images/dingbu/jpg"));xuanze = new JLabel("票的种类:");jiage = new JLabel("应付金额:");goupiao = new JComboBox(new String[]{"成人票","儿童票","团购","温馨家庭版"});money = new JTextField(10);yes = new JButton("确认",new ImageIcon("images/对.png"));fukuan = new JButton("付款",new ImageIcon("images/钱.png"));piaoshu= new JLabel("票的数量:");shuliang =new JTextField(10);Font font = new Font("隶书", Font.PLAIN, 20);yes.setFont(font);xuanze.setFont(font);jiage.setFont(font);goupiao.setFont(font);fukuan.setFont(font);piaoshu.setFont(font);money.setFont(font);shuliang.setFont(font);tupian.setBounds(20, 0, 840, 100);xuanze.setBounds(100, 120, 100, 50);goupiao.setBounds(250, 120, 200, 50);piaoshu.setBounds(100, 200, 100, 50);shuliang.setBounds(250, 200, 200, 50);yes.setBounds(500, 200, 100, 50);jiage.setBounds(100, 280, 100, 50);money.setBounds(250, 280, 200, 50);fukuan.setBounds(500, 280, 100, 50);this.add(tupian);this.add(xuanze);this.add(goupiao);this.add(jiage);this.add(money);this.add(fukuan);this.add(piaoshu);this.add(shuliang);this.add(yes);this.setVisible(true);yes.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (goupiao.getSelectedItem() == "成人票"){int chengren = Integer.parseInt(shuliang.getText()) * 60;String s = Integer.toString(chengren);money.setText(s);}else if (goupiao.getSelectedItem() == "儿童票"){int ertong = Integer.parseInt(shuliang.getText()) * 48;String s = Integer.toString(ertong);money.setText(s);}else if (goupiao.getSelectedItem() == "团购"){int renshu = Integer.parseInt(shuliang.getText());if (renshu <6){JOptionPane.showMessageDialog(null, "抱歉,团购人数最少为6人(详情请上页面左上角查询)");}else if (renshu >=6 && renshu <10){int liu = renshu * 48;String s = Integer.toString(liu);money.setText(s);}else {int ten = renshu * 45;String s = Integer.toString(ten);money.setText(s);}}else {int renshu = Integer.parseInt(shuliang.getText());if (renshu == 3){money.setText("150");}else if (renshu == 4){money.setText("200");}else {JOptionPane.showMessageDialog(null, "抱歉,您选择的人数不在家庭版中(详情请上页面左上角查询)");}}}});fukuan.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new FuKuan();}});}}
点击注册按钮,会出现注册消息框,采用空布局,定义标签和文本框,定位置放置标签和文本框,设置提交按钮和退出按钮。
在界面用get方法得到文本框内的内容,调用UserDao里的insert方法把注册页面的内容添加到数据库的表中,从而保存下来游客注册的数据,再次登录选择注册的账号和密码就可以进入游客界面。
public class ZhuCe extends JFrame {JLabel zhanghao,xingming,pasword,age,sex,phone;JTextField hao,name,nianling,xingbie,dianhua;JPasswordField mima;JButton tijiao,tuichu;JPanel pnlMain; //面板Dimension dsSIZE;Toolkit toolkit = Toolkit.getDefaultToolkit(); //获取电脑屏幕大小public ZhuCe(){pnlMain = new JPanel(); //创建面板对象pnlMain.setLayout(null);//设置空布局zhanghao = new JLabel("QQ/手机号:");xingming = new JLabel("姓名:");pasword = new JLabel("密码:");age = new JLabel("年龄:");sex = new JLabel("性别:");phone = new JLabel("联系电话:");hao = new JTextField(20);name = new JTextField(20);mima = new JPasswordField(20);nianling = new JTextField(20);xingbie = new JTextField(20);dianhua = new JTextField(20);tijiao = new JButton("提交");tuichu = new JButton("退出");zhanghao.setBounds(20,20,100, 30);xingming.setBounds(20,70,100, 30);pasword.setBounds(20,120,100, 30);age.setBounds(20,190,100, 30);sex.setBounds(20,240,100, 30);phone.setBounds(20,290,100, 30);hao.setBounds(120, 20, 100, 30);name.setBounds(120, 70, 100, 30);mima.setBounds(120, 120, 100, 30);nianling.setBounds(120, 190, 100, 30);xingbie.setBounds(120, 240, 100, 30);dianhua.setBounds(120, 290, 100, 30);tijiao.setBounds(50, 360, 70, 30);tuichu.setBounds(150, 360, 70, 30);pnlMain.add(zhanghao);pnlMain.add(xingming);pnlMain.add(pasword);pnlMain.add(age);pnlMain.add(sex);pnlMain.add(phone);pnlMain.add(hao);pnlMain.add(name);pnlMain.add(mima);pnlMain.add(nianling);pnlMain.add(xingbie);pnlMain.add(dianhua);pnlMain.add(tijiao);pnlMain.add(tuichu);tijiao.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String common = e.getActionCommand();UserBean bean = new UserBean();bean.setYoukeid(hao.getText());bean.setYoukename(name.getText());bean.setPassword(mima.getText());bean.setAge(Integer.parseInt(nianling.getText()));bean.setSex(xingbie.getText());bean.setPhone(dianhua.getText());try {UserDao.insert(bean);//调用UserDao里的insert方法//提示保存成功JOptionPane.showMessageDialog(null, "注册成功");setVisible(true);//关闭对话框dispose();} catch (Exception e1) {//提示保存失败JOptionPane.showMessageDialog(null, "注册失败");setVisible(true);e1.printStackTrace();}}});this.setContentPane(pnlMain);//将面板加到窗体上this.setTitle("注册账号");//设置标题this.setVisible(true);//让窗口可视this.setSize(300, 450);//设置窗口大小dsSIZE = toolkit.getScreenSize();//获取显示器大小,必须写在设置窗口大小下面this.setLocation(dsSIZE.width/2-this.getWidth()/2, dsSIZE.height/2-this.getHeight()/2);//将窗口居中Image img = toolkit.getImage("images/标题图.png");this.setIconImage(img);//换头标题图像this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭方式}
}
差不多主要界面就这些,菜单栏还有一些其他的小部分基本都是通过I/O流读取的txt文档,还有一张场景俯视图(当然图也是去网上找的自己改了一点),在菜单栏里点击按钮可以弹出弹窗查看,剩下内容太多了,建了六个包,一共三十多个类,需要的同学可以找我要一下,大家交流学习,这是我发的第一个帖子,我也在这个网站学习半年了,感觉挺好的,期末作业也完成了就拿出来发一个帖子吧。本人初学者还希望大神不要喷我的程序。虽然一个很简陋的程序,可我还是想拿出来发一下,毕竟这是我这学期的纪念。
最后希望大家都好好学习天天向上吧。一起努力学习,哈哈哈哈哈哈哈哈