In: Computer Science
Create a UI that has a field in which to enter text, a field to display read-only text and a button. When the button is pressed, take the value of the text box and try to create a month object. If a number was entered, use the constructor that accepts an integer. If alphabetic characters were entered, use the constructor that accepts a string. If nothing is entered, use the no-argument constructor. If a valid month object is created, use the toString function on the month object to display info about the month. Otherwise, display an appropriate error message.
Create a UI with two drop-down lists, a button, and a field to display read-only text. One drop-down has the values 1-12. The other has the names of the months. When the button is pressed, create two-month objects using the appropriate constructors, then display if the months are equal, if month 1 > month 2 or if month 2 > month 1.
TWO SEPERATE USER INTERFACES.
THIS IS TO BE DONE USING JAVA
//Java code
import javax.swing.*; public class Month { private final static String[] monthsName ={"January","February","March","April","May","June","July","August","September","October","November","December"}; private String monthName; public Month() { this.monthName = monthsName[0]; } public Month(int monthNumber) { if(monthNumber>=1 && monthNumber<=12) { monthName = monthsName[monthNumber-1]; } else { errorMessage("Invalid month name","MONTH NAME ERROR!"); return; } } public Month(String monthName) { setMonthName(monthName); } public String getMonthName() { return monthName; } public void setMonthName(String monthName) { boolean isValid = false; for (int i = 0; i <monthsName.length ; i++) { if(monthsName[i].equalsIgnoreCase(monthName)) { isValid = true; this.monthName = monthsName[i]; } } if(!isValid) { errorMessage("Invalid month name","MONTH NAME ERROR!"); return; } } public static void errorMessage(String message, String title) { JOptionPane.showMessageDialog(null,message,title,JOptionPane.ERROR_MESSAGE); } }
//========================================
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.InvocationTargetException; public class MonthGUI extends JFrame { JLabel lblPrompt; JTextField txtMonth; JButton btnDisplay; JLabel lblResult; public MonthGUI() { setTitle("Month GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); setSize(400,150); setVisible(true); lblPrompt = new JLabel("Enter month"); txtMonth = new JTextField(10); btnDisplay = new JButton("Display Month"); lblResult = new JLabel(); //Add all components to frame add(lblPrompt); add(txtMonth); add(btnDisplay); add(lblResult); //add action listener to button btnDisplay.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Month object Month month; String text = txtMonth.getText(); if(text.equals("")) { month = new Month(); lblResult.setText("Month is "+month.getMonthName()); } else if(text.matches("[0-9]+")) { month = new Month(Integer.parseInt(text)); lblResult.setText("Month is "+month.getMonthName()); } else { month = new Month(text); lblResult.setText("Month is "+month.getMonthName()); } } }); } public static void main(String[] args) throws InvocationTargetException, InterruptedException { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { new MonthGUI(); } }); } }
//Output
//Java code 2
import javax.swing.*; public class Month { private final static String[] monthsName ={"January","February","March","April","May","June","July","August","September","October","November","December"}; private String monthName; public Month() { this.monthName = monthsName[0]; } public Month(int monthNumber) { if(monthNumber>=1 && monthNumber<=12) { monthName = monthsName[monthNumber-1]; } else { errorMessage("Invalid month name","MONTH NAME ERROR!"); return; } } public Month(String monthName) { setMonthName(monthName); } public String getMonthName() { return monthName; } public void setMonthName(String monthName) { boolean isValid = false; for (int i = 0; i <monthsName.length ; i++) { if(monthsName[i].equalsIgnoreCase(monthName)) { isValid = true; this.monthName = monthsName[i]; } } if(!isValid) { errorMessage("Invalid month name","MONTH NAME ERROR!"); return; } } public int getIndex(String month) { for (int i = 0; i <monthsName.length ; i++) { if(monthsName[i].equalsIgnoreCase(month)) return i; } return 0; } public static void errorMessage(String message, String title) { JOptionPane.showMessageDialog(null,message,title,JOptionPane.ERROR_MESSAGE); } }
//=====================================
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.InvocationTargetException; public class MonthGUI extends JFrame { JComboBox<Integer>cmbMonthNumbers; JComboBox<String>cmbMonthName; JButton btnDisplay; JLabel lblResult; Integer[]monthsNumber = {1,2,3,4,5,6,7,8,9,10,11,12}; String[] monthsName ={"January","February","March","April","May","June","July","August","September","October","November","December"}; public MonthGUI() { setTitle("Month GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); setSize(400,150); setVisible(true); cmbMonthNumbers = new JComboBox<>(); cmbMonthNumbers.setModel(new DefaultComboBoxModel<Integer>(monthsNumber)); cmbMonthName = new JComboBox<>(); cmbMonthName.setModel(new DefaultComboBoxModel<>(monthsName)); btnDisplay = new JButton("Display Month"); lblResult = new JLabel(); //Add all components to frame add(cmbMonthName); add(cmbMonthNumbers); add(btnDisplay); add(lblResult); //add action listener to button btnDisplay.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Month object Month month,month1; String text1 = cmbMonthName.getSelectedItem().toString(); String text2 = cmbMonthNumbers.getSelectedItem().toString(); if(text1.equals("")) { month = new Month(); } else if(text1.matches("[0-9]+")) { month = new Month(Integer.parseInt(text1)); } else { month = new Month(text1); } if(text2.equals("")) { month1 = new Month(); } else if(text2.matches("[0-9]+")) { month1 = new Month(Integer.parseInt(text2)); } else { month1 = new Month(text2); } if(month.getIndex(month.getMonthName())> month1.getIndex(month1.getMonthName())) { lblResult.setText("Month1 is greater than Month2"); } else if(month.getIndex(month.getMonthName())< month1.getIndex(month1.getMonthName())) { lblResult.setText("Month1 is smaller than Month2"); } else { lblResult.setText("Month1 and Month2 are equal."); } } }); } public static void main(String[] args) throws InvocationTargetException, InterruptedException { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { new MonthGUI(); } }); } }
//Output
//If you need any help regarding this solution ......... please leave a comment ........ thanks