how 2 make GUI from oop application


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: how 2 make GUI from oop application

  1. #1
    Join Date
    Aug 2002
    Posts
    5

    how 2 make GUI from oop application

    hi all, i need your help urgently as i have to finish my project by this week.
    iv got all the classes and methods that take care of everything that my application will need.

    however i dont know how to create the GUI and implement the listener etc.. to take care of menu selections, making them do what they're supposed to do. can anyone help me?

    thank you

  2. #2
    Join Date
    Jul 2002
    Posts
    26
    Take a look at the Swing tutorial from sun:

    http://java.sun.com/docs/books/tutorial/uiswing/

    -gc

  3. #3
    Join Date
    Aug 2002
    Posts
    5
    here's something simple that may help....


    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class guiExample extends JFrame
    {
    JButton closeBtn, submitBtn, clearBtn;
    JTextField unameTxt, passTxt;

    public static void main(String[] args)
    {
    new guiExample().entryGUI();
    }

    public void entryGUI()
    {
    Container frmContent = getContentPane();
    frmContent.setLayout(new BorderLayout());

    //Create the username label and textfield
    JPanel userPanel = new JPanel();
    JLabel unameLbl = new JLabel("User name");
    unameTxt = new JTextField(15);
    userPanel.add(unameLbl);
    userPanel.add(unameTxt);

    //Create the password label and textfield
    JPanel passPanel = new JPanel();
    JLabel passLbl = new JLabel("Password");
    passTxt = new JTextField(15);
    passPanel.add(passLbl);
    passPanel.add(passTxt);

    //Create the buttons
    JPanel buttonPanel = new JPanel();
    closeBtn = new JButton("Close");
    submitBtn = new JButton("Submit");
    clearBtn = new JButton("Clear");
    closeBtn.addActionListener(new buttonListener());
    submitBtn.addActionListener(new buttonListener());
    clearBtn.addActionListener(new buttonListener());
    buttonPanel.add(closeBtn);
    buttonPanel.add(submitBtn);
    buttonPanel.add(clearBtn);

    frmContent.add(userPanel, BorderLayout.NORTH);
    frmContent.add(passPanel);
    frmContent.add(buttonPanel, BorderLayout.SOUTH);

    setSize(350, 300);
    setLocation(50,100);
    setVisible(true);
    }

    public class buttonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent event)
    {
    Object source = event.getSource();
    if(source == closeBtn)
    System.exit(0);

    else if(source == submitBtn)
    {
    String uname = unameTxt.getText();
    String pass = passTxt.getText();

    //I guess here you would have to connect to a database so the program could
    //compare the user's input against all the ones contained in the database.
    //For now I'll just compare the user's input with "scott" and "tiger".
    if(uname.equals("scott") & pass.equals("tiger"))
    createMain();
    }

    else if(source == clearBtn)
    {
    unameTxt.setText("");
    passTxt.setText("");
    }
    }
    }

    public void createMain()
    {
    System.out.println("in main");
    JFrame mainWindow = new JFrame();
    mainWindow.setSize(600, 600);
    mainWindow.setVisible(true);
    setVisible(false);
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center