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();
}
}
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 :cool:
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);
}
}