Java 快速搭建UI 作业之船货信息管理系统

 

简单船货管理信息系统:

主页面

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.MouseInputAdapter;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.table.DefaultTableModel;

import business.EmployeeManager;
import vo.Employee;


public class CargoInformation extends JFrame {

	private JPanel contentPane;
	private JTable table;
    ShipManager sm;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CargoInformation frame = new CargoInformation();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CargoInformation() {
		setFont(new Font("Microsoft Uighur", Font.BOLD, 18));
		setTitle("船货信息管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 974, 552);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u8239\u8D27\u8BB0\u5F55\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), "\u8239\u8D27\u8BB0\u5F55\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(0, 47, 735, 441);
		contentPane.add(panel);
		panel.setLayout(new BorderLayout());
		Object [] cObject= {"\u8239\u8D27\u7533\u8BF7\u7F16\u53F7", "\u8239\u8236\u540D\u79F0", "\u6CCA\u4F4D\u4F4D\u7F6E", "\u8D27\u7269\u540D\u79F0", "\u96C6\u88C5\u7BB1\u6570\u91CF"};
		Object [] [] bObjects= {{"D405-301","上海东海船","定海港","出口日本大蒜","4500"},
				{"D501-301","广州交冲船","浦东段","出口奥迪汽车","500"},
		       {"D701-206","青岛灵山船","浦西段","海尔冰箱","740"}};
		DefaultTableModel tblModel = new DefaultTableModel(bObjects, cObject);
		table = new JTable(tblModel);
		panel.add(table.getTableHeader(),BorderLayout.NORTH);
		panel.add(table, BorderLayout.CENTER);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u76F8\u5173\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(744, 47, 206, 441);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JButton btnNewButton = new JButton("删除");
		btnNewButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                int selectedRow = table.getSelectedRow();
                if(selectedRow!=-1)  
                {
                    tblModel.removeRow(selectedRow); 
                }
            }
        });

		btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 30));
		btnNewButton.setBounds(29, 42, 149, 69);
		panel_1.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("增加");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddInformation addInformation = new AddInformation();
				addInformation.setVisible(true);
				CargoInformation.this.disable();// 销毁当前界面

			}
		});
		btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 30));
		btnNewButton_1.setBounds(29, 175, 149, 69);
		panel_1.add(btnNewButton_1);
		
		JButton btnNewButton_1_1 = new JButton("关闭");
		btnNewButton_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			dispose();
			}
		});

		btnNewButton_1_1.setFont(new Font("微软雅黑", Font.BOLD, 30));
		btnNewButton_1_1.setBounds(29, 312, 149, 69);
		panel_1.add(btnNewButton_1_1);
		sm = new ShipManager();
		fillDataToTable(sm.getshipList());

	}
	private void fillDataToTable(java.util.List ship) {
		// TODO Auto-generated method stub
		DefaultTableModel tblmodel = (DefaultTableModel) table.getModel();
		tblmodel.setRowCount(0);
		for (Object obj : ship) {
			
			Ship s= (Ship) obj;
			System.out.println(s.getClass().getName());
			String [] str=new String [5];
			str[0]=s.getShipId();
			str[1]=s.getShipName();
			str[2]=s.getSize();
			str[3]=s.getCargoName();
			str[4]=s.getContainer();
           tblmodel.addRow(str);
			
		}

		table.invalidate();
}
}

副业面

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import business.EmployeeManager;
import data.AccessFile;
import ui.AddFrame;
import ui.MainFrame;
import vo.Employee;

import javax.swing.border.EtchedBorder;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;

public class AddInformation extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	Access ac;
	ShipManager sm;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddInformation frame = new AddInformation();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddInformation() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1238, 447);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u521B\u5EFA\u8239\u8D27\u8BB0\u5F55\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 30, 1010, 369);
		contentPane.add(panel);
		panel.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(679, 177, 230, 45);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(679, 77, 230, 45);
		panel.add(textField_1);
		
		JLabel cargoName = new JLabel("货物名称:");
		cargoName.setFont(new Font("微软雅黑", Font.BOLD, 20));
		cargoName.setHorizontalAlignment(SwingConstants.CENTER);
		cargoName.setBounds(556, 62, 124, 66);
		panel.add(cargoName);
		
		JLabel container = new JLabel("集装箱数量:");
		container.setHorizontalAlignment(SwingConstants.CENTER);
		container.setFont(new Font("微软雅黑", Font.BOLD, 20));
		container.setBounds(556, 162, 124, 66);
		panel.add(container);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(213, 279, 230, 45);
		panel.add(textField_2);
		
		JLabel size = new JLabel("泊位位置:");
		size.setHorizontalAlignment(SwingConstants.CENTER);
		size.setFont(new Font("微软雅黑", Font.BOLD, 20));
		size.setBounds(79, 264, 124, 66);
		panel.add(size);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(213, 177, 230, 45);
		panel.add(textField_3);
		
		JLabel shipName = new JLabel("船舶名称:");
		shipName.setHorizontalAlignment(SwingConstants.CENTER);
		shipName.setFont(new Font("微软雅黑", Font.BOLD, 20));
		shipName.setBounds(79, 162, 124, 66);
		panel.add(shipName);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(210, 66, 230, 45);
		panel.add(textField_4);
		
		JLabel shipId = new JLabel("船货申请编号:");
		shipId.setHorizontalAlignment(SwingConstants.CENTER);
		shipId.setFont(new Font("微软雅黑", Font.BOLD, 20));
		shipId.setBounds(32, 51, 176, 66);
		panel.add(shipId);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u64CD\u4F5C", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_1.setBounds(1017, 30, 210, 369);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JButton btnNewButton = new JButton("保存");
				btnNewButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Ship ship=new Ship();
						ship.setShipId(textField_4.getText());
						ship.setShipName(textField_3.getText());
						ship.setSize(textField_2.getText());
						ship.setCargoName(textField_1.getText());
						ship.setContainer(textField.getText());
						
						if (sm.addShip(ship)) {
							JOptionPane.showMessageDialog(AddInformation.this, "船货信息添加成功");
							CargoInformation aInformation=new CargoInformation();
							aInformation.setVisible(true);
							AddInformation.this.dispose();
							
							
							
						}else {
							JOptionPane.showMessageDialog(AddInformation.this, "船货信息添加失败");
						}
					}
				});
				sm=new ShipManager();
				ac=new Access();
				
		btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 30));
		btnNewButton.setBounds(25, 47, 163, 73);
		panel_1.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 30));
		btnNewButton_1.setBounds(25, 222, 163, 73);
		panel_1.add(btnNewButton_1);
	}

	
}

文件数据传输

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;


public class Access {
	public static final String FILENAME="D:"+File.separator+"ship.dat";
	public void wtrieFile(List ship) {
		try {
			if (ship==null) {
				ship=new ArrayList<Ship>();
				System.out.println("dcasd");
				
			}
			System.out.println("d");
			ObjectOutputStream objout=
					new ObjectOutputStream(new FileOutputStream(FILENAME));
			objout.writeObject(ship);
			objout.flush();
			objout.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public List readFile() {
		List ships=null;
		try {
			File file=new File(FILENAME);
			if (file.exists()==false) {
				this.wtrieFile(null);
				System.out.println("dc");
			}
			System.out.println("dca");
			if (file.length()==0) {
				System.out.println("dcas");
				return ships;
				
			}
			System.out.println("dcasass");
			ObjectInputStream objin=new ObjectInputStream(new FileInputStream(FILENAME));
			ships = (List)objin.readObject();
			objin.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ships;
		
	}

}

船信息

import java.io.Serializable;

public class Ship implements Serializable{
	String shipId;
    String shipName;
    String size;
    String cargoName;
    String container;
     
	public Ship() {
		super();
	}

	public Ship(String shipId, String shipName, String size, String cargoName, String container) {
		super();
		this.shipId = shipId;
		this.shipName = shipName;
		this.size = size;
		this.cargoName = cargoName;
		this.container = container;
	}

	public String getShipId() {
		return shipId;
	}

	public void setShipId(String shipId) {
		this.shipId = shipId;
	}

	public String getShipName() {
		return shipName;
	}

	public void setShipName(String shipName) {
		this.shipName = shipName;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getCargoName() {
		return cargoName;
	}

	public void setCargoName(String cargoName) {
		this.cargoName = cargoName;
	}

	public String getContainer() {
		return container;
	}

	public void setContainer(String container) {
		this.container = container;
	}
	
    

}

船信息管理

import java.util.List;

import vo.Employee;

public class ShipManager {
   Access ac;
   public ShipManager(){
	   ac=new Access();
   }
   public boolean addShip(Ship s) {
		boolean result = false;
		try { 
	     	List ship=ac.readFile();
	     	ship.add(s);
	     	ac.wtrieFile(ship);;
	     	result=true; 	

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return result;

	}
	public List getshipList() {
		// TODO Auto-generated method stub
		return ac.readFile();
	}
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值