如何从GUI的“输入”按钮中获取更多代码?

时间:2018-08-16 08:20:56

标签: java swing user-interface

当前,我在Enter按钮操作中从GUI收集了所有变量。有没有办法让我把所有这些东西从输入按钮中移到另一个类中以清理外观?

private void enterActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // saves all entered data to its variable
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy",Locale.ENGLISH);
    LocalDate today = LocalDate.now();

    String dobDay1 = String.valueOf(dobDay.getSelectedItem());
    String dobMonth1 = String.valueOf(dobMonth.getSelectedItem());
    String dobYear1 = String.valueOf(dobYear.getSelectedItem());
    String SdobDay1 = String.valueOf(SdobDay.getSelectedItem());
    String SdobMonth1 = String.valueOf(SdobMonth.getSelectedItem());
    String SdobYear1 = String.valueOf(SdobYear.getSelectedItem());
    String children1 = String.valueOf(children.getSelectedItem());
    String annDay1 = String.valueOf(annDay.getSelectedItem());
    String annMonth1 = String.valueOf(annMonth.getSelectedItem());
    String annYear1 = String.valueOf(annYear.getSelectedItem());
    String state1 = String.valueOf(state.getSelectedItem());
    String dState1 = String.valueOf(dState.getSelectedItem());
    String fName1 = fName.getText();
    String MI1 = MI.getText();
    String lName1 = lName.getText();
    String SfName1 = SfName.getText();
    String SMI1 = SMI.getText();
    String SlName1 = SlName.getText();
    String spouse = SdobMonth1 + "/" + SdobDay1 + "/" + SdobYear1;
    String city1 = city.getText();
    String pMessage0 = pMessage.getText();
    String you = dobMonth1 + "/" + dobDay1 + "/" + dobYear1;
    String ann = annMonth1 + "/" + annDay1 + "/" + annYear1;
    String selfGender = buttonGroup1.getSelection().getActionCommand();
    String spouseGender = buttonGroup2.getSelection().getActionCommand();
    String mType = buttonGroup3.getSelection().getActionCommand();

    Period duration;
    //constructors for birthstones, zodiacs and gender pronouns from respective classes
    birthstone birthstonex = new birthstone(dobMonth1);
    birthstone1 birthstoney = new birthstone1(SdobMonth1);
    zodiac zodiacx = new zodiac(dobMonth1,dobDay1);
    zodiac2 zodiacy = new zodiac2(SdobMonth1, SdobDay1);
    gender gender1 = new gender(selfGender);
    gender2 gender2 = new gender2(spouseGender);
    messageType message = new messageType(mType);
    //String m = String.valueOf(message);
    LocalDate dob = LocalDate.parse(you,dtf);
    LocalDate sdob = LocalDate.parse(spouse,dtf);
    if (dob.compareTo(sdob) > 0) {
        duration = Period.between(sdob, dob);
    }  else {
        duration = Period.between(dob, sdob);
    }

    LocalDate annDate = LocalDate.parse(ann,dtf);
    Period married = Period.between(annDate, today);

    String marriage = String.format("%d year(s), %d month(s), %d day(s) " , married.getYears(), married.getMonths(), married.getDays());
    String difference = String.format("%d year(s), %d month(s), %d day(s) " ,duration.getYears(), duration.getMonths(), duration.getDays());


    output.setText(String.valueOf(messageType.message)+pMessage0);

这确实可以正常工作,但是在enter按钮中似乎有很多代码,并且希望使其更简洁。我对编程还是很陌生,所有这些都是从一个严肃的蛮力型程序开始的,此后我一直在清理它。 我希望这里有足够的信息来寻求帮助。

0 个答案:

没有答案