Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: help with debug
|
Posted: Dec 16, 2002 12:57 PM
|
|
Hmm... Well you've created this class called "labB" for some reason, instead of just adding a method to your existing class. You've also changed the padThis() method to ignore its second parameter, which is a pretty silly thing to do.
In any case, you never even call the padThis() method, so that's why the text is not getting padded.
By the way, what the heck happened with the formatting? Do you really work with your code in that scattered state, or does it just look this way when posted into the message?
Anyway, here is a somewhat repaired version of your post:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class checkVal extends JFrame
{
private int fieldWidth = 9;
private final String entryFormatMessage = "Please enter only valid " +
"numbers, without letters " +
"or other symbols.";
private JTextField input;
private JTextArea outputArea;
private double p;
public checkVal()
{
super("Check Protection Program");
getContentPane().setLayout(new FlowLayout());
JLabel prompt = new JLabel("Enter Check Amount");
getContentPane().add(prompt);
input = new JTextField(10);
JButton b = new JButton(" Protect ");
b.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
p = Double.parseDouble(input.getText());
//if user enter's 5 convert to 5.00 or 5.8 to 5.80
DecimalFormat twoDigits = new DecimalFormat("0.00");
String h = twoDigits.format(p);
String y = padThis(h, fieldWidth);
outputArea.append(y.toString() + "\n");
}
catch( java.lang.NumberFormatException nfe )
{
JOptionPane.showMessageDialog( checkVal.this,
entryFormatMessage );
}
}
});
getContentPane().add(input);
getContentPane().add(b);
outputArea = new JTextArea(10, 20); //width,height panel
outputArea.setEditable(false);
getContentPane().add(new JScrollPane(outputArea));
setSize(275, 270); // width,height Win grey
show();
}
private String padThis(String text, int lenny)
{
if(lenny < 0)
throw new IllegalArgumentException("Length can't be negative!");
StringBuffer buffy = new StringBuffer(text);
buffy.insert(0, "*"); // Should always have at least 1 asterisk.
while(buffy.length() < lenny)
buffy.insert(0, "*");
return buffy.toString();
}
public static void main(String args[])
{
checkVal window = new checkVal();
window.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
}
}
|
|