-
Please! can anyone help me on this, Please!
Hi!,
Please! anyone help me on this and If anyone read this please help me on this and I don't know what is the problem on this and here is an sample output. Please!, help me on this.
Sample Output:
Enter Month Code: 1
January
Enter Month Code: a
Letter is Not Accepted!!!
Here is the Java Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MonthApp extends Applet implements ActionListener
{
Label lmonth=new Label("Enter Month Code:");
TextField tmonth=new TextField(3);
Font large=new Font("Tahoma",Font.BOLD,15);
Button displaymonth=new Button("Display Month!!!");
public void init()
{
add(lmonth);
add(tmonth);
add(displaymonth);
displaymonth.addActionListener(this);
}
public void actionPerformed(ActionEvent AlcioAbaquita)
{
int monthcode=Integer.parseInt(tmonth.getText());
Label month=new Label("");
Label text=new Label("");
String displaybutton=displaymonth.getLabel();
switch (monthcode)
{
case 1:
month.setText("January");
month.setFont(large);
add(month);
break;
case 2:
month.setText("February");
month.setFont(large);
add(month);
break;
case 3:
month.setText("March");
month.setFont(large);
add(month);
break;
case 4:
month.setText("April");
month.setFont(large);
add(month);
break;
case 5:
month.setText("May");
month.setFont(large);
add(month);
break;
case 6:
month.setText("June");
month.setFont(large);
add(month);
break;
case 7:
month.setText("July");
month.setFont(large);
add(month);
break;
case 8:
month.setText("August");
month.setFont(large);
add(month);
break;
case 9:
month.setText("September");
month.setFont(large);
add(month);
break;
case 10:
month.setText("October");
month.setFont(large);
add(month);
break;
case 11:
month.setText("November");
month.setFont(large);
add(month);
break;
case 12:
month.setText("December");
month.setFont(large);
add(month);
break;
default:
month.setText("Invalid Month Code!!!");
month.setFont(large);
add(month);
text.setText("Letter is Not Accepted!!!");
text.setFont(large);
add(text);
}
validate();
}
}
-
Well, you're only telling your applet to display a moth if the input is a number between 1 and 12. If the input is 'a', it does the default thing in the switch statement.
-
What is the program doing incorrectly? Or what do you want it to do that it is not doing?
It looks like it is working correctly.
-
Where are you generating your action event? I don't find anything in your code which would send a "AlcioAbaquita" message to your ActionListener.
Also - you have a lot of repeating code (all of the display stuff). Can you think of a way to decompose that?
-
What's with the pile of labels ?
I have restructure and enhanced this a bit. Also, I changed the logic that
repeatedly created new labels and added them, didn't look good 
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MonthApp
extends Applet
implements ActionListener {
Label month = new Label("");
Label text = new Label("");
Label lmonth = new Label("Enter Month Code:");
TextField tmonth = new TextField(3);
Font large = new Font("Tahoma", Font.BOLD, 15);
Button displaymonth = new Button("Display Month!!!");
String [] months={
"January","February","March","April","May","June",
"July","August","September","October","November", "December"
};
public void init() {
add(lmonth);
add(tmonth);
add(displaymonth);
add(month);
add(text);
displaymonth.addActionListener(this);
month.setFont(large);
text.setFont(large);
}
public void actionPerformed(ActionEvent ae) {
clearMsg();
String entry=tmonth.getText().trim();
try {
int monthcode = Integer.parseInt(entry);
getMonthByNumber(monthcode);
}
catch (NumberFormatException ex) {
getMonthByString(entry);
}
validate();
}
/**
* Get a month by character match
*/
private void getMonthByString(String entry) {
// check for sensible string input
for (int i=0; i<months.length; i++) {
if (months[i].toLowerCase().startsWith(entry.toLowerCase())) {
getMonthByNumber(i+1);
return;
}
}
issueErrorMsg();
}
/**
* Get a month by number
*/
private void getMonthByNumber(int monthcode) {
if (monthcode >=1 && monthcode <= 12) {
month.setText(months[monthcode-1]);
} else {
issueErrorMsg();
}
}
private void issueErrorMsg() {
month.setText("Invalid Month Code!!!");
text.setText("Input is Not Accepted!!!");
}
private void clearMsg() {
month.setText("");
text.setText("");
}
/**
* Test driver for applet
* @param args
*/
public static void main (String [] args) {
Frame f=new Frame();
f.setLayout(new GridLayout());
MonthApp ma=new MonthApp();
f.add(ma);
f.setBounds(20,20,600,400);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ma.init();
f.setVisible(true);
}
}
Last edited by sjalle; 09-18-2005 at 06:59 PM.
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks