有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法将JTextField输入转换为整数

我正在尝试创建一个程序,该程序可以请求输入,并自动从JtextFields面板获取输入的总和,但输入后我总是得到0值。如果你想看看我的全部产品,请看下面。提前谢谢

{
JPanel myPanel = new JPanel();   
JTextField SGEAC1 = new JTextField("0",10);
String sgeac1tx = SGEAC1.getText();
int eac1sg = Integer.parseInt(sgeac1tx);

JTextField SGEAC2 = new JTextField("0",10);
String sgeac2tx = SGEAC2.getText();
int eac2sg = Integer.parseInt(sgeac2tx);

    myPanel.add(new JLabel("SG-EAC1:"));
    myPanel.add(SGEAC1);
    myPanel.add(Box.createHorizontalStrut(15));

    myPanel.add(new JLabel("SG-EAC2:"));
    myPanel.add(SGEAC2);
    myPanel.add(Box.createHorizontalStrut(15));
    int IPLBW = JOptionPane.showConfirmDialog(null, myPanel, "BW", 
    JOptionPane.OK_CANCEL_OPTION);
    int sumbw = eac1sg+eac2sg; }

 if (IPLBW == JOptionPane.OK_OPTION) {
  JTextArea ta = new JTextArea(100, 100);
       ta.setText("INTERNATIONAL UPSTREAM LINK UPDATE"+"\n" + sumbw 
         + "\n"+"-----------------------------"
         + "\n"+"TOTALS SG BW:   "+ sumbw +"/"
         + "\n"+"-----------------------------"
         + "\n" + "SG via EAC1"+"            " + SGEAC1.getText()+"G"
         + "\n" + "SG via EAC2"+"            " + SGEAC2.getText()+"G");

 JOptionPane.showMessageDialog(null, new JScrollPane(ta), "RESULT", 
 JOptionPane.INFORMATION_MESSAGE);
    ta.setWrapStyleWord(true);
    ta.setLineWrap(true);
    ta.setCaretPosition(0);
    ta.setEditable(false);
]

共 (0) 个答案