gpt4 book ai didi

java - 需要帮助传递变量和标记 JPanel

转载 作者:行者123 更新时间:2023-11-30 09:24:57 24 4
gpt4 key购买 nike

我已经研究了一段时间了,现在越来越困惑了。我想完成几件事:

  1. 向 JTextField 1 添加 2 个标签。第一个要求他们输入电子邮件。第二个表示已经为他们输入了@address.com。

  2. 我对如何传递 String 变量感到困惑,也许我应该只有一种方法?

    import javax.swing.*;
    import java.awt.*;

    public class Email {

    public static void main(String[] args) { //main method

    //get user input
    JPanel panel = new JPanel(); //create new JPanel

    JTextField field1 = new JTextField(20); //create a field for email and show 20 characters
    panel.add(field1);

    JTextField field2 = new JTextField(6); //create a field for grade and show to 6 characters
    panel.add(field2);

    int iValue = JOptionPane.showConfirmDialog (
    null,
    panel,
    "Please enter students email adress and test grade",
    JOptionPane.OK_OPTION); //create button in panel

    if (iValue == JOptionPane.OK_OPTION) {

    //capture email address as String
    String sEmail = field1.getText() + "@address.com";

    //capture numerical grade input and convert it to a float
    float fGrade = Float.parseFloat(field2.getText());
    }
    } //end main


    public void convertGrades(float fGrade) {

    /***"shortcut if" grade conversion***********
    String sLetterGrade = (fGrade >= 97) ? "A+" :
    (fGrade >= 94) ? "A" :
    (fGrade >= 90) ? "A-" :
    (fGrade >= 87) ? "B+" :
    (fGrade >= 84) ? "B" :
    (fGrade >= 80) ? "B-" :
    (fGrade >= 77) ? "C+" :
    (fGrade >= 74) ? "C" :
    (fGrade >= 70) ? "C-" :
    (fGrade >= 67) ? "D+" :
    (fGrade >= 64) ? "D" :
    (fGrade >= 60) ? "D-" : "F";
    *************************************************************************/


    //experimental grade conversion
    // Determine letter grade
    String sAlphaGrade = (fGrade >= 90) ? "A" :
    (fGrade >= 80) ? "B" :
    (fGrade >= 70) ? "C" :
    (fGrade >= 60) ? "D" : "F";
    //determine + or -
    if (fGrade >= 60) {
    float fGradeModifier = (fGrade % 10);
    String sAlphaModifier = (fGradeModifier >= 7) ? "+" :
    (fGradeModifier >= 4)? "" : "-";

    //concatenate letter grade and modifier
    String sLetterGrade = sAlphaGrade + sAlphaModifier;

    } // end if


    String sResults = getInput.sEmail + "\n" +
    sMessage + "\n" +
    "your test score is: " + fGrade + "\n" +
    "your letter grade is: " + sLetterGrade;


    JOptionPane.showMessageDialog(null, sOutput,"Email and Grade",
    JOptionPane.INFORMATION_MESSAGE);

    } //end convertGrades
    } //end class Email

最佳答案

您的代码在声明和访问变量的位置存在一些问题。 sMessagesOutput 从未在任何地方声明或赋值。此外,您尝试在 convertGrades 方法中访问 sEmail 当它已在 main 中声明时,并尝试访问 sLetterGradeif block 之外声明它。

如果您想在convertGrades 方法中使用sEmail,您需要在调用它时将其传递给该方法。 sLetterGrade 必须在 if block 之外声明,以便稍后使用。此外,您实际上从未从 main 方法中调用 convertGrades,因此这些代码都不会执行。

要标记您的 JTextField,我建议将所有标签和字段放在 Box 中.这将确保他们都以正确的顺序待在一起。

这里是您编辑的代码以包含所有这些更改:

    import javax.swing.*;
import java.awt.*;

public class Email {

public static void main(String[] args) { //main method

//get user input
JPanel panel = new JPanel(); //create new JPanel


Box field1Box = Box.createHorizontalBox(); // Box to hold field1 and its labels
field1Box.add(new JLabel("Enter email: ")); // JLabels can be added directly; we don't need to keep a reference
JTextField field1 = new JTextField(20); //create a field for email and show 20 characters
field1Box.add(field1);
field1Box.add(new JLabel("@address.com"));
panel.add(field1Box); // Adds the Box containing field1 and the two labels to panel


JTextField field2 = new JTextField(6); //create a field for grade and show to 6 characters
panel.add(field2);

int iValue = JOptionPane.showConfirmDialog (
null,
panel,
"Please enter student's email adress and test grade",
JOptionPane.OK_OPTION); //create button in panel

if (iValue == JOptionPane.OK_OPTION) {

//capture email address as String
String sEmail = field1.getText() + "@address.com";

//capture numerical grade input and convert it to a float
float fGrade = Float.parseFloat(field2.getText());

//call convertGrades to display the results
convertGrades(fGrade, sEmail);
}
} //end main


// convertGrades must be static to be called from main
public static void convertGrades(float fGrade, String sEmail) {

/***"shortcut if" grade conversion***********
String sLetterGrade = (fGrade >= 97) ? "A+" :
(fGrade >= 94) ? "A" :
(fGrade >= 90) ? "A-" :
(fGrade >= 87) ? "B+" :
(fGrade >= 84) ? "B" :
(fGrade >= 80) ? "B-" :
(fGrade >= 77) ? "C+" :
(fGrade >= 74) ? "C" :
(fGrade >= 70) ? "C-" :
(fGrade >= 67) ? "D+" :
(fGrade >= 64) ? "D" :
(fGrade >= 60) ? "D-" : "F";
*************************************************************************/


//experimental grade conversion
// Determine letter grade
String sAlphaGrade = (fGrade >= 90) ? "A" :
(fGrade >= 80) ? "B" :
(fGrade >= 70) ? "C" :
(fGrade >= 60) ? "D" : "F";
String sLetterGrade; // This needs to be visible outside of the if
//determine + or -
if (fGrade >= 60) {
float fGradeModifier = (fGrade % 10);
String sAlphaModifier = (fGradeModifier >= 7) ? "+" :
(fGradeModifier >= 4)? "" : "-";

//concatenate letter grade and modifier
sLetterGrade = sAlphaGrade + sAlphaModifier;

} // end if
else {
// sLetterGrade still needs to get a value
sLetterGrade = sAlphaGrade;
}


String sResults = sEmail + "\n" +
// sMessage has been commented out so the program will compile and run
// Uncomment it if you declare it and give it a value somewhere.
// sMessage + "\n" +
"your test score is: " + fGrade + "\n" +
"your letter grade is: " + sLetterGrade;


// Assuming sOutput was meant to mean sResults
JOptionPane.showMessageDialog(null, sResults,"Email and Grade",
JOptionPane.INFORMATION_MESSAGE);

} //end convertGrades
} //end class Email

关于java - 需要帮助传递变量和标记 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15467830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com