【问题标题】:Java Timer unknown eventJava Timer 未知事件
【发布时间】:2016-03-05 23:50:05
【问题描述】:

当我在我的程序中执行以下方法时,一些组件会在计时器用完时发生变化。例如,我创建了一个 jTextArea 的大小更改,而没有任何包含构造来更改其大小的事件。如果我先扩展 jTextArea 然后启动计时器,或者反之亦然,都没有关系。

//Show Debug Information for given Seconds with given Text
void giveUserInformation(String input, boolean function, int duration) {
    //Debug information and label visibility handling
    jLabelDebug.setVisible(true);
    jLabelDebug.setText(input);

    //Image
    if (function)
        jLabelDebug.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/ok-icon.png")));
    else
        jLabelDebug.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/Actions-edit-delete-icon.png")));

    //Show duration
    if (timerShowDurationRuns) {
        timerShowDuration.cancel();
        timerShowDuration = new Timer();
    }

    timerShowDurationRuns = true;
    //fadeIn();
    timerShowDuration.schedule(new TimerTask() {
        @Override
        public void run() {
            jLabelDebug.setVisible(false);
            timerShowDurationRuns = false;
            //fadeOut();
        }
    }, duration * 1000);
    setCursor(Cursor.getDefaultCursor());
}

【问题讨论】:

  • 您将标签设置为不可见 - 这是更改内容的指令 - 以及 jtextArea 在哪里?
  • 假设您正在使用布局管理器,当您更改组件的可见状态时,需要计算布局以补偿该组件的丢失(因为不可见组件不占用空间)

标签: java swing events timer freeze


【解决方案1】:

当您创建 JTextArea 时,您应该使用如下代码:

JTextArea textArea = new JTextArea(5, 30);

现在文本区域将能够确定其首选大小,并在您向其添加文本时保持固定。

然后,当您将其添加到 GUI 时,您可以使用如下代码:

frame.add( new JScrollPane( textArea ) );

现在,当您添加更多数据时,文本区域大小将保持固定,但会在需要时出现滚动条。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多