【发布时间】:2015-05-06 05:09:27
【问题描述】:
我有一个 Java Swing 程序,当 jSlider 更改时,后台程序会在其中执行。但是 jSlider 不能通过人为交互来改变,它会自动减小(jSlider 代表液体的浓度水平)。一旦它减少,其他程序通过 actionListener 读取 jSlider 并提供适当的溶液以满足浓度要求。我无法实现这种情况。我尝试过使用线程,但由于我的知识很少,我可能没有编写正确的代码。
public static void main(String args[])
{
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new IPS().setVisible(true);
}
});
Thread concentration = new Thread()
{
public void run()
{
try
{
Thread.sleep(3000);
} catch (InterruptedException v)
{
}
sldLevel.setValue(sldLevel.getValue() - 3);
}
};
boolean carryOn = true;
while (carryOn && !btnIPSStart.isEnabled())
{
concentration.start();
}
}
使用 Swing 计时器更新代码
public static void main(String args[])
{
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new IPS().setVisible(true);
}
});
Timer myTimer = new Timer(100, 1000);
// I get a error 'int cannot be converted to action listener
while (myTimer.isRunning)
{
sldLevel.setValue(sldLevel.getValue() - 3)
}
【问题讨论】:
-
我看不出你的介绍和你的代码有多大关系。
-
@MadProgrammer 你能帮我写代码吗?
-
1) 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。 2)不要阻塞EDT(事件调度线程)。发生这种情况时,GUI 将“冻结”。有关详细信息和修复,请参阅 Concurrency in Swing。
-
@user3337714,
can you assist me on the code.- 你怎么可能在不到一分钟的时间内阅读完本教程?或者您如何在论坛中搜索使用 Swing Timer 的示例? -
@user3337714,你为什么要创建一个带有两个 int 参数的 Timer?你在教程中哪里看到的?或者您在 Swing Timer API 中的哪里看到了一个将两个整数作为参数的构造函数?读取 API 是解决编译错误的方法。
标签: java multithreading swing event-dispatch-thread seconds