Guest User

Untitled

a guest
Jul 4th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6.  
  7.  
  8. // All the events are in the creation of each control so that I can separate the model from layout
  9. // Just like HTML / CSS
  10.  
  11.  
  12. public class AuctionGUI {
  13.     // Things for the server
  14.     AuctionConnector server;
  15.    
  16.     // Create the controls and their actions
  17.     // Username
  18.     JLabel usernameLabel = new JLabel(){{ setText("Username:"); }};
  19.     JTextField usernameText = new JTextField(){{ setPreferredSize(new Dimension(190,20)); }};
  20.     // Password
  21.     JLabel passwordLabel = new JLabel(){{ setText("Password:"); }};
  22.     JTextField passwordText = new JTextField(){{ setPreferredSize(new Dimension(190,20)); }};
  23.    
  24.     // Create button for login
  25.     JButton loginButton = new JButton("Login"){{
  26.         addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {
  27.             status.setText("Logging in as "+usernameText.getText());
  28.             if(server.login(usernameText.getText(), passwordText.getText())) { status.setText("Logged in"); }
  29.             else { status.setText("Error"); }
  30.                
  31.         }});
  32.     }};
  33.    
  34.     // List of running auctions
  35.     DefaultListModel listModel = new DefaultListModel();
  36.     JList auctionList = new JList(listModel){{ setPreferredSize(new Dimension(260,150)); }};
  37.    
  38.     JButton updateList = new JButton("Update List"){{
  39.         addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
  40.             String list = server.getList();
  41.             String[] temp = list.split("\r\n");
  42.             listModel.removeAllElements();
  43.             for (int i = 0; i< temp.length; i++) { listModel.addElement(temp[i]); }
  44.         }});
  45.     }};
  46.    
  47.     JButton addAuction = new JButton("Create"){{
  48.         addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
  49.            
  50.         }});
  51.     }};
  52.    
  53.     JButton endAuction = new JButton("End"){{
  54.         addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
  55.            
  56.         }});
  57.     }};
  58.    
  59.     JButton bidAuction = new JButton("Bid"){{
  60.         addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
  61.                
  62.         }});
  63.     }};
  64.    
  65.     JLabel status = new JLabel("Waiting for input...");
  66.    
  67.     /**
  68.      *  Create a new GUI to work with the given AuctionConnector
  69.      * @param server is the AuctionConnector object for the server
  70.      */
  71.     AuctionGUI(AuctionConnector server) {
  72.         this.server = server;
  73.         // Position stuff :D
  74.        
  75.         // JFrame
  76.         new JFrame(){{
  77.            
  78.             // Top bar
  79.             add(new JPanel(){{
  80.                 // Username field
  81.                 add(new JPanel(){{
  82.                     add(usernameLabel);
  83.                     add(usernameText);
  84.                     setLayout(new FlowLayout());
  85.                 }});
  86.                
  87.                 // Password field
  88.                 add(new JPanel(){{
  89.                     add(passwordLabel);
  90.                     add(passwordText);
  91.                     setLayout(new FlowLayout());
  92.                 }});
  93.                
  94.                 // Login Button
  95.                 add(loginButton);
  96.                
  97.                 // Status Bar
  98.                 add(status);
  99.                 setLayout(new FlowLayout(FlowLayout.CENTER));
  100.             }});
  101.            
  102.            
  103.             // List of items to bid on
  104.             add(new JPanel(){{
  105.                 add(auctionList);
  106.                 add(updateList);
  107.                
  108.                 add(addAuction);
  109.                 add(endAuction);
  110.                 add(bidAuction);
  111.                
  112.                 setLayout(new FlowLayout());
  113.             }});
  114.            
  115.            
  116.             // Set layout for main pane
  117.             setVisible(true);
  118.             setLayout(new GridLayout(0,2));
  119.             setPreferredSize(new Dimension(620,250));
  120.             pack();
  121.         }};
  122.     }
  123.    
  124.     /**
  125.      * reloads the list of auctions from the list given.
  126.      * @param list
  127.      */
  128.     void loadAuctions(String list) {
  129.         System.out.println("Running Auctions: \r\n"+list);
  130.     }
  131.    
  132.    
  133. }
Add Comment
Please, Sign In to add comment