首页 > 解决方案 > Java - 重新绘制jframe时如何以编程方式模拟StdIn

问题描述

客户已为其当前的命令行驱动程序请求图形界面。(程序在一个单独的窗口中为流程图制作动画。)这些命令激活了重新绘制流程图窗口的循环。

我遇到的问题是,当使用我的 GUI 时,流程图没有动画。repaint() 不是以相同的方式重新绘制。

我已经尝试了 如何使用模拟用户输入写入 stdIn (JAVA)JUnit 测试中的解决方案, 但它们都依赖于对我不起作用的System.setIn(in) 。

这是显示问题的最小代码。通过标准输入输入命令,您将看到它们动画。通过 GUI 做同样的事情并且没有动画。两种输入法都运行相同的命令,drawFakeData - 那么为什么它们会产生不同的结果呢?

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class FlowChart {

    public GUImin gui;
    private static int i;

    public static void main(String[] args) {
        FlowChart sdm = new FlowChart();
        sdm.runInterpreter();
    }

    public FlowChart() {
        EventQueue.invokeLater(() -> {
            gui = new GUImin();
        });
    }

    public void runInterpreter() {

        Scanner takeCommand = new Scanner(System.in);
        String userCommandLine;
        do {

            //capture all Standard Input from console
            userCommandLine = takeCommand.nextLine();
            processInput(userCommandLine);


        } while (!userCommandLine.equals("quit"));
        takeCommand.close();
    }

    public void processInput(String input) {

        for (i = 0; i < 10; i++) {  //emulate an animation loop

            EventQueue.invokeLater(() -> {
                gui.drawFakeData(input + i);
            });
            // pause for a given number of seconds
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    class GUIpanelmin extends JPanel {
        public String input = "^";
        public int x;
        public int y;

        public GUIpanelmin() {
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);  //no idea what this does - draws background I think?
            System.out.println("GUI: " + "Paint Component Reached.");
            Graphics2D g2d = (Graphics2D) g;  //or this
            g2d.drawString(input, x, y);  //just an example
        }

    }

    public class GUImin extends JFrame {
        public GUIpanelmin GUIpanel;

        public GUImin() {
            super("GUI");
            super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            super.setVisible(true);
            setSize(350, 250);  //TBD
            setLocationRelativeTo(null);  //centers the panel
            this.getContentPane().setLayout(new FlowLayout());


            //input text box
            JTextField inputField = new JTextField(10);
            inputField.setPreferredSize(new Dimension(200, 30));
            add(inputField);

            //the button
            JButton submitButton = new JButton("Submit Command");
            submitButton.setPreferredSize(new Dimension(40, 30));
            submitButton.addActionListener(e -> {
                for (int i = 0; i < 10; i++) {  //emulate animation loop
                    drawFakeData(inputField.getText());
                    // pause for a given number of seconds
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }
                inputField.setText("");
            });
            add(submitButton);

            //the panel with all the pretties
            GUIpanel = new GUIpanelmin();  //there's only one
            GUIpanel.setPreferredSize(new Dimension(300, 300));
            add(GUIpanel);
        }

        public void drawFakeData(String command) {
            //just an example - real thing will be much more complicated
            GUIpanel.x = (int) (Math.random() * 100 + 5);
            GUIpanel.y = (int) (Math.random() * 100 + 5);
            GUIpanel.input = command;
            GUIpanel.repaint();
        }
    }
}

感谢您解释这种令人费解的行为的任何帮助。

编辑:这是 GUI 的样子(右图): 在此处输入图像描述 来自标准输入的文本(左图)动画。文本框中的文本不会动画,即使两个文本都由相同的代码处理。

标签: javajframejpanelstdinrepaint

解决方案


推荐阅读