gpt4 book ai didi

java - JFrame 在 TextPane 设置之前退出

转载 作者:行者123 更新时间:2023-12-02 03:45:44 25 4
gpt4 key购买 nike

这里我有两个类:

测试类:

public class test {
private static JPanel contentPane;
private static JFrame frame = new JFrame("Sleeping");
private static JTextPane textPane;
private static StyledDocument doc;
private static Thread sleep;
private static HandleMouse S = new HandleMouse();

public static void main(String[] args) {

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setBounds(0, 0, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
frame.setContentPane(contentPane);
frame.setLocationRelativeTo(null);

JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BorderLayout(0, 0));

textPane = new JTextPane();
panel.add(textPane, BorderLayout.CENTER);

doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(doc.getLength() + 1, 1, center, false);

textPane.setEditable(false);
sleep = new Thread(() -> {
try {
textPane.setBackground(new Color(59, 82, 217));
Thread.sleep(1200);
doc.insertString(doc.getLength(), "Waiting for sleep, I drift from thoughts like these;\n", null);
Thread.sleep(1200);
doc.insertString(doc.getLength(), "And where to-day was dream-like, build my dreams.\n", null);
Thread.sleep(1200);
doc.insertString(doc.getLength(), "Across my brain, ghost of remembered chords\n ", null);
Thread.sleep(1200);
doc.insertString(doc.getLength(), "Which still can make such radiance in my dream.\n", null);
Thread.sleep(1200);
doc.insertString(doc.getLength(), "And count their faces; faces; sunlit faces.\n", null);
Thread.sleep(1200);
doc.insertString(doc.getLength(), "Falling asleep ... the herons, and the hounds....\n", null);
Thread.sleep(1200);
doc.insertString(doc.getLength(), "September in the darkness; and the world I've known\n", null);
Thread.sleep(1200);
doc.insertString(doc.getLength(), "all fading past me into peace. ", null);

} catch (BadLocationException | InterruptedException e) {

}
});

frame.setResizable(false);
frame.setVisible(true);
sleep.start();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
textPane.addMouseMotionListener(S);
frame.addMouseMotionListener(S);

}

}

以及测试类中处理鼠标运动的另一个内部类:

public static class HandleMouse implements MouseMotionListener {

@Override
public synchronized void mouseDragged(MouseEvent e) {
textPane.setText("Test");
ifinterrupt();
}

@Override
public synchronized void mouseMoved(MouseEvent e) {
textPane.setText("Test");
ifinterrupt();

}

private void ifinterrupt() {
sleep.interrupt();
textPane.removeMouseMotionListener(S);
frame.removeMouseMotionListener(S);
textPane.setBackground(Color.RED);
try {
Thread.sleep(1000);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

但是,当我移动鼠标并触发 mouseMoved 方法时,JFrame 在执行此行之前退出(System.exit(0)):

textPane.setText("Test");

但理论上它应该在退出之前将文本设置为“Test”,因为setText是在ifinterrupt()行之前设置的:

textPane.setText("Test");
ifinterrupt();

我的问题是,为什么 JFrame 在设置文本之前退出,是什么原因导致的?

感谢任何帮助。

最佳答案

如果您想等待 1 秒才能看到 GUI 重新渲染,请不要阻止 EDT,而是创建一个新计时器并关闭该计时器内的框架

        Thread r = new Thread() {
public void run() {
try {
sleep(1000);
System.exit(0);
} catch (InterruptedException e) {
}
};
};
r.start();

关于java - JFrame 在 TextPane 设置之前退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36324157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com