Use the Time2 and Time2Test classes given in the link below and change the test
ID: 665929 • Letter: U
Question
Use the Time2 and Time2Test classes given in the link below and change the test program to make it GUI based. You need to develop a Java GUI based application which should display a User Interface similar to the one shown below. The user should be able to enter values for a Time object in the textboxes at the top of the screen. If the user clicks on the Create Time Object button, the application should create a new instance of a Time Object, likewise, if the user clicks on the Create Time Zoned Object button, a new instance of a Time Zoned Object should be created. The new object should be added to an array list of Time Objects and the new contents of the arraylist should be displayed in the Text Area at the bottom of the application using the appropriate format depending on the state of the UseToUniversalString checkbox.
If the user clicks on the AddTime Button, the value of time in the textboxes should be added to the first object in the arraylist and the display in the textArea refreshed (the value in the Time Zone textbox should be ignored). When the user changes the state of the UseToUniversalString checkbox, the contents of the textArea shoud be refreshed in the appropriate format.
The set of Time and Timed Zone Objects entered by the user should be kept in an arrayList of Time Objects (Remember that the Timed Zone objects are ALSO time objects since Time Zoned Class extends Time). When inappropriate values are used, the program should pop an optionPane message (and do nothing) for example , when trying to create a Time object with insane values like 36 Hours 10 Minutes 15 Seconds, etc. or when adding time to an object would make the new time value in the object overflow to the next day.
Here is a link for time2.java file
https://cs.fit.edu/~mmahoney/cis5100/examples/ch08/fig08_05_06/Time2.java
And link for Time2Test. java
https://cs.fit.edu/~mmahoney/cis5100/examples/ch08/fig08_05_06/Time2Test.java
Here is a link for sample design http://imgur.com/okAV58v
Explanation / Answer
package time;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JComboBox;
public class TimeZone {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextArea textArea;
private ArrayList<Object> alltimeobjects=null;
private JComboBox comboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TimeZone window = new TimeZone();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TimeZone() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 657, 429);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(85, 73, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(244, 73, 86, 20);
frame.getContentPane().add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(417, 73, 86, 20);
frame.getContentPane().add(textField_2);
JLabel lblHh = new JLabel("hh:");
lblHh.setBounds(54, 76, 21, 14);
frame.getContentPane().add(lblHh);
textArea = new JTextArea();
textArea.setBounds(125, 205, 342, 158);
frame.getContentPane().add(textArea);
JLabel lblMm = new JLabel("mm:");
lblMm.setBounds(208, 76, 26, 14);
frame.getContentPane().add(lblMm);
JLabel lblSs = new JLabel("ss:");
lblSs.setBounds(386, 76, 21, 14);
frame.getContentPane().add(lblSs);
JButton btnAddTime = new JButton("Add Time");
btnAddTime.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
textArea.setText("");
System.out.println(alltimeobjects);
for (int i = 0; i < alltimeobjects.size(); i++) {
if("12".equals(comboBox.getSelectedItem().toString()))
textArea.setText(alltimeobjects.get(i).toString()+" ");
else
textArea.setText(((Time2) alltimeobjects.get(i)).toUniversalString()+" ");
}
}
});
btnAddTime.setBounds(333, 133, 89, 23);
frame.getContentPane().add(btnAddTime);
JButton btnCreateTimezone = new JButton("Create TimeZone");
alltimeobjects=new ArrayList<Object>();
btnCreateTimezone.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
String regex="\d+";
String hh="".equals(textField.getText())?"00":textField.getText();
String mm="".equals(textField_1.getText())?"00":textField_1.getText();
String ss="".equals(textField_2.getText())?"00":textField_2.getText();
Time2 t=new Time2(Integer.parseInt(hh),Integer.parseInt(mm),Integer.parseInt(ss));
alltimeobjects.add(t);
}
});
btnCreateTimezone.setBounds(126, 133, 150, 23);
frame.getContentPane().add(btnCreateTimezone);
comboBox = new JComboBox();
comboBox.setBounds(536, 73, 67, 20);
comboBox.addItem("12");
comboBox.addItem("24");
frame.getContentPane().add(comboBox);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.