Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- // All the events are in the creation of each control so that I can separate the model from layout
- // Just like HTML / CSS
- public class AuctionGUI {
- // Things for the server
- AuctionConnector server;
- // Create the controls and their actions
- // Username
- JLabel usernameLabel = new JLabel(){{ setText("Username:"); }};
- JTextField usernameText = new JTextField(){{ setPreferredSize(new Dimension(190,20)); }};
- // Password
- JLabel passwordLabel = new JLabel(){{ setText("Password:"); }};
- JTextField passwordText = new JTextField(){{ setPreferredSize(new Dimension(190,20)); }};
- // Create button for login
- JButton loginButton = new JButton("Login"){{
- addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {
- status.setText("Logging in as "+usernameText.getText());
- if(server.login(usernameText.getText(), passwordText.getText())) { status.setText("Logged in"); }
- else { status.setText("Error"); }
- }});
- }};
- // List of running auctions
- DefaultListModel listModel = new DefaultListModel();
- JList auctionList = new JList(listModel){{ setPreferredSize(new Dimension(260,150)); }};
- JButton updateList = new JButton("Update List"){{
- addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
- String list = server.getList();
- String[] temp = list.split("\r\n");
- listModel.removeAllElements();
- for (int i = 0; i< temp.length; i++) { listModel.addElement(temp[i]); }
- }});
- }};
- JButton addAuction = new JButton("Create"){{
- addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
- }});
- }};
- JButton endAuction = new JButton("End"){{
- addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
- }});
- }};
- JButton bidAuction = new JButton("Bid"){{
- addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) {
- }});
- }};
- JLabel status = new JLabel("Waiting for input...");
- /**
- * Create a new GUI to work with the given AuctionConnector
- * @param server is the AuctionConnector object for the server
- */
- AuctionGUI(AuctionConnector server) {
- this.server = server;
- // Position stuff :D
- // JFrame
- new JFrame(){{
- // Top bar
- add(new JPanel(){{
- // Username field
- add(new JPanel(){{
- add(usernameLabel);
- add(usernameText);
- setLayout(new FlowLayout());
- }});
- // Password field
- add(new JPanel(){{
- add(passwordLabel);
- add(passwordText);
- setLayout(new FlowLayout());
- }});
- // Login Button
- add(loginButton);
- // Status Bar
- add(status);
- setLayout(new FlowLayout(FlowLayout.CENTER));
- }});
- // List of items to bid on
- add(new JPanel(){{
- add(auctionList);
- add(updateList);
- add(addAuction);
- add(endAuction);
- add(bidAuction);
- setLayout(new FlowLayout());
- }});
- // Set layout for main pane
- setVisible(true);
- setLayout(new GridLayout(0,2));
- setPreferredSize(new Dimension(620,250));
- pack();
- }};
- }
- /**
- * reloads the list of auctions from the list given.
- * @param list
- */
- void loadAuctions(String list) {
- System.out.println("Running Auctions: \r\n"+list);
- }
- }
Add Comment
Please, Sign In to add comment