This is the coding for the Pool Tab: import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.text.DecimalFormat;

paltryboarpigΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

78 εμφανίσεις

This is the coding for the Pool Tab
:


import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import

j
ava.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import

java.text.DecimalFormat;

import
javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;


public class Pool extends JPanel {

// private JFrame mainFrame;


JTextField selectPoolText;

private JButton calculateButto
n;

private JButton exitButton;

private JTextField lengthField;

private JTextField widthField;

private JTextField depthField;

private JTextField volumeField;

private JLabel lengthLabel;

private JLabel widthLabel;

private JLabel depthLabel;

private JLabel vo
lumeLabel;


public Pool() {

// mainFrame = new JFrame("Pools");


calculateButton = new JButton("Calculate volume");

exitButton = new JButton("Exit");

lengthLabel = new JLabel("Enter the pool's length (ft):");

widthLabel = new JLabel("Enter the pool's width

(ft):");

depthLabel = new JLabel("Enter the pool's depth (ft):");

volumeLabel = new JLabel("The pool's volume is (ft^3):");

lengthField = new JTextField(5);

widthField = new JTextField(5);

depthField = new JTextField(5);

volumeField = new JTextField(5);

/
/Container c = this.getContentPane();

this.setLayout(new FlowLayout());

this.add(lengthLabel);

this.add(lengthField);


this.add(widthLabel);

this.add(widthField);


this.add(depthLabel);

this.add(depthField
);


this.add(calculateButton);

this.add(exitButton
);



this.add(volumeLabel);

this.add(volumeField);


calculateButton.setMnemonic('C');

exitButton.setMnemonic('x');


calculateButtonHandler chandler = new calculateButtonHandler();

calculateButton.addActionListener(chandler);


ExitButtonHandler ehandler

= new ExitButtonHandler();

exitButton.addActionListener(ehandler);



FocusHandler fhandler = new FocusHandler();

lengthField.addFocusListener(fhandler);

widthField.addFocusListener(fhandler);

depthField.addFocusListener(fhandler);

volumeField.addFocusListener(fhandler);


this.show();

}


class calculateButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

DecimalFormat num = new DecimalFormat(" , ###.## ");

double width=0, length=0, depth=0, volume;


String instring;

instring = lengthField.getText();

length = Double.parseDouble(instring);

instring = widthField.getText();

width = Double.parseDouble(instring);

instring = depthField.getText();

depth = Double.parseDouble(instring);

volume

= depth * length * width;

volumeField.setText(Double.toString(volume));

}

}


class ExitButtonHandler implements ActionListener

{

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

}


class FocusHandler implements FocusListener

{

public void focusGained(FocusEvent e) {

if(e.getSource() == lengthField || e.getSource() == widthField || e.getSource() == depthField)

{

volumeField.setText(" ");

}

else if(e.getSource() == volumeField)

{

volumeField.setNextFocusableComponent(
calculateButton);

calculateButton.grabFocus();

}

}


public void focusLost(FocusEvent e) {

if(e.getSource() == widthField)

{

widthField.setNextFocusableComponent(calculateButton);

}

}

}

}