无法将JTextField输入转换为Integer

时间:2018-08-30 18:52:03

标签: 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 个答案:

没有答案