简体   繁体   中英

How to update and refresh JTextField in Java?

JTextField BalanceTextField = new JTextField("Balance:" + cointoss.GetBalance());
BalanceTextField.setBounds(500,170,100,15);
BalanceTextField.setBorder(null);
BalanceTextField.setOpaque(false);
BalanceTextField.setVisible(true);
CoinTossLabel.add(BalanceTextField);

CoinTossPlayButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent CoinTossPlayButtonClicked){
                    System.out.print('\u000C'); 
                    Balance = cointoss.GetBalance();
                    WagerText = WagerTextField.getText();
                    Wager = Integer.valueOf(WagerText);
                    if(Balance >= 0 +  Wager){
                        cointoss.GetCoinResult(CoinTossGuess,Wager);
                        CoinTossButtonHeads.setVisible(true);
                        CoinTossButtonTails.setVisible(true);
                    } else{
                        System.out.println("Not Enough Funds");
                      }
                      BalanceTextField.revalidate();
                }
            });

This is the code for the text field and the action listener for the button I am using as part of my program. I tried to re-validate,invalidate, update and validate the text field but it wouldn't update on the screen?

您可以再次重置JTextField值,只需使用

BalanceTextField.setText("Balance:" + cointoss.GetBalance());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM