c/s结构用户界面设计
【实验内容】
编写一整套Mis系统UI界面,Mis系统名称自拟,尽量运用到如下控件:
l 窗体
l 菜单
l 工具栏
l 状态栏控件
l 标签控件
l 按钮控件
l 文本框控件
l 单选按钮控件
l 复选框控件
l 列表框控件
l 组合框控件
l 分组框控件
l 面板控件
l 图片框控件
l 定时器控件
l 滚动条控件
l 月历控件
源码如下:
1.ClientMain.java
package Client; import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; 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.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; public class ClientMain extends JFrame { public static void main(String[] args) { // TODO Auto-generated method stub ClientSend.connectServer(); new ClientMain(); } private JPanel contentPane = new JPanel(); private JTable table = new JTable(); private DefaultTableModel tablemode=new DefaultTableModel(); private JScrollPane scrollPane = new JScrollPane(); JTextField id_TextField=new JTextField(); public ClientMain() { setBounds(250, 50, 1000, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); setContentPane(contentPane); contentPane.setLayout(new BorderLayout()); setTitle("学生信息管理系统"); JSplitPane splitPane1 = new JSplitPane(); splitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT); splitPane1.setDividerLocation(600); splitPane1.setTopComponent(scrollPane); JPanel pane2=new JPanel(); pane2.setLayout(null); JLabel id_label_2 = new JLabel("学号"); id_label_2.setFont(new Font("宋体", Font.PLAIN, 16)); id_label_2.setBounds(20, 20, 36, 16); pane2.add(id_label_2); id_TextField.setBounds(70, 14, 100, 31); pane2.add(id_TextField); JButton selectbutton = new JButton("查询"); selectbutton.setFont(new Font("宋体", Font.PLAIN, 16)); selectbutton.setBounds(65, 150, 93, 23); selectbutton.addActionListener(new selectAction()); pane2.add(selectbutton); splitPane1.setRightComponent(pane2); ///表格 String[] tabletitle={"学号","姓名","性别","专业","电话"}; table.setModel(tablemode); for(int j=0;j<5;j++) { tablemode.addColumn(tabletitle[j]); } ClientSend.ss(tablemode); scrollPane.setViewportView(table); contentPane.add(splitPane1);//切分窗格 setVisible(true); } //查询 class selectAction implements ActionListener{ public void actionPerformed(ActionEvent e) { String name=id_TextField.getText(); if(name.isEmpty()) { return; }else { String mess="查询"+"##"+name; ClientSend.sending(mess); } } } }
2.ClientRecive.java
package Client; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; import javax.swing.table.DefaultTableModel; public class ClientRecive extends Thread{ Socket sock; DefaultTableModel tablemode; ClientRecive(Socket sock){ this.sock=sock; } ClientRecive(Socket sock,DefaultTableModel tablemode){ this.sock=sock; this.tablemode=tablemode; } public void run() { try { InputStream is=sock.getInputStream(); DataInputStream dis = new DataInputStream(is); while(true) { String mess=dis.readUTF(); String message=sock.getInetAddress().toString(); System.out.println(mess); String reciveSelect[]=mess.split("##"); tablemode.getDataVector().clear(); System.out.println(reciveSelect.length); ClientStudent select_stu; for(int i=0;i<((int)reciveSelect.length/5);i++) { select_stu = new ClientStudent(reciveSelect[1+(i*5)],reciveSelect[2+(i*5)], reciveSelect[3+(i*5)],reciveSelect[4+(i*5)], reciveSelect[5+(i*5)]); tablemode.addRow(select_stu.toVector()); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); // Server.socks.remove(sock); } } }
3.ClientSend.java
package Client; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.table.DefaultTableModel; public class ClientSend { static Socket sock; public static void connectServer() { try { System.out.println("客户端连接......"); sock= new Socket("223.104.103.148",50009); System.out.println("客户端连接成功咯"); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }//建立连接 } public static void ss(DefaultTableModel tablemode) { ClientRecive myThread=new ClientRecive(sock,tablemode); //创建客户端线程 myThread.start();//开始运行线程 } public static void sending(String allmessage) { try { OutputStream os =sock.getOutputStream();//sock的输出流 DataOutputStream dos = new DataOutputStream(os); dos.writeUTF(allmessage); System.out.println("数据发送完毕!"); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
4.ClientStudent.java
package Client; import java.util.Vector; public class ClientStudent { private String name; private String id; private String sex; private String dep; private String phone; public ClientStudent(String name, String id, String sex, String dep, String phone) { super(); this.name = name; this.id = id; this.sex = sex; this.dep = dep; this.phone = phone; } public Vector<Object> toVector() { // TODO Auto-generated method stub Vector<Object> vect=new Vector(); vect.add(name); vect.add(id); vect.add(sex); vect.add(dep); vect.add(phone); return vect; } }