简体   繁体   English

如何从另一个类的文本添加到JMessage框?

[英]How to add text from another class to a JMessage box?

Not sure how to use a string I made in another class as the body text for a JMessage box I made. 不知道如何使用我在另一个类中创建的字符串作为我创建的JMessage框的正文。 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. 当在另一个类中按下按钮时,我还将确定该值,并将值从我的JFrame类推到正在处理所有计算内容的类中。 Here's the code: 这是代码:

JFrame class code: 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();
            }
        }
    );
}   
}

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! 谢谢你的帮助!

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... 重组您的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);*/
//      }
//  }
}

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) 您的Basic类和addTimeTo应该与所有其他类和信息完全分离,并完全依赖于传递的信息(除了信息需要执行其内部计算外,例如daysInYear

Unless otherwise required, I would also encourage you to make use of Java 8's Time API or JodaTime to perform your calculations 除非另有要求,否则我也鼓励您使用Java 8的Time API或JodaTime进行计算

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM