Write a program with a Graphical User Interface (GUI) that inputs a date in the
ID: 3778708 • Letter: W
Question
Write a program with a Graphical User Interface (GUI) that inputs a date in the usual American numerical format of Month/Day/Year and displays it spelling out the month. Display an error message if the day is not between 1 and 31 and the month is not between 1 and 12. We will not worry about trying to catch February 30 or other short months. You can format the GUI in any way that works effectively. One possible format is shown below: Your program should contain an array of the names of the month. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" The numerical month number (minus one) can be used as an index into the array.Explanation / Answer
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JavaApplication2 extends Frame implements ActionListener {
JFrame f;
JLabel l1, l2, l3, l4;
TextField tf1, tf2, tf3;
Button b1, b2;
String[] month1 = {"January", "Febrary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
public static void main(String[] args) {
firstgui.JavaApplication2 ja = new firstgui.JavaApplication2();
}
public JavaApplication2() {
f = new JFrame();
l1 = new JLabel("Enter Date");
l2 = new JLabel("/");
l3 = new JLabel("/");
l4 = new JLabel();
tf1 = new TextField();
tf2 = new TextField();
tf3 = new TextField();
b1 = new Button("Convert");
b2 = new Button("Exit");
l1.setBounds(30, 100, 80, 30);
b1.setBounds(130, 100, 80, 30);
b2.setBounds(230, 100, 80, 30);
f.add(l1);
f.add(tf1);
f.add(l2);
f.add(tf2);
f.add(l3);
f.add(tf3);
f.add(b1);
f.add(l4);
f.add(b2);
f.setLayout(new FlowLayout(FlowLayout.LEFT));
f.setSize(300, 300);
//setLayout();
f.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
String m = tf1.getText();
int i = Integer.parseInt(m);
try {
if ((i < 1) || (i > 11)) {
l4.setText("Incorrect date");
}
String name = month1[i - 1];
l4.setText(name + tf2.getText() + "," + tf3.getText());
} catch (Exception ex) {
}
}
if (e.getSource() == b2) {
System.exit(0);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.