The Artima Developer Community
Sponsored Link

Java Answers Forum
JButtons help

1 reply on 1 page. Most recent reply: Jun 17, 2002 12:21 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

JButtons help Posted: Jun 4, 2002 10:35 PM
Reply to this message Reply
Advertisement
I have to write this program that has a login screen (a JFrame), that will let you put a name in, then click ok, and go to another JFrame (main menu), and display that name in a JLabel at the top of the page. Does anyone know how I could make the ok button open the main menu JFrame.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: JButtons help Posted: Jun 17, 2002 12:21 AM
Reply to this message Reply

/* Access.java
* @author: Charles Bell
* @version: Jun 17, 2002
*/

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

/** Access
*/
public class Access extends JFrame{

public Access(){
super("Access");
init();
}

public static void main(String[] args){
Access access = new Access();
}

public void init(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String user = login();
JPanel topPanel = new JPanel();
JPanel middlePanel = new JPanel();
JPanel bottomPanel = new JPanel();
topPanel.add(new JLabel(user));
middlePanel.add(new JLabel("Welcome to my web site."));
bottomPanel.add(new JLabel("Please feel free to write more Java programs."));
getContentPane().add(topPanel, BorderLayout.NORTH);
getContentPane().add(middlePanel, BorderLayout.CENTER);
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
show();
}

private String login(){
String userName = "";
JFrame f = new JFrame();
JTextField nameField = new JTextField(8);
JPasswordField passwordField = new JPasswordField(8);
passwordField.setEchoChar('*');
String message = "Login:";
Object[] array = {message,nameField, passwordField};
//JOptionPane editpane = new JOptionPane(textfield);
JPanel loginPanel = new JPanel();
JPanel namePanel = new JPanel();
JPanel accessCodePanel = new JPanel();
loginPanel.setLayout(new BorderLayout());
namePanel.setLayout(new BorderLayout());
accessCodePanel.setLayout(new BorderLayout());
namePanel.add(new JLabel("User Name: "), BorderLayout.WEST);
namePanel.add(nameField, BorderLayout.EAST);
accessCodePanel.add(new JLabel("Password: "), BorderLayout.WEST);
accessCodePanel.add(passwordField, BorderLayout.EAST);
loginPanel.add(namePanel, BorderLayout.NORTH);
loginPanel.add(accessCodePanel, BorderLayout.SOUTH);
JOptionPane loginPane = new JOptionPane(loginPanel, JOptionPane.PLAIN_MESSAGE,
JOptionPane.DEFAULT_OPTION);
JDialog dialog = loginPane.createDialog(f,message);
//center frame on screen
f.setLocationRelativeTo(null);
dialog.show();
userName = nameField.getText();
String password = new String(passwordField.getPassword());
//put your own validation code in here
return userName;
}
}

Flat View: This topic has 1 reply on 1 page
Topic: do classes have properties Previous Topic   Next Topic Topic: How to add java application to the taskbar tray?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use