Java 96. Cách sử dụng JCombobox và JList trong Java Swing



    Bản quyền thuộc về TITV.vn, 
    vui lòng không đăng tải lại nội dung từ trang này.

    Video giải thích chi tiết


     Code chi tiết 

    
    package model;
    
    import java.util.StringTokenizer;
    
    public class ThucDonModel {
    	private String luaChon_MonChinh;
    	private String luaChon_MonPhu;
    	private double tongTien;
    
    	public ThucDonModel() {
    		this.luaChon_MonChinh = "";
    		this.luaChon_MonPhu = "";
    		this.tongTien = 0;
    	}
    
    	public String getLuaChon_MonChinh() {
    		return luaChon_MonChinh;
    	}
    
    	public void setLuaChon_MonChinh(String luaChon_MonChinh) {
    		this.luaChon_MonChinh = luaChon_MonChinh;
    	}
    
    	public String getLuaChon_MonPhu() {
    		return luaChon_MonPhu;
    	}
    
    	public void setLuaChon_MonPhu(String luaChon_MonPhu) {
    		this.luaChon_MonPhu = luaChon_MonPhu;
    	}
    
    	public double getTongTien() {
    		return tongTien;
    	}
    
    	public void setTongTien(double tongTien) {
    		this.tongTien = tongTien;
    	}
    
    	public void tinhTongTien() {
    		this.tongTien = 0;
    		if(this.luaChon_MonChinh.equals("CƠM")) {
    			tongTien+= 20000;
    		}else if(this.luaChon_MonChinh.equals("PHỞ")) {
    			tongTien+= 50000;
    		}else if(this.luaChon_MonChinh.equals("BÁNH MÌ")) {
    			tongTien+= 15000;
    		}
    		
    		
    		StringTokenizer stk = new StringTokenizer(this.luaChon_MonPhu, ";");
    		while(stk.hasMoreElements()) {
    			String monPhu = stk.nextToken();
    			monPhu = monPhu.trim();
    			if(monPhu.equals("TRÀ SỮA")) {
    				tongTien+= 5000;
    			}else if(monPhu.equals("COCACOLA")) {
    				tongTien+= 10000;
    			}else if(monPhu.equals("BÁNH NGỌT")) {
    				tongTien+= 15000;
    			}else if(monPhu.equals("NEM")) {
    				tongTien+= 20000;
    			}
    		}
    	}
    
    }
    
         
    
    package view;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    import control.ThucDonController;
    import model.ThucDonModel;
    
    public class ThucDonView extends JFrame {
    	public ThucDonModel thucDonModel;
    	public JLabel jLabel_ThongTin;
    	public ArrayList<JCheckBox> list_buttonGroup_MonPhu;
    	public JComboBox<String> jComboBox_MonChinh;
    	public JList<String> jList_MonPhu;
    
    	public ThucDonView() {
    		this.thucDonModel = new ThucDonModel();
    		init();
    	}
    
    	private void init() {
    		this.setSize(600, 600);
    		this.setLocationRelativeTo(null);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLayout(new BorderLayout());
    
    		Font font = new Font("Arial", Font.BOLD, 40);
    
    		JLabel header = new JLabel("THỰC ĐƠN NHÀ HÀNG TITV");
    		header.setFont(font);
    
    		JPanel jPanel_TieuDe = new JPanel();
    		jPanel_TieuDe.add(header);
    		this.add(jPanel_TieuDe, BorderLayout.NORTH);
    
    		JPanel jPanel_MonChinh = new JPanel();
    		jPanel_MonChinh.setLayout(new FlowLayout());
    		String[] cacMonChinh = new String[] {"CƠM","PHỞ", "BÁNH MÌ"};
    		jComboBox_MonChinh  = new JComboBox<String>(cacMonChinh);
    		jComboBox_MonChinh.setFont(font);
    		jPanel_MonChinh.add(jComboBox_MonChinh);
    
    		JPanel jPanel_MonPhu = new JPanel();
    		jPanel_MonPhu.setLayout(new GridLayout(2, 2));
    		String[] cacMonPhu= new String[] {"TRÀ SỮA","COCACOLA","BÁNH NGỌT", "NEM"};
    		jList_MonPhu = new JList<String>(cacMonPhu);
    		jList_MonPhu.setFont(font);
    		jPanel_MonPhu.add(jList_MonPhu);
    
    		JPanel jPanel_LuaChon = new JPanel();
    		jPanel_LuaChon.setLayout(new BorderLayout());
    		jPanel_LuaChon.add(jPanel_MonChinh, BorderLayout.NORTH);
    		jPanel_LuaChon.add(jPanel_MonPhu, BorderLayout.CENTER);
    
    		this.add(jPanel_LuaChon, BorderLayout.CENTER);
    
    		JPanel jPanel_ThanhToan = new JPanel();
    		jPanel_ThanhToan.setLayout(new GridLayout(1, 2));
    		jLabel_ThongTin = new JLabel();
    		jLabel_ThongTin.setFont(new Font("Arial",Font.BOLD, 20));
    		JButton jButton_ThanhToan = new JButton("Thanh Toán");
    		jButton_ThanhToan.setFont(font);
    		ActionListener thucDonController = new ThucDonController(this);
    		jButton_ThanhToan.addActionListener(thucDonController);
    		jPanel_ThanhToan.add(jLabel_ThongTin);
    		jPanel_ThanhToan.add(jButton_ThanhToan);
    		this.add(jPanel_ThanhToan, BorderLayout.SOUTH);
    
    		this.setVisible(true);
    	}
    
    	public void hienThiKetQua() {
    		String kq = "Món chính: " + this.thucDonModel.getLuaChon_MonChinh()
    					+ "; Món phụ: " + this.thucDonModel.getLuaChon_MonPhu()
    					+ "; Tổng tiền: " + this.thucDonModel.getTongTien();
    		this.jLabel_ThongTin.setText(kq);
    	}
    }
    
         
    
    package control;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Enumeration;
    
    import javax.swing.AbstractButton;
    import javax.swing.JRadioButton;
    
    import view.ThucDonView;
    
    public class ThucDonController implements ActionListener {
    	private ThucDonView thucDonView;
    
    	public ThucDonController(ThucDonView thucDonView) {
    		this.thucDonView = thucDonView;
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		String monChinh = thucDonView.jComboBox_MonChinh.getSelectedItem().toString();
    
    		String monPhu = "";
    		Object[] luaChonMonPhus = thucDonView.jList_MonPhu.getSelectedValues();
    		for (Object o : luaChonMonPhus) {
    			monPhu = monPhu + o.toString() + "; ";
    		}
    
    		thucDonView.thucDonModel.setLuaChon_MonChinh(monChinh);
    		thucDonView.thucDonModel.setLuaChon_MonPhu(monPhu);
    		thucDonView.thucDonModel.tinhTongTien();
    		thucDonView.hienThiKetQua();
    	}
    
    }
    
         
    
    package Test;
    
    import javax.swing.UIManager;
    
    import view.ThucDonView;
    
    public class TestThucDon {
    	public static void main(String[] args) {
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    			new ThucDonView();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    
         

    Không có nhận xét nào:

    Đăng nhận xét