David Garton
Posts: 3
Nickname: davidkiwi
Registered: Aug, 2002
|
|
Re: Help with GUI
|
Posted: Aug 16, 2002 3:23 PM
|
|
Here is something I slapped together very quickly, maybe it'll help you get started. As you can see I just used a normal JTextField for the password, but you'll easily be able to replace this with a proper password box. Anyway here it is, good luck.
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) { //Grab the user's input and compare it to //something valid 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() { //make the main window JFrame mainWindow = new JFrame(); mainWindow.setSize(600, 600); mainWindow.setVisible(true);
//make the entry window invisible, however I guess //it would be better to close it completely. setVisible(false); } }
|
|