简体   繁体   中英

How can I make my code work in a GUI using Swing in Java?

我的GUI的想法 I'm making a tool that does multiple things, and I'm onto the last feature for now: You type in a date and it tells you what day of the week it would be on that date.

So I have two problems here:

  1. I can't use arguments, it doesn't like them
  2. On the last line, I can't use jTextArea2.setText(d0); because it doesn't like that either...

My code is here:

    public static void main(String[] args) { 
        int d = Integer.parseInt(args[0]);
        int m = Integer.parseInt(args[1]);
        int y = Integer.parseInt(args[2]);

        int y0 = y - (14 - m) / 12;
        int x = y0 + y0/4 - y0/100 + y0/400;
        int m0 = m + 12 * ((14 - m) / 12) - 2;
        int d0 = (d + x + (31*m0)/12) % 7;

        System.out.println("Sunday = 0\nMonday = 1\nTuesday = 2\nWednesday = 3\nThursday = 4\nFriday = 5\nSaturday = 6\nThis date is on a:");
        System.out.println(d0);

Basically, this code was at first for use with the console, and now I want to implement it into a Swing GUI app.

I'm only a week into learning Java, so excuse me if the problem is obvious or easy to fix... But can anyone figure out how to work it? Thanks!

The question you have at your feet, is in what format will the date come it.

You've only supplied a JTextArea for them to enter the value in, so they can enter just about anything...

So, first things first, you need a method by which you can accept an incoming value...

public String toDayOfWeek(String date) {
}

From here you need to format the incoming value to a Date

String dayOfWeek = null;
try {
    Date date = DateFormat.getDateInstance().parse(date);
} catch (ParseException exp) {
    dayOfWeek = date + " is an invalid date format";
}
return dayOfWeek;

(Obviously, the above belongs in the toDayOfWeek(String) method)

Now, personally, I would pass the Date value onto another method, but that cause I'm crazy...

public String toDayOfWeek(Date date) {
    // Now you could extract the various values from the Date object
    // but those methods are deprecated...
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    int day = cal.get(Calendar.DATE);
    int month = cal.get(Calendar.MONTH); // Months are 0 based
    int year = cal.get(Calendar.YEAR);

    // Your calculation here...

    return yourDayCalculation;
}

But to be honest, it would simpler and easier to do this...

public String toDayOfWeek(Date date) {
    // Now you could extract the various values from the Date object
    // but those methods are deprecated...
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    return DateFormatSymbols.getInstance().getWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
}

So you would end up with two methods...

public String toDayOfWeek(String date) {
    String dayOfWeek = null;
    try {
        Date date = DateFormat.getDateInstance().parse(date);
        dayOfWeek = toDayOfWeek(date);
    } catch (ParseException exp) {
        dayOfWeek = date + " is an invalid date format";
    }
    return dayOfWeek;
}

public String toDayOfWeek(Date date) {
    // Now you could extract the various values from the Date object
    // but those methods are deprecated...
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    return DateFormatSymbols.getInstance().getWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
}

You can use Calendar for it. Look at this code and you'll get the idea

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.MONTH,0);//Note that months start at 0 i.e. January is 0
cal.set(Calendar.DAY,1);

System.out.println(cal.get(Calendar.DAY_OF_WEEK)));

First check if there are actually 3 elements in the args by

if(args.length == 3)
       // colculate
     else
       System.out.println("Usage: java DateID <month> <day> <year>"):

Assuming your GUI is not being displayed for the sake of example...

For your second problem, JTextArea inherits from JTextComponent, where you will find the setText method . Since it accepts strings and not ints, you're probably looking to do something like:

String.valueOf(d0);

I believe you could also concatenate d0 to a string literal -- such as the one in your println -- and be just as well.

Edit: Remember that setText will merely set the value of that field. If you want to get the text, you'll need to use the getText method and split the resulting string on whatever delimiters you specify. The result will be stored in an array of strings, which you could then convert to ints and use with Calendar ala Abu's response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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