java—在jtextfield中插入值后,不会显示它们的随机值我该怎么修?

esbemjvw  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(328)

我编写了这个java应用程序,用于在启动计数器后从jtextfields中输入的值中提取一个随机值。编译并没有错误,但是( "" +randomElement )不显示值。
我创造了 randomElement 内部的属性 TimerListener 班级 actionPerformed() 启动计数器的方法。在计数结束时,必须显示从四个jtextfields中获取的随机值(如下面的代码所示)。
你能告诉我哪里错了,我该怎么解决吗?

public class Merge extends JPanel {
    public static final int TIMER_DELAY = 1000;
    public static final String TEST_TEXT = "123456";
    public JTextField textField = new JTextField(16);
    public JButton button = new JButton(new ButtonAction());
    public Timer timer;
    JTextField t1Text = new JTextField();
    JTextField t2Text = new JTextField();
    JTextField t3Text = new JTextField();
    JTextField t4Text = new JTextField();
    JLabel f1Label = new JLabel("Filippo film 1");
    JLabel f2Label = new JLabel("Filippo film 2");
    JLabel l1Label = new JLabel("Laura film 1");
    JLabel l2Label = new JLabel("Laura film 2");

    public Merge() {
        add(button);
        add(textField);
    }

    public static void main(String[] args) {
        Merge me = new Merge();
        me.createAndShowGui();
    }

    public void createAndShowGui() {
        Merge mainPanel = new Merge();
        JFrame frame = new JFrame("Film roulette");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.setLayout(new GridLayout(11, 4));
        frame.pack();
        frame.setBounds(100, 10, 400, 400);
        frame.add(f1Label);
        frame.add(t1Text);
        frame.add(f2Label);
        frame.add(t2Text);
        frame.add(l1Label);
        frame.add(t3Text);
        frame.add(l2Label);
        frame.add(t4Text);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class ButtonAction extends AbstractAction {

        public ButtonAction() {
            super("The movie you will see...");
            putValue(MNEMONIC_KEY, KeyEvent.VK_P);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            if (timer != null && timer.isRunning()) {
                return;
            }
            textField.setText("");
            timer = new Timer(TIMER_DELAY, new TimerListener());
            timer.start();
        }
    }

    private class TimerListener implements ActionListener {
        private String text = "";
        private int counter = 0;

        @Override
        public void actionPerformed(ActionEvent e) {

            String filmFilippo1, filmFilippo2, filmLaura1, filmLaura2;

            filmFilippo1 = t1Text.getText();
            filmFilippo2 = t2Text.getText();
            filmLaura1 = t3Text.getText();
            filmLaura2 = t4Text.getText();

            List<String> givenList = Arrays.asList(filmFilippo1, filmFilippo2, filmLaura1, filmLaura2);
            Random rand = new Random();
            String randomElement = givenList.get(rand.nextInt(givenList.size()));

            text += TEST_TEXT.charAt(counter);
            textField.setText(text);
            counter++;
            if (counter >= TEST_TEXT.length()) {
                timer.stop();
                textField.setText("" +randomElement);
            }
        }
    }
}
i86rm4rw

i86rm4rw1#

要回答你的实际问题,它是空的原因是因为你这样做 createAndShowGui :

Merge mainPanel = new Merge();
// ...
frame.getContentPane().add(mainPanel);

您可以创建 Merge 类,然后将其添加到 JFrame 你已经做过了 main(string[] args) :

public static void main(String[] args) {
    Merge me = new Merge();
    me.createAndShowGui();
}

现在有两个 Merge 类,其中一个对用户不可见。你只需要在家里做这件事 createAndShowGui :

frame.getContentPane().add(this);

然而
不要延伸 JPanel 不必要的
通过在edt上创建所有swing组件 SwingUtilities.invokeLater 使用 JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 所以它完全退出应用程序,除非你只想处理?
呼叫 JFrame#pack() 仅在添加所有组件之后
不要打电话 setBounds 你自己你用的是 LayoutManager 就这样吧!
不用打电话了 JFrame#getContentPane()#add 打个电话就行了 JFrame#add 你为计时器的每一个滴答声选择一个随机数,为什么不在计时器结束时选择一次呢?
您可能还想禁用 textField 这算得上人们可以把输入放在那里,弄乱你的代码逻辑哈哈
不要全局声明或初始化变量,除非在不同的方法中需要它们

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Merge {

    public static final int TIMER_DELAY = 1000;
    public static final String TEST_TEXT = "12345";
    private int counter = 1;
    private Random rand = new Random();
    private Timer timer;

    public Merge() {
        createAndShowGui();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Merge::new);
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame("Film roulette");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // create components
        JTextField textField = new JTextField(16);
        JButton button = new JButton("The movie you will see...");
        button.setMnemonic(KeyEvent.VK_P);
        JPanel mergePanel = new JPanel();
        JTextField t1Text = new JTextField();
        JTextField t2Text = new JTextField();
        JTextField t3Text = new JTextField();
        JTextField t4Text = new JTextField();
        JLabel f1Label = new JLabel("Filippo film 1");
        JLabel f2Label = new JLabel("Filippo film 2");
        JLabel l1Label = new JLabel("Laura film 1");
        JLabel l2Label = new JLabel("Laura film 2");

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (timer != null && timer.isRunning()) {
                    return;
                }

                // gather user input
                List<String> givenList = Arrays.asList(t1Text.getText(), t2Text.getText(), t3Text.getText(), t4Text.getText());

                // reset fields (if button is clicked more then once)
                textField.setText("");
                counter = 0;

                // start a timer
                timer = new Timer(TIMER_DELAY, (ActionEvent ev) -> {
                    if (counter >= TEST_TEXT.length()) {
                        ((Timer) ev.getSource()).stop();
                        String randomElement = givenList.get(rand.nextInt(givenList.size()));
                        textField.setText(randomElement);
                    } else {
                        textField.setText(textField.getText() + TEST_TEXT.charAt(counter));
                        counter++;
                    }
                });

                timer.start();
            }
        });

        // layout components
        mergePanel.add(button);
        mergePanel.add(textField);
        frame.add(mergePanel);
        frame.setLayout(new GridLayout(11, 4));
        frame.add(f1Label);
        frame.add(t1Text);
        frame.add(f2Label);
        frame.add(t2Text);
        frame.add(l1Label);
        frame.add(t3Text);
        frame.add(l2Label);
        frame.add(t4Text);
        frame.setLocationByPlatform(true);

        frame.pack();
        frame.setVisible(true);
    }
}

相关问题