Client of Java small project ordering system (1)

Source: Internet
Author: User

Client of Java small project ordering system (1)

After opening a Java course at school during this time, I started to write a small project-ordering system. I will record the entire project in four blogs.

I. Client

Ii. Server

Iii. Database

Iv. project experiences and problems with porting to linux

Source code uploaded: http://download.csdn.net/detail/u010214003/8138421

The source code contains the client and server, and database files are not included. After reading these blogs, you can add some data to compile the entire project.

Client details: 1. Interface Analysis 1. Initial Interface

Rotate/K/rotate/c8L3A + rotate/rotate + sLrt9bO9qOsvt/rotate "brush: java;"> // obtain the screen size Dimension screenSize = Toolkit. getdefatooltoolkit (). getScreenSize (); // setBounds (screenSize. width-640)/2, (screenSize. height-480)/2,640,480); // do not modify setUndecorated (true) in the title bar; // set the form to transparent AWTUtilities. setWindowOpaque (this, false); background = init. backImage; // background image bgLabel = new JLabel (background); // display the background image in a tag. // set the size and position of the tag to be filled with bgLabel of the entire panel. setBounds (0, 0, background. getIconWidth (), background. getIconHeight (); // convert the content pane to JPanel. Otherwise, you cannot use setOpaque () to make the content pane transparent. JPanel imagePanel = (JPanel) this. getContentPane (); imagePanel. setOpaque (false); // The default layout manager of the content pane is BorderLayoutimagePanel. setLayout (new FlowLayout (); getLayeredPane (). setLayout (null); // set imagePanel. add (new JButton ("test button"); // add the background image to the bottom layer of the layered pane as the background getLayeredPane (). add (bgLabel, new Integer (Integer. MIN_VALUE); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setSize (background. getIconWidth (), background. getIconHeight (); setResizable (false );

The ActionListenr interface can be implemented with the "minimize and close" button.

The label is used here. To implement the five interface functions of the MouseListener interface (some do not need to be implemented), the Code is as follows:

// Add the listening minButton. addMouseListener (this); exitButton. addMouseListener (this); // implement the interface function public void mouseClicked (MouseEvent e) {if (e. getSource () = minButton) {setExtendedState (JFrame. ICONIFIED);} else if (e. getSource () = exitButton) {dispose () ;}} public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e ){}


Timer thread code:

Package mytool; import java. awt. basicStroke; import java. awt. color; import java. awt. graphics; import java. awt. graphics2D; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. geom. line2D; import java. util. date; import javax. swing. JLabel; import javax. swing. timer; public class CreateClock extends JLabel implements ActionListener {public Timer timer; double pointsx [] = new double [60]; double pointsy [] = new double [60]; double pointmx [] = new double [60]; double pointmy [] = new double [60]; double pointhx [] = new double [60]; double pointhy [] = new double [60]; public int second, hour, minute; public Date date; public Line2D secondline, hourline, minuteline; public int a, B, c; public CreateClock () {// initialize the coordinate pointsx [0] = pointmx [0] = pointhx [0] = 0; pointsy [0] =-70; pointmy [0] =-55; pointhy [0] =-40; double angle = 6 * Math. PI/180; for (int I = 0; I <59; I ++) {pointsx [I + 1] = pointsx [I] * Math. cos (angle)-pointsy [I] * Math. sin (angle); pointsy [I + 1] = pointsy [I] * Math. cos (angle) + pointsx [I] * Math. sin (angle); pointmx [I + 1] = pointmx [I] * Math. cos (angle)-pointmy [I] * Math. sin (angle); pointmy [I + 1] = pointmy [I] * Math. cos (angle) + pointmx [I] * Math. sin (angle); pointhx [I + 1] = pointhx [I] * Math. cos (angle)-pointhy [I] * Math. sin (angle); pointhy [I + 1] = pointhy [I] * Math. cos (angle) + pointhx [I] * Math. sin (angle) ;}for (int I = 0; I <60; I ++) {pointsx [I] = pointsx [I] + 80; pointsy [I] = pointsy [I] + 100; pointmx [I] = pointmx [I] + 80; pointmy [I] = pointmy [I] + 100; pointhx [I] = pointhx [I] + 80; pointhy [I] = pointhy [I] + 100 ;} //// initialize each line a = B = c = 0; secondline = new Line2D. double (0, 0, 0); minuteline = new Line2D. double (0, 0, 0); hourline = new Line2D. double (0, 0, 0 ); /// // sets the timer timer = new Timer (1000, this); timer. start () ;}public void paint (Graphics g) {// draw the dial for (int I = 0; I <60; I ++) {int m = (int) pointsx [I]; int n = (int) pointsy [I]; if (I % 5 = 0) {g. setColor (Color. blue); g. fillOval (m-4, n-4, 8, 8);} else {g. setColor (Color. green); g. fillOval (m-2, N-2, 4, 4);} g. fillOval (,); // Graphics2D grap = (Graphics2D) g; BasicStroke bb = new BasicStroke (2f, BasicStroke. CAP_ROUND, BasicStroke. JOIN_MITER); grap. setStroke (bb); grap. setColor (Color. green); grap. draw (secondline); BasicStroke bs = new BasicStroke (3f, BasicStroke. CAP_ROUND, BasicStroke. JOIN_MITER); grap. setStroke (bs); grap. setColor (Color. BLUE); grap. draw (minuteline); BasicStroke bss = new BasicStroke (6f, BasicStroke. CAP_ROUND, BasicStroke. JOIN_MITER); grap. setStroke (bss); grap. setColor (Color. YELLOW); grap. draw (hourline);} public void actionreceivmed (ActionEvent arg0) {// TODO Auto-generated method stub if (arg0.getSource () = timer) {date = new Date (); string str = date. toString (); second = Integer. parseInt (str. substring (17,19); minute = Integer. parseInt (str. substring (14,16); hour = Integer. parseInt (str. substring (11,13); int h = hour % 12; a = second; B = minute; c = h * 5 + minute/12; secondline. setLine (80,100, pointsx [a], pointsy [a]); minuteline. setLine (80,100, pointmx [B], pointmy [B]); hourline. setLine (80,100, pointhx [c], pointhy [c]); repaint ();}}}

2. Main Interface Analysis

Log on to the main interface. The server determines whether you are an ordinary student or a merchant Based on the account you log on to. After logging on, the server determines the identity to enter based on the returned information.

Here is a brief introduction to the student interface.


The main interface uses a JPanel object mainPanel, which uses a box layout for this mainPanel, and then each functional area inherits to override a JPanel and adds it to mainPanel,

When you click the function label, call the card. show () function in the interface function of the listening label to switch between jpanels.

Two images are also prepared for each function tag. When you click the corresponding tag, you can change the background image to be different from the other three.

The code in the listener function is as follows:

// Add a listener for daily recommendation. addMouseListener (this); shop overview. addMouseListener (this); my menu. addMouseListener (this); tucao area. addMouseListener (this); public void mouseClicked (MouseEvent e) {if (e. getSource () = daily recommendation) {// daily recommendation of images with tags changed. setIcon (init. suggestImage_enter); shop overview. setIcon (init. shopImage); my menu. setIcon (init. myOrderImage); tucao area. setIcon (init. opinionImage); // switch the Panel card. show (mainPanel, "suggest");} else if (e. getSource () = shop list) {daily recommendation. se TIcon (init. suggestImage); shop overview. setIcon (init. shopImage_enter); my menu. setIcon (init. myOrderImage); tucao area. setIcon (init. opinionImage); card. show (mainPanel, "shop");} else if (e. getSource () = my menu) {daily recommendation. setIcon (init. suggestImage); shop overview. setIcon (init. shopImage); my menu. setIcon (init. myOrderImage_enter); tucao area. setIcon (init. opinionImage); myOrderPanel. update (); card. show (mainPanel, "myOrder");} else if (e. getSource () = tucao area ){ Daily recommendation. setIcon (init. suggestImage); shop overview. setIcon (init. shopImage); my menu. setIcon (init. myOrderImage); tucao area. setIcon (init. opinionImage_enter); JOptionPane. showMessageDialog (mainWindow. getContentPane (), "Time is tight. This function has not yet been activated. Please wait !! "," System information ", JOptionPane. INFORMATION_MESSAGE) ;}} public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {}

Ii. Socket connection to the server

On the logon page, you are required to enter the account, password, IP address, and port number. The client city tries to establish a connection with the server to verify the user and return the user information,

Determine which interface to enter based on the returned information

String ip = loginPanel. ipTextField. getText (), account = loginPanel. accountTextField. getText (), passwd = new String (loginPanel. passwdTextField. getPassword (); int port = Integer. parseInt (loginPanel. portTextField. getText (); if (clientSocket. isConnected () {} else {try {InetAddress address = InetAddress. getByName (ip); InetSocketAddress socketAddress = new InetSocketAddress (address, port); clientSocket. connect (socketAddress);} catch (IOException e1) {JOptionPane. showMessageDialog (getContentPane (), "network connection exception", "system information", JOptionPane. ERROR_MESSAGE);} try {in = new DataInputStream (clientSocket. getInputStream (); out = new DataOutputStream (clientSocket. getOutputStream (); out. writeUTF ("LOGIN"); out. writeUTF (account); out. writeUTF (passwd); String reply = in. readUTF (); if (reply. equals ("student") {studentPanel = new StudentPanel (init, in, out, this); mainPanel. add ("student", studentPanel); card. show (mainPanel, "student");} else if (reply. equals ("seller") {sellerPanel = new SellerPanel (init, in, out); mainPanel. add ("seller", sellerPanel); card. show (mainPanel, "seller");} else if (reply. equals ("defeat") {JOptionPane. showMessageDialog (getContentPane (), "incorrect user name or password", "system information", JOptionPane. ERROR_MESSAGE) ;}} catch (IOException e1 ){}}

Iii. Function Analysis

Function introduction:

After a user (student) logs on to the system, the system obtains the recommended menu information, shop information, and shop item information from the server. Users can order food from the recommendation interface and shop, modify the menu in my menu, and submit the order and payment. After a seller logs on to the ordering system, the ordering system can manage the menu of his shop, obtain the student's order, process the order, and then feedback the information to the server, all information interaction with the database is completed on the server, and the function is relatively simple.

1. Recommendation menu

First obtain information from the server, and then use JTabel to display the information. Then, add JCheckBox to JTabel to select and cancel corresponding menu items by performing operations on JCheckBox.

Code: SuggetPanel. java

Package mypanel; import init. init; import java. awt. dimension; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. IOException; import java. util. export list; import javax. swing. defaultCellEditor; import javax. swing. JButton; import javax. swing. JCheckBox; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JScrollPane; import javax. swing. JTable; import javax. swing. table. defaultTableCellRenderer; import javax. swing. table. tableColumn; import mytool. food; import mytool. suggestTabelModel; import mytool. suggestTableCellRenderer; public class SuggestPanel extends JPanel implements ActionListener {DataInputStream in = null; DataOutputStream out = null; public int food_num; public parameter list
  
   
Food; public answer list
   
    
Selectfood; final String [] headers = {"no.", "Dish name", "Shop", "category", "price/RMB", "recommendation degree ",""}; object [] [] cellData = null; JTable table; SuggestTabelModel model; JScrollPane jscrollPane; private TableColumn column = null; private JButton enterButton; public SuggestPanel (Init init, DataInputStream in, DataOutputStream out) {int I = 0; selectfood = new queue list
    
     
(); Model = new SuggestTabelModel (cellData, headers); table = new JTable (model); jscrollPane = new JScrollPane (table); jscrollPane. setPreferredSize (new Dimension (340,270); enterButton = new JButton ("add menu"); enterButton. setPreferredSize (new Dimension (); add (jscrollPane); add (enterButton); enterButton. addActionListener (this); // The Center shows DefaultTableCellRenderer r = new DefaultTableCellRenderer (); r. setHorizontalAlignment (JLabel. CENTER); table. setDefaultRenderer (Object. class, r); column = table. getColumnModel (). getColumn (0); column. setPreferredWidth (120); column = table. getColumnModel (). getColumn (1); column. setPreferredWidth (140); column = table. getColumnModel (). getColumn (2); column. setPreferredWidth (170); column = table. getColumnModel (). getColumn (6); column. setPreferredWidth (30); try {model. setRowCount (0); food_num = in. readInt (); System. out. println (food_num); food = new shortlist <> (); for (I = 0; I
     
      

2. Store Preview

The system displays the store information obtained from the server in JTable format. You can double-click the selected store to enter the store.

Code: ShopPanel. java

Package mypanel; import java. awt. cardLayout; import java. awt. dimension; import java. awt. event. mouseAdapter; import java. awt. event. mouseEvent; import java. io. IOException; import java. util. export list; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JScrollPane; import javax. swing. JTable; import javax. swing. table. defaultTableCellRenderer; import javax. swing. table. tableColumn; import mytool. food; import mytool. shop; import mytool. suggestTabelModel; public class ShopPanel extends JPanel {detail list
       
        
Shop; public writable list
        
         
OrderFood; public Goods List
         
          
Shop1; public writable list
          
           
Shop2; public writable list
           
            
Shop3; int shop_num, shop1_num, shop2_num, shop3_num; public JTable table; SuggestTabelModel model; JScrollPane jscrollPane; final String [] headers = {"no.", "No ", "shop Name"}; Object [] [] cellData = null; private TableColumn column = null; public ShopPanel (StudentPanel studentPanel) {shop = new shop list
            
              (); OrderFood = new shortlist
             
               (); Shop1 = new shortlist
              
                (); Shop2 = new shortlist
               
                 (); Shop3 = new shortlist
                
                  (); Model = new SuggestTabelModel (cellData, headers); table = new JTable (model); jscrollPane = new JScrollPane (table); jscrollPane. setPreferredSize (new Dimension (340,310); add (jscrollPane); // The Center shows DefaultTableCellRenderer r = new DefaultTableCellRenderer (); r. setHorizontalAlignment (JLabel. CENTER); table. setDefaultRenderer (Object. class, r); // column = table. getColumnModel (). getColumn (0); // column. setPreferredWidth (120); // column = table. getColumnModel (). getColumn (1); // column. setPreferredWidth (140); // column = table. getColumnModel (). getColumn (2); // column. setPreferredWidth (170); // column = table. getColumnModel (). getColumn (6); // column. setPreferredWidth (30); try {shop_num = studentPanel. in. readInt (); System. out. println (shop_num); for (int I = 0; I
                 
                  

3. My menu

After you select a menu, the system will add it to my menu. You can preview and modify the menu in this function interface. After confirmation, you can submit and enter the payment interface.

The corresponding classes are MyOrderPanel. java and PayPanel. java. The code is viewed in the source code and will not be pasted out.

4. tucao Area

This function has not yet been implemented. We plan to use UDP to transmit information. Due to time issues, let's talk about it later. If you have a good method, you can reply to me below.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.