Reputation: 97
Not sure how to use a string I made in another class as the body text for a JMessage box I made. Any other suggestions are welcome. FYI what I am trying to do is get a message box with an output that is defined in another class to display on button press, the box will display, but not when I set it's body to the string from the other class. I also am determining this value when the button is pressed in another class and pushing the values FROM my JFrame class to my class that is handling all of my calculation stuff. Here's the code:
JFrame class code:
package clock;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class displayFrame extends JPanel{
private static final long serialVersionUID = 1L;
public static int hrsIn;
public static int daysIn;
public static int monthsIn;
public static int yearsIn;
public static int timeAdd;
public static JButton enter;
public static JLabel dayLabel, hourLabel, monthLabel, yearLabel, timeAdded1, newTime;
public static JTextField dayField, hourField, monthField, yearField, timeAdded;
public static JFrame frame;
public static JPanel newTimePanel;
public displayFrame()
{
super(new BorderLayout());
enter = new JButton("Okay");
dayField = new JTextField(20);
hourField = new JTextField(20);
monthField = new JTextField(20);
yearField = new JTextField(20);
timeAdded = new JTextField(20);
hourLabel = new JLabel("Hour: ", JLabel.CENTER);
dayLabel = new JLabel("Day: ", JLabel.CENTER);
monthLabel = new JLabel("Month: ", JLabel.CENTER);
yearLabel = new JLabel("Year: ", JLabel.CENTER);
timeAdded1 = new JLabel("Added: ", JLabel.CENTER);
newTime = new JLabel(basic.output, JLabel.CENTER);
JPanel button = new JPanel(new GridLayout(0,1));
button.add(enter);
JPanel fieldPane = new JPanel(new GridLayout(0,1));
fieldPane.add(hourField);
fieldPane.add(dayField);
fieldPane.add(monthField);
fieldPane.add(yearField);
fieldPane.add(timeAdded);
JPanel newTimePanel = new JPanel(new FlowLayout());
newTimePanel.add(newTime);
JPanel labels = new JPanel(new GridLayout(0,1));
labels.add(hourLabel);
labels.add(dayLabel);
labels.add(monthLabel);
labels.add(yearLabel);
labels.add(timeAdded1);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(fieldPane, BorderLayout.LINE_END);
add(labels, BorderLayout.CENTER);
add(button, BorderLayout.PAGE_END);
}
private static void createAndShowGUI()
{
frame = new JFrame("Clock Adder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.add(new displayFrame());
frame.pack();
frame.setVisible(true);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
hrsIn = Integer.parseInt(hourField.getText());
daysIn = Integer.parseInt(dayField.getText());
monthsIn = Integer.parseInt(monthField.getText());
yearsIn = Integer.parseInt(yearField.getText());
timeAdd = Integer.parseInt(timeAdded.getText());
JOptionPane.showMessageDialog(frame, basic.output);
}
});
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run(){
createAndShowGUI();
}
}
);
}
}
Calculating class code:
package clock;
public class basic {
static int year = displayFrame.yearsIn;
static int month = displayFrame.monthsIn;
static int day = displayFrame.daysIn;
static int hour = displayFrame.hrsIn;
public static String output;
double yearD = year;
double leapYear = yearD/4;
int daysInMonth = 0;
int daysInYear = 365;
int casePick = 0;
int hoursAdded = 0;
int x = 0;{
//Making sure that the number added isn't negative, ones for other inputs MAY come...
while(x == 0)
{
if(Math.abs(hoursAdded) != hoursAdded){
continue;
}else{
x++;
}
/*Starts the counting loop and for each month value, it will determine how many days are in it
this will allow for exact measurement.*/
while(hoursAdded > 0){
switch(month)
{
case 1:
daysInMonth = 31;
break;
case 2:
if(leapYear % 1 == 0){
daysInYear = 366;
daysInMonth = 29;
}else{
daysInMonth = 28;
}
break;
case 3:
daysInMonth = 31;
break;
case 4:
daysInMonth = 30;
break;
case 5:
daysInMonth = 31;
break;
case 6:
daysInMonth = 30;
break;
case 7:
daysInMonth = 31;
break;
case 8:
daysInMonth = 31;
break;
case 9:
daysInMonth = 30;
break;
case 10:
daysInMonth = 31;
break;
case 11:
daysInMonth = 30;
break;
case 12:
daysInMonth = 31;
break;
}
/*Decides whether a year, month, day or hour is left, adds one to its counter,
and then takes away that amount of time from the hours added that are left*/
if(hoursAdded >= daysInYear * 24){
hoursAdded = hoursAdded - (daysInYear * 24);
year++;
break;
}
if(hoursAdded >= daysInMonth * 24 && hoursAdded < daysInYear * 24){
hoursAdded = hoursAdded - (daysInMonth * 24);
month++;
break;
}
if(hoursAdded >= 24 && hoursAdded < daysInMonth * 24){
hoursAdded = hoursAdded - 24;
day++;
break;
}
if(hoursAdded >= 1 && hoursAdded < 24){
hoursAdded = hoursAdded - 1;
hour++;
break;
}
//Makes sure there are never 25 hours, 32 days or 13 months
if(hour > 24){
day++;
hour = 1;
}
if(day > daysInMonth){
month++;
day = 1;
}
if(month > 12){
year++;
month = 1;
}
}
output = "hello";
/*hello is a test value, this will be the actual value:
("The date you have requested is: " + month + "/" + day + "/" + year + " Hour:" + hour);*/
}
}
}
Thanks for your help!
Upvotes: 0
Views: 96
Reputation: 347314
Create a method which takes your parameters and returns your result...
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
hrsIn = Integer.parseInt(hourField.getText());
daysIn = Integer.parseInt(dayField.getText());
monthsIn = Integer.parseInt(monthField.getText());
yearsIn = Integer.parseInt(yearField.getText());
timeAdd = Integer.parseInt(timeAdded.getText());
String result = Basic.addTimeTo(yearsIn, monthsIn, daysIn, hrsIn, timeAdd);
JOptionPane.showMessageDialog(frame, result);
}
});
Restructure your Basic
class to take parameters into a method and return the required value...
public class Basic {
public static String addTimeTo(int year, int month, int day, int hour, int timeToAdd) {
// Perform calculations...
// return result...
}
// static int year = displayFrame.yearsIn;
// static int month = displayFrame.monthsIn;
// static int day = displayFrame.daysIn;
// static int hour = displayFrame.hrsIn;
//
// public static String output;
//
// double yearD = year;
// double leapYear = yearD / 4;
// int daysInMonth = 0;
// int daysInYear = 365;
// int casePick = 0;
// int hoursAdded = 0;
// int x = 0;
//
// {
//
// //Making sure that the number added isn't negative, ones for other inputs MAY come...
// while (x == 0) {
// if (Math.abs(hoursAdded) != hoursAdded) {
// continue;
// } else {
// x++;
// }
//
// /*Starts the counting loop and for each month value, it will determine how many days are in it
// this will allow for exact measurement.*/
// while (hoursAdded > 0) {
// switch (month) {
// case 1:
// daysInMonth = 31;
// break;
// case 2:
// if (leapYear % 1 == 0) {
// daysInYear = 366;
// daysInMonth = 29;
// } else {
// daysInMonth = 28;
// }
// break;
// case 3:
// daysInMonth = 31;
// break;
// case 4:
// daysInMonth = 30;
// break;
// case 5:
// daysInMonth = 31;
// break;
// case 6:
// daysInMonth = 30;
// break;
// case 7:
// daysInMonth = 31;
// break;
// case 8:
// daysInMonth = 31;
// break;
// case 9:
// daysInMonth = 30;
// break;
// case 10:
// daysInMonth = 31;
// break;
// case 11:
// daysInMonth = 30;
// break;
// case 12:
// daysInMonth = 31;
// break;
// }
//
//
// /*Decides whether a year, month, day or hour is left, adds one to its counter,
// and then takes away that amount of time from the hours added that are left*/
// if (hoursAdded >= daysInYear * 24) {
// hoursAdded = hoursAdded - (daysInYear * 24);
// year++;
// break;
// }
//
// if (hoursAdded >= daysInMonth * 24 && hoursAdded < daysInYear * 24) {
// hoursAdded = hoursAdded - (daysInMonth * 24);
// month++;
// break;
// }
//
// if (hoursAdded >= 24 && hoursAdded < daysInMonth * 24) {
// hoursAdded = hoursAdded - 24;
// day++;
// break;
// }
//
// if (hoursAdded >= 1 && hoursAdded < 24) {
// hoursAdded = hoursAdded - 1;
// hour++;
// break;
// }
//
// //Makes sure there are never 25 hours, 32 days or 13 months
// if (hour > 24) {
// day++;
// hour = 1;
// }
//
// if (day > daysInMonth) {
// month++;
// day = 1;
// }
//
// if (month > 12) {
// year++;
// month = 1;
// }
// }
// output = "hello";
// /*hello is a test value, this will be the actual value:
// ("The date you have requested is: " + month + "/" + day + "/" + year + " Hour:" + hour);*/
// }
// }
}
You Basic
class and addTimeTo
should be completely decoupled from all other classes and information and be completely reliant on what it is passed (with the exception to information it needs to perform it's internal calculations, like daysInYear
for example)
Unless otherwise required, I would also encourage you to make use of Java 8's Time
API or JodaTime to perform your calculations
Upvotes: 1