如何在 JTextField 中放入随机文本?

发布于 2024-12-05 02:59:44 字数 830 浏览 0 评论 0原文

我想在文本字段内显示随机播放的单词。 到目前为止,这是我的随机代码:

    public MyTextTwist(String w){
        if (w != null){
            word = getRandomWord();
                txtWord.setText(word);}

        GameOver = false;
    }

       private String getRandomWord(){
                ArrayList<Character> chars = new ArrayList<Character>(txtWord.getText().length());
                for ( char c : word.toCharArray() ) {
                    chars.add(c);
                }
                Collections.shuffle(chars);
                char[] shuffled = new char[chars.size()];
                for ( int i = 0; i < shuffled.length; i++ ) {
                    shuffled[i] = chars.get(i);

                }String shuffledWord = new String(shuffled);
                return shuffledWord;
        }

它没有显示。

I want to show the shuffle word inside the Textfield.
so far this is my random code:

    public MyTextTwist(String w){
        if (w != null){
            word = getRandomWord();
                txtWord.setText(word);}

        GameOver = false;
    }

       private String getRandomWord(){
                ArrayList<Character> chars = new ArrayList<Character>(txtWord.getText().length());
                for ( char c : word.toCharArray() ) {
                    chars.add(c);
                }
                Collections.shuffle(chars);
                char[] shuffled = new char[chars.size()];
                for ( int i = 0; i < shuffled.length; i++ ) {
                    shuffled[i] = chars.get(i);

                }String shuffledWord = new String(shuffled);
                return shuffledWord;
        }

It Doesn't show.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-12-12 02:59:44

同样,如果要将文本放回 JTextField,代码必须通过在 JTextField 上调用 setText(...) 来完成此操作,但不能在构造函数中执行此操作,因为调用构造函数时,用户还没有机会在 JTextField 中输入任何文本。相反,您必须在事件响应中调用此方法,也许在已添加到 JButton 的 ActionListener 中:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.*;

public class TestWordScramble extends JPanel {
   private JTextField txtWord = new JTextField(10);
   private JButton scrambleBtn = new JButton("Scramble");

   public TestWordScramble() {
      scrambleBtn.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            String word = getRandomWord(txtWord.getText());
            txtWord.setText(word);
         }
      });

      add(txtWord);
      add(scrambleBtn);
   }

   private String getRandomWord(String text) {
      ArrayList<Character> chars = new ArrayList<Character>();
      for (char c : text.toCharArray()) {
         chars.add(c);
      }
      Collections.shuffle(chars);
      char[] shuffled = new char[chars.size()];
      for (int i = 0; i < shuffled.length; i++) {
         shuffled[i] = chars.get(i);

      }
      String shuffledWord = new String(shuffled);
      return shuffledWord;
   }

   private static void createAndShowGui() {
      TestWordScramble mainPanel = new TestWordScramble();

      JFrame frame = new JFrame("TestWordScramble");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Again, if you want to put the text back into the JTextField, the code must do this by calling setText(...) on the JTextField, but you can't do this in the constructor since when the constructor has been called, the user hasn't had a chance to enter any text into the JTextField. Instead you must call this method in the response to an event, perhaps in an ActionListener that has been added to a JButton:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.*;

public class TestWordScramble extends JPanel {
   private JTextField txtWord = new JTextField(10);
   private JButton scrambleBtn = new JButton("Scramble");

   public TestWordScramble() {
      scrambleBtn.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            String word = getRandomWord(txtWord.getText());
            txtWord.setText(word);
         }
      });

      add(txtWord);
      add(scrambleBtn);
   }

   private String getRandomWord(String text) {
      ArrayList<Character> chars = new ArrayList<Character>();
      for (char c : text.toCharArray()) {
         chars.add(c);
      }
      Collections.shuffle(chars);
      char[] shuffled = new char[chars.size()];
      for (int i = 0; i < shuffled.length; i++) {
         shuffled[i] = chars.get(i);

      }
      String shuffledWord = new String(shuffled);
      return shuffledWord;
   }

   private static void createAndShowGui() {
      TestWordScramble mainPanel = new TestWordScramble();

      JFrame frame = new JFrame("TestWordScramble");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文