欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Swing实现台球桌按时计费

程序员文章站 2022-04-15 23:02:12
前些日子我一兄弟找到我,说想让我帮他的小台球厅弄个计费软件,好家伙,就3个台还得记个费~~超级简易。虽然简易,但是也要登录!import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Login extends JFrame{private static final long serialVersionUID = 1L;public Login(){setVisible(true...

前些日子我一兄弟找到我,说想让我帮他的小台球厅弄个计费软件,好家伙,就3个台还得记个费~~
超级简易。
虽然简易,但是也要登录!
先看看最终效果
Swing实现台球桌按时计费
上代码!!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Login extends JFrame{
	
	private static final long serialVersionUID = 1L;

	public Login(){
		
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		//设置顶部提示文字和主窗体的宽,高,x值,y值
		setTitle("***台球");
		setBounds(300, 200, 300, 150);
        setLocationRelativeTo(null);//居中

		Container cp=getContentPane();	//添加一个cp容器
		cp.setLayout(null);	//设置添加的cp容器为流布局管理器
		
		//设置左侧用户名文字
		JLabel jl=new JLabel("用户名:");
		jl.setBounds(10, 10, 200, 18);
		final JTextField name=new JTextField();	//用户名框
		name.setBounds(80, 10, 150, 18);	//设置用户名框的宽,高,x值,y值
		
		//设置左侧密码文字
		JLabel jl2=new JLabel("密码:");
		jl2.setBounds(10, 50, 200, 18);
		final JPasswordField password=new JPasswordField();	//密码框:为加密的***
		password.setBounds(80, 50, 150, 18);	//设置密码框的宽,高,x值,y值
		
		//将jl、name、jl2、password添加到容器cp中
		cp.add(jl);
		cp.add(name);
		cp.add(jl2);
		cp.add(password);
		
		//确定按钮
		JButton jb=new JButton("确定");	//添加一个确定按钮
		jb.addActionListener(new ActionListener(){		//为确定按钮添加监听事件
			
			public void actionPerformed(ActionEvent arg0) {
				
				if(name.getText().trim().length()==0||new String(password.getPassword()).trim().length()==0){
					JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
					return;
				}
				if(name.getText().trim().equals("11")&&new String(password.getPassword()).trim().equals("123456")){
//					JOptionPane.showMessageDialog(null, "登录成功");
					setVisible(false);// 本窗口隐藏,
	                new play();// 新窗口显示
	                dispose();//本窗口销毁,释放内存资源
				}
				else{
					JOptionPane.showMessageDialog(null, "用户名或密码错误");
				}
			}
		});
		jb.setBounds(80, 80, 60, 18);	//设置确定按钮的宽,高,x值,y值
		cp.add(jb);	//将确定按钮添加到cp容器中
		
		//重置按钮
		final JButton button = new JButton();
		button.setText("重置");
		button.addActionListener(new ActionListener(){		//为重置按钮添加监听事件
			//同时清空name、password的数据
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成方法存根
				name.setText("");
				password.setText("");
			}
		});
		button.setBounds(150, 80, 60, 18);	//设置重置按钮的宽,高,x值,y值
		getContentPane().add(button);
	}

	//main方法入口
	public static void main(String[] args) {
		new Login();	//调用three()

	} 

}

登录成功跳转play.class

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.event.ActionEvent;

public class play extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JFrame frame;
	protected Date date1;
	protected Date date2;
	protected Date date3;
	static int count = 0;
	static double countMoney = 0;

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

	/**
	 * Create the application.
	 */
	public play() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					initialize();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setLocationRelativeTo(null);//居中
		frame.setSize(1200, 700);//大小
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel jp = new JPanel();
        JLabel jl1 = new JLabel("欢迎使用");
        JLabel jl2 = new JLabel("浩南台球计费");
        jl2.setForeground(Color.BLUE);//设置文本颜色
        JLabel jl3 = new JLabel("\u7CFB\u7EDF");
        jp.add(jl1);
        jp.add(jl2);
        jp.add(jl3);
        
        frame.getContentPane().add(jp, BorderLayout.NORTH);
        
        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        
        JPanel panel_1 = new HomePanel();
        FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        flowLayout.setVgap(180);
        flowLayout.setHgap(120);
        panel.add(panel_1);
        
        JPanel panel_4 = new JPanel();
        panel_1.add(panel_4);
        panel_4.setOpaque(false);
        FlowLayout flowLayout_3 = (FlowLayout) panel_4.getLayout();
        flowLayout_3.setVgap(0);
        flowLayout_3.setHgap(0);
        flowLayout_3.setAlignment(FlowLayout.TRAILING);
        JButton btnNewButton_1 = new JButton("1\u53F7\u5F00\u53F0");
        btnNewButton_1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		if(btnNewButton_1.getText().equals("1号开台")){
        			btnNewButton_1.setText("1号结账");
        			date1 = new Date();
        		}else{
            		Date endDate = new Date();
            		if(showOption(date1,endDate)){
            			btnNewButton_1.setText("1号开台");
            		}
            		//System.out.println("" + diff[1] + "小时" + diff[2] + "分" + diff[3] + "秒");
        		}
        	}
        });
        btnNewButton_1.setHorizontalAlignment(SwingConstants.LEFT);
        btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 17));
        panel_4.add(btnNewButton_1);
        JPanel panel_2 = new HomePanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_2.getLayout();
        flowLayout_1.setVgap(180);
        flowLayout_1.setHgap(120);
        panel.add(panel_2);
        
        JPanel panel_5 = new JPanel();
        panel_2.add(panel_5);
        panel_5.setOpaque(false);
        FlowLayout flowLayout_4 = (FlowLayout) panel_5.getLayout();
        flowLayout_4.setVgap(0);
        flowLayout_4.setHgap(0);
        
        JButton btnNewButton = new JButton("2\u53F7\u5F00\u53F0");
        btnNewButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		if(btnNewButton.getText().equals("2号开台")){
        			btnNewButton.setText("2号结账");
        			date2 = new Date();
        		}else{
            		Date endDate = new Date();
            		if(showOption(date2,endDate)){
            			btnNewButton.setText("2号开台");
            		}
        		}
        	}
        });
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 17));
        panel_5.add(btnNewButton);
        
        JPanel panel_3 = new HomePanel();
        FlowLayout flowLayout_2 = (FlowLayout) panel_3.getLayout();
        flowLayout_2.setVgap(180);
        flowLayout_2.setHgap(120);
        panel.add(panel_3);
        
        JPanel panel_6 = new JPanel();
        panel_3.add(panel_6);
        panel_6.setOpaque(false);
        FlowLayout flowLayout_5 = (FlowLayout) panel_6.getLayout();
        flowLayout_5.setVgap(0);
        flowLayout_5.setHgap(0);
        
        JButton button = new JButton("3\u53F7\u5F00\u53F0");
        button.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		if(button.getText().equals("3号开台")){
        			button.setText("3号结账");
        			date3 = new Date();
        		}else{
            		Date endDate = new Date();
            		if(showOption(date3,endDate)){
            			button.setText("3号开台");
            		}}
        	}
        });
        button.setFont(new Font("宋体", Font.PLAIN, 17));
        panel_6.add(button);
        panel.setLayout(new FlowLayout(FlowLayout.CENTER,30,20));
        
        JPanel panel_7 = new JPanel();
        FlowLayout flowLayout_6 = (FlowLayout) panel_7.getLayout();
        flowLayout_6.setAlignment(FlowLayout.LEADING);
        flowLayout_6.setVgap(100);
        flowLayout_6.setHgap(0);
        panel.add(panel_7);
        
        //退出
        JButton button_1 = new JButton("  \u9000  \u51FA  ");
        button_1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		StringBuffer exitMsg = new StringBuffer("今日累计开台:"+count +" 次\n");
        		exitMsg.append("累计收入:"+countMoney+"元");
        		Object[] options = {" 确定 "," 取消 "};
        		int response=JOptionPane.showOptionDialog(null, exitMsg, "退出",JOptionPane.YES_OPTION,
        				 
                        JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
         
                if(response==0){
                	System.exit(0);//退出
                }
        		
        	}
        });
        panel_7.add(button_1);
		
	}

	public static boolean showOption(Date date1,Date endDate){
        Object[] options = {" 确定 "," 取消 "};    
        long[] diff = getDistanceTimes(date1,endDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuffer msg = new StringBuffer("开始时间:");
        msg.append(sdf.format(date1));
        msg.append("\n");
        msg.append("结束时间:");
        msg.append(sdf.format(endDate));
        msg.append("\n");
        
        msg.append("累计 ");
        msg.append(diff[1]+"小时"+diff[2]+"分"+diff[3]+"秒\n");
        
        msg.append("共计: ");
        double money;
        if(diff[1]<1){
        	money = 10.00;
        }else{
        	money = diff[1]*10 + diff[2]*0.17;
        }
        msg.append(money+" 元");
        int response=JOptionPane.showOptionDialog(null, msg, "计时结账",JOptionPane.YES_OPTION,
 
                JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
 
        if(response==0){
        	count = count+1;
        	countMoney = countMoney+money;
            return true;//确定
 
        } else{
        	return false;
 
        } 
    }
	
	/**
     * 两个时间相差距离多少天多少小时多少分多少秒
     * @param str1 时间参数 1 格式:1990-01-01 12:00:00
     * @param str2 时间参数 2 格式:2009-01-01 12:00:00
     * @return long[] 返回值为:{天, 时, 分, 秒}
     */
    public static long[] getDistanceTimes(Date strDate, Date endDate) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long time1 = strDate.getTime();
		long time2 = endDate.getTime();
		long diff ;
		if(time1<time2) {
		    diff = time2 - time1;
		} else {
		    diff = time1 - time2;
		}
		day = diff / (24 * 60 * 60 * 1000);
		hour = (diff / (60 * 60 * 1000) - day * 24);
		min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
		sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        long[] times = {day, hour, min, sec};
        return times;
    }
}

图片设置

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class HomePanel extends JPanel{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	ImageIcon icon;
	Image img;
	
	public HomePanel() {
		//  /img/HomeImg.jpg 是存放在你正在编写的项目的bin文件夹下的img文件夹下的一个图片
//		icon=new ImageIcon(getClass().getResource("/img/bg.jpg"));
		icon=new ImageIcon(HomePanel.class.getResource("/img/bg.jpg"));
		img=icon.getImage();
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		//下面这行是为了背景图片可以跟随窗口自行调整大小,可以自己设置成固定大小
		g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
	}
	
}

另外需要导入两个jar包

jgoodies-forms-1.8.0-sources.jar
jgoodies-common-1.8.0-sources.jar
Swing实现台球桌按时计费

结束,这里就简单记录一下,后续他如果做大了,我再回来改改,收他费~
哦对了,想玩玩swing的同学建议安装一下windowbuilder插件,安装方法百度一大把。

本文地址:https://blog.csdn.net/weixin_44262301/article/details/108263681

相关标签: swing java