Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Need *Help fixing this
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


remi
Need *Help fixing this
3 Answer(s)      5 years and 4 months ago
Posted in : Java Beginners



Advertisement
View Answers

August 18, 2009 at 11:53 AM


Hi Friend,

We are providing you the code that will first prompt the user to enter username and password and if they input correct username and password then they will move to next page i.e Pool Form.

1)LoginForm.java

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

class Login extends JFrame implements ActionListener
{
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);

label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);

SUBMIT=new JButton("SUBMIT");

panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
}
public void actionPerformed(ActionEvent ae) {
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("roseindia") && value2.equals("roseindia")) {
PoolVolume page=new PoolVolume();
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
class LoginForm{
public static void main(String arg[]) {
try {
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}


August 18, 2009 at 11:55 AM


continue................

2) PoolVolume.java

import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class PoolVolume extends JFrame{
private JFrame mainFrame;
private JButton calcButton;
private JButton clearButton;
private JButton saveButton;
private JButton exitButton;
private JTextField length;
private JTextField width;
private JTextField average;
private JTextField volume;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel averageLabel;
private JLabel volumeLabel;
public PoolVolume() {
this.setLayout(new BorderLayout());
mainFrame = new JFrame("Swimming Pool Volume Calculator");
calcButton = new JButton("Calculate volume");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
saveButton=new JButton("Save");
length = new JTextField(5);
width = new JTextField(5);
average = new JTextField(5);
volume = new JTextField(5);
lengthLabel = new JLabel("Enter the length of the swimming pool: ");
widthLabel = new JLabel("Enter the width of the swimming pool: ");
averageLabel = new JLabel("Enter the average dept of the swimming pool: ");
volumeLabel = new JLabel("Swimming pool volume: ");
Container c =
mainFrame.getContentPane();
c.setLayout(
new FlowLayout());
c.add(lengthLabel, BorderLayout.SOUTH);
c.add(length);
c.add(
widthLabel);
c.add(width);
c.add(averageLabel);
c.add(average);
c.add(volumeLabel);
c.add(volume);
c.add(calcButton);
c.add(clearButton);
c.add(exitButton);
c.add(saveButton);
calcButton.setMnemonic('C');
clearButton.setMnemonic('l');
exitButton.setMnemonic('x');
mainFrame.setSize(400,300);
mainFrame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {System.exit(0);}});

calcButtonHandler chandler =new calcButtonHandler();
calcButton.addActionListener(chandler);
clearButtonHandler lHandler =new clearButtonHandler();
clearButton.addActionListener(lHandler);
exitButtonHandler ehandler =new exitButtonHandler();
exitButton.addActionListener(ehandler);
saveButtonHandler shandler =new saveButtonHandler();
saveButton.addActionListener(shandler);
FocusHandler fhandler =new FocusHandler();
length.addFocusListener(fhandler);
width.addFocusListener(fhandler);
average.addFocusListener(fhandler);
volume.addFocusListener(fhandler);
mainFrame.setVisible(true);
}

class calcButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
DecimalFormat num =new DecimalFormat(",###.##");
Double cLength, cWidth, cAverage, cVolume, Total;
String sLength, sWidth, sAverage, sVolume;
sLength =length.getText();
cLength = Double.parseDouble(sLength);
sWidth =width.getText();
cWidth = Double.parseDouble(sWidth);
sAverage =average.getText();
cAverage = Double.parseDouble(sAverage);
if(e.getSource() == calcButton) {
Total = cLength * cWidth * cAverage;
volume.setText(num.format(Total));
}
}
}


August 18, 2009 at 11:56 AM


continue....................

class clearButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
length.setText("");
width.setText("");
average.setText("");
volume.setText("");
length.grabFocus();
}
}

class exitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}

class saveButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
String value=volume.getText();
try{
File file = new File("output.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("The volume of Swimming Pool is "+value);
out.newLine();
out.close();
}
catch(Exception ex){}
}
}

class FocusHandler implements FocusListener {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
}
}
public static void main(String args[]){
new PoolVolume();
}
}

Thanks


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com