代码之家  ›  专栏  ›  技术社区  ›  john k

Java——在重新绘制jframe时,如何以编程方式模拟StdIn

  •  0
  • john k  · 技术社区  · 8 年前

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

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

    我已经试过了 How can I write to stdIn (JAVA) 和 JUnit testing with simulated user input 但他们都依赖 系统塞廷 这对我不管用。

    下面是显示问题的最简单代码。通过标准输入输入输入命令,您将看到它们的动画效果。通过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 回复  |  直到 8 年前