Java-重绘jframe时,如何以编程方式模拟StdIn

时间:2018-07-11 06:53:18

标签: java jframe jpanel stdin repaint

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

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

我尝试了以下解决方案 How can I write to stdIn (JAVA)JUnit testing with simulated user input 但它们都依赖于 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的外观(右图): enter image description here 来自标准输入的文本(左图)具有动画效果。 即使两个文本都由相同的代码处理,文本框中的文本也不会动画。

0 个答案:

没有答案