小编use*_*565的帖子

从Eclipse运行时不出现Java JFrame窗口

一个非常简单的问题.我尝试运行一个非常简单的演示来创建和显示Eclipse的Window Frame,但没有任何反应.没有错误,没有窗口,代码运行完成.

我添加了断点并确保代码按预期运行.代码直接来自Java教程(FrameDemo),我只是将包重命名为适合放置它的位置(此包中的其他代码运行正常):

package ui;

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

/* FrameDemo.java requires no other files. */
public class FrameDemo {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("FrameDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel emptyLabel = new JLabel("");
        emptyLabel.setPreferredSize(new Dimension(175, 100));
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java eclipse macos swing jfreechart

11
推荐指数
1
解决办法
6426
查看次数

python pexpect清除或冲洗线

我试图在从未知状态恢复后清除该行上的任何字符,因为在某些情况下,它们包含我在将来期望方法调用中使用的提示和其他关键字.我已经尝试了多种混合结果的方法,因为我一直在遇到非预期的行为.

看到意外的行为(使用pexpect V3.3和Python 2.7.9):

  1. 执行以下代码后,当我随后尝试从缓冲区读取时,偶尔会看到不稳定的行为,其中只有部分累积的字符被清除.这对我的下游逻辑造成了严重破坏.我假设这是因为整个线程被置于休眠状态5秒,因此当它被唤醒时,它没有时间在执行read_nonblocking()命令之前获取完整的传入缓冲区.

    time.sleep(5)
    flushedStuff += child.read_nonblocking(100000, timeout=0)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 当我尝试使用.expect调用以非阻塞方式刷新行时,我发现在TIMEOUT异常之后,传入缓冲区未被清除.它的内容可以在child.before属性中按预期找到,但在下一个.expect方法调用期间也会被解析.所以这根本不冲洗线!我还注意到,read_nonblocking()不从本地缓冲区读取,而是直接通过操作系统从该行读取,因此它没有看到这一点.

    try:
        child.expect("ZzqQJjSh_Impossible_String", timeout = 5)
    except pexpect.TIMEOUT:
        pass
    flushedStuff = child.before
    
    Run Code Online (Sandbox Code Playgroud)

所以在这之后,我提供一种可靠的方法来刷新行的当前解决方案是扩展spawn类并添加一个执行以下操作的方法...它访问一个未记录的属性:

class ExtendedSpawn(pexpect.spawn):
    def flushBuffer(delay)
        try:
            # Greedily read in all the incoming characters
            self.expect("ZzqQJjSh_Impossible_String", timeout = delay)
        except pexpect.TIMEOUT:
            pass
        # Clear local input buffer inside the spawn Class
        self.buffer = self.string_type()
        return self.before
Run Code Online (Sandbox Code Playgroud)

上述方法也可用于非阻塞睡眠命令.

对于一些应该简单的方法来说,这似乎过于复杂,更不用说我浪费了几天.有没有更好的方法呢?

谢谢!

python pexpect

9
推荐指数
2
解决办法
8776
查看次数

标签 统计

eclipse ×1

java ×1

jfreechart ×1

macos ×1

pexpect ×1

python ×1

swing ×1