eclair
Mar 10th, 2003, 08:14 AM
I am fairly new to java and I dont really know what I am doing so if someone could help me it would b great.
I am coding a login screen, where the user enters a name and password and when they press "Ok" I want a welcome message to appear. I tried this code below but keep getting 1 error.
Please help me!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PassWordInterface extends JFrame implements ActionListener{
private JLabel nameLabel, passWordLabel, EMCLabel;
private JTextField nameTextField, passwordTextField;
private JButton okButton, CancelButton;
private FlowLayout layout;
public PassWordInterface(){
super("Enter Password");
setBounds(200, 200, 350, 200);
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel pane = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.setLayout(layout);
ImageIcon EMC = new ImageIcon("EMC.gif");
createConstraints(constraints, 0, 0, 4, 1, 0, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
JLabel EMCLabel = new JLabel(EMC);
layout.setConstraints(EMCLabel, constraints);
pane.add(EMCLabel);
createConstraints(constraints, 0, 1, 1, 1, 50, 20);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel nameLabel = new JLabel ("Name:", JLabel.LEFT);
layout.setConstraints(nameLabel, constraints);
pane.add(nameLabel);
createConstraints(constraints, 1, 1, 2, 1, 50, 20);
constraints.fill = GridBagConstraints.HORIZONTAL;
JTextField nameTextField = new JTextField();
layout.setConstraints(nameTextField, constraints);
pane.add(nameTextField);
createConstraints(constraints, 0, 2, 1, 1, 50, 20);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel passWordLabel = new JLabel ("Password:", JLabel.LEFT);
layout.setConstraints(passWordLabel, constraints);
pane.add(passWordLabel);
createConstraints(constraints, 1, 2, 2, 1, 50, 20);
constraints.fill = GridBagConstraints.HORIZONTAL;
JPasswordField passwordTextField = new JPasswordField();
passwordTextField.setEchoChar('*');
layout.setConstraints(passwordTextField, constraints);
pane.add(passwordTextField);
createConstraints(constraints, 0, 3, 1, 1, 30, 40);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
JButton okButton = new JButton ("OK");
layout.setConstraints(okButton, constraints);
pane.add(okButton);
okButton.addActionListener(this);
createConstraints(constraints, 2, 3, 1, 4, 40, 40);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
JButton CancelButton = new JButton ("Cancel");
layout.setConstraints(CancelButton, constraints);
pane.add(CancelButton);
CancelButton.addActionListener(this);
setContentPane(pane);
}
private void createConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy){
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
public static void main (String args[]){
PassWordInterface test = new PassWordInterface();
test.show();
}
public void ActionPerformed(ActionEvent e)
{
if (e.getSource()==okButton)
{
JOptionPane.showMessageDialog(null, "Welcome "+ nameTextField.getText());
}
if (e.getSource()==CancelButton)
{
nameTextField.setText("");
passwordTextField.setText("");
}
}
}
I am coding a login screen, where the user enters a name and password and when they press "Ok" I want a welcome message to appear. I tried this code below but keep getting 1 error.
Please help me!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PassWordInterface extends JFrame implements ActionListener{
private JLabel nameLabel, passWordLabel, EMCLabel;
private JTextField nameTextField, passwordTextField;
private JButton okButton, CancelButton;
private FlowLayout layout;
public PassWordInterface(){
super("Enter Password");
setBounds(200, 200, 350, 200);
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel pane = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.setLayout(layout);
ImageIcon EMC = new ImageIcon("EMC.gif");
createConstraints(constraints, 0, 0, 4, 1, 0, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
JLabel EMCLabel = new JLabel(EMC);
layout.setConstraints(EMCLabel, constraints);
pane.add(EMCLabel);
createConstraints(constraints, 0, 1, 1, 1, 50, 20);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel nameLabel = new JLabel ("Name:", JLabel.LEFT);
layout.setConstraints(nameLabel, constraints);
pane.add(nameLabel);
createConstraints(constraints, 1, 1, 2, 1, 50, 20);
constraints.fill = GridBagConstraints.HORIZONTAL;
JTextField nameTextField = new JTextField();
layout.setConstraints(nameTextField, constraints);
pane.add(nameTextField);
createConstraints(constraints, 0, 2, 1, 1, 50, 20);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel passWordLabel = new JLabel ("Password:", JLabel.LEFT);
layout.setConstraints(passWordLabel, constraints);
pane.add(passWordLabel);
createConstraints(constraints, 1, 2, 2, 1, 50, 20);
constraints.fill = GridBagConstraints.HORIZONTAL;
JPasswordField passwordTextField = new JPasswordField();
passwordTextField.setEchoChar('*');
layout.setConstraints(passwordTextField, constraints);
pane.add(passwordTextField);
createConstraints(constraints, 0, 3, 1, 1, 30, 40);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
JButton okButton = new JButton ("OK");
layout.setConstraints(okButton, constraints);
pane.add(okButton);
okButton.addActionListener(this);
createConstraints(constraints, 2, 3, 1, 4, 40, 40);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
JButton CancelButton = new JButton ("Cancel");
layout.setConstraints(CancelButton, constraints);
pane.add(CancelButton);
CancelButton.addActionListener(this);
setContentPane(pane);
}
private void createConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy){
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
public static void main (String args[]){
PassWordInterface test = new PassWordInterface();
test.show();
}
public void ActionPerformed(ActionEvent e)
{
if (e.getSource()==okButton)
{
JOptionPane.showMessageDialog(null, "Welcome "+ nameTextField.getText());
}
if (e.getSource()==CancelButton)
{
nameTextField.setText("");
passwordTextField.setText("");
}
}
}