计算结果未显示在文本字段中

时间:2017-06-24 11:50:28

标签: java

为一个学校项目做一个小型申请,除了这件事之外我基本完成了。在我的第一个小组中,人们可以每天填写小时数。文本字段中的这些数字需要加起来,结果显示在第二个面板的文本字段中。

此处输入会在动作侦听器中添加:

        public class knopHandler implements ActionListener
        {
        public void actionPerformed ( ActionEvent e )
        {
        JFrame frame2 = new JFrame ( "Total Hours" );
        frame2.setSize ( 600, 500 );
        JPanel uitvoerpanel = new uitvoerpanel();
        frame2.setContentPane( uitvoerpanel );
        frame2.setVisible( true );


        String invoerstring1 = maandaginvoer.getText();
        int getal1 = Integer.parseInt( invoerstring1 );

        String invoerstring2 = dinsdaginvoer.getText();
        int getal2 = Integer.parseInt( invoerstring2 );

        String invoerstring3 = woensdaginvoer.getText();
        int getal3 = Integer.parseInt( invoerstring3 );

        String invoerstring4 = donderdaginvoer.getText();
        int getal4 = Integer.parseInt( invoerstring4 );

        String invoerstring5 = vrijdaginvoer.getText();
        int getal5 = Integer.parseInt( invoerstring5 );

        String invoerstring6 = zaterdaginvoer.getText();
        int getal6 = Integer.parseInt( invoerstring6 );

        String invoerstring7 = zondaginvoer.getText();
        int getal7 = Integer.parseInt( invoerstring7 );

        int resultaat = getal1 + getal2 + getal3 + getal4 + getal5 + getal6 
        + getal7;

现在int resultaat需要显示在新面板的totaalurenvak

   totaalurenvak = new JTextField ( 20 );
   totaalurenvak.setHorizontalAlignment ( JTextField.LEFT );
   totaalurenvak.setEditable ( false );
   totaalurenvak.setText(Integer.toString( resultaat) );

现在这是我尝试的最新代码行,我也尝试了totaalurenvak.setText("" + resultaat);totaalurenvak.setText(Integer.parseInt ( resultaat) );

可能值得注意的是,来自其他两个文本字段的输入确实显示在第二个面板中,只有这样才不会出现这样的错误吗?无论我尝试编译器的任何代码行告诉我"找不到符号"。

3 个答案:

答案 0 :(得分:1)

问题是:您没有将uitvoerpanel添加到JTextField totaalurenvak = new JTextField(20); ..... uitvoerpanel.add(totaalurenvak);

做的:

totaalurenvak.setText(Integer.toString( resultaat) );

然后在事件结束时

public abstract class WifiPrinterConnection extends AsyncTask<Void, Void, Void>  {
    private byte[] printData;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         //place your progress dialog
    }


    @Override
    protected Void doInBackground(final Void... params) {



        try {
            final Socket socket = new Socket();
            socket.connect(new InetSocketAddress(InetAddress.getByName(((WifiPrintingEntity)this.connectionEntity).getIp()), ((WifiPrintingEntity)this.connectionEntity).getPort()),
                    ((WifiPrintingEntity)this.connectionEntity).getTimeout());
            LOGGER.info(PrinterLoggerMessage.WIFI_PRINTER_CONNECTION_SUCCESS);
            final OutputStream outputStream = socket.getOutputStream();

            final byte[] data = this.printData;
            LOGGER.info(PrinterLoggerMessage.PRINTING_STARTED);
            outputStream.write(data, 0, data.length);

            outputStream.close();
            socket.close();
            entity.setSuccessful(true);
            LOGGER.info(PrinterLoggerMessage.SOCKET_CLOSE);


        } catch (final IOException e) {

            LOGGER.error(e.getMessage(),e);
        } catch (final Exception ee) {


        }

        return null;
    }


    @Override
    public void connection(final PrintingEntity connectionEntity, final IConnectionResponse listner,final byte[] data) {
        this.connectionEntity = connectionEntity;
        this.printData = data;
        this.response = listner;
        this.execute();
    }

}

答案 1 :(得分:1)

您可以将totaalurenvak的创建保留在代码的不同部分的init函数中。但是,在您的事件处理程序中,您必须为新JTextField

设置文本
int resultaat = getal1 + getal2 + getal3 + getal4 + getal5 + getal6 + getal7;
totaalurenvak.setText(Integer.toString(resultaat));

这是因为您尝试更改其中一个类变量的值。您必须在该变量上显式调用函数setText(value)

顺便说一下,让totaalurenvak成为JLabel而不是无法编辑的JTextField更清晰,因为这在语义上更正确。

答案 2 :(得分:0)

我在奥拉夫舒斯勒非常有见地的帮助下弄明白了。

原来JPanel uitvoerpanel = new uitvoerpanel();应为uitvoerpanel uitvoerpanel = uitvoerpanel();