不确定如何使用我在另一个类中创建的字符串作为我制作的JMessage框的正文文本。欢迎任何其他建议。仅供参考我想做的是获取一个消息框,其输出在另一个类中定义,以便在按下按钮时显示,该框将显示,但不是当我将其设置为来自其他类的字符串时。当我在另一个类中按下按钮并将值从我的JFrame类推送到处理我所有计算内容的类时,我也确定了这个值。这是代码:
JFrame类代码:
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();
}
}
);
}
}
计算班级代码:
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);*/
}
}
}
感谢您的帮助!
答案 0 :(得分:1)
创建一个获取参数并返回结果的方法......
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);
}
});
重组您的Basic
类,将参数带入方法并返回所需的值......
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);*/
// }
// }
}
您Basic
类和addTimeTo
应与所有其他类和信息完全分离,并完全依赖于传递的内容(除了执行它所需的信息之外)内部计算,例如daysInYear
)
除非另有要求,否则我还建议您使用Java 8的Time
API或JodaTime来执行计算