在edt中更新swing组件(Updating swing components within edt)
如果我理解正确,那么当我创建GUI swing组件时,例如我有这个:
public class frameExample extends JFrame{ public frameExample(){ //Here adding bunch if components setVisible(true); } }
因此,只要我不调用setVisible方法,就会从创建实例的线程中创建组件。 所以如果在我的主要方法的类中写:
JFrame test=new frameExample();
和我sysout
Thread.currentThread.getName();
在setExample的构造函数中,在setVisible之前,我应该得到:main。
之后,创建和维护swing元素的责任被传递给event-dispatch-thread,因为它不是线程安全的,所以每个组件的添加/删除/修改都应该在EDT线程中完成。
所以我应该将我的setVisible作为构造函数中的最后一行代码,或者单独调用它。
据我所知,所有的事件都是通过EDT进行的。 因此,如果我在例如actionPerformed方法中创建一个新组件,它应该没问题。
另外,如果我将runnable实例传递给invokeLater或invokeAndWait,那么所有run()方法都将由EDT完成。
所以这就是为什么我很困惑。
我做了这个代码:
public class GUI extends JFrame { JButton btn = new JButton("Change"); JMenuBar m = new JMenuBar(); public GUI() { super("Test"); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); m.add(new JMenu("menu")); add(m, BorderLayout.NORTH); add(btn, BorderLayout.SOUTH); System.out.println("Current thread: before setVisible "+Thread.currentThread().getName()); setVisible(true); System.out.println("Current thread: after setVisible "+Thread.currentThread().getName()); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { add(new JButton("testbtn1"), BorderLayout.EAST); add(new JButton("testbtn2")); System.out.println("Current thread: "+Thread.currentThread().getName()); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 1E8; i++) { Math.sin(5.0); } System.out.println("Current thread: "+Thread.currentThread().getName()); } }).start(); } }); }
}
所以在我的anonim类中,我在EDT中添加了两个按钮,但是在我调整它之后组件没有添加到我的框架中(这迫使edt更新其组件???这是什么原因?) 。
因此,即使在edt中我也不会得到我的新组件,但是当我在edt之外创建一个随机线程并使其更改一些gui元素属性(例如setText)在edt之外工作正常。
所以我的第一个问题是:为什么我的组件没有在edt中更新,以及为什么它们在调整大小后可见
第二个:为什么我可以对edt外的摆动组件进行更改,一切正常? 这只是一个随机的线程行为,例如,没有同步块的情况下工作正常,但是当你重新运行程序时,它会因为缺乏同步而最终崩溃。
If I understand correctly then when I create GUI swing components, for example I have this:
public class frameExample extends JFrame{ public frameExample(){ //Here adding bunch if components setVisible(true); } }
So as long as I don't call the setVisible method the components are being made from the thread the instance was created. So if in a class where i have my main method write:
JFrame test=new frameExample();
and I sysout
Thread.currentThread.getName();
in the constructor in frameExample just before setVisible I should get: main.
After that the responsibility of creating and maintaining swing elements are passed to the event-dispatch-thread and because it is not thread safe every component add/remove/modify should be done within the EDT thread.
So I should place my setVisible as the last line of code in my constructor, or call it separately.
As I understand all event listening goes through the EDT. So if I create a new component within for example an actionPerformed method it should do fine.
Also if I pass a runnable instance to invokeLater or invokeAndWait then all the run() method will be done by the EDT.
So here is why I'm confused.
I made this code:
public class GUI extends JFrame { JButton btn = new JButton("Change"); JMenuBar m = new JMenuBar(); public GUI() { super("Test"); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); m.add(new JMenu("menu")); add(m, BorderLayout.NORTH); add(btn, BorderLayout.SOUTH); System.out.println("Current thread: before setVisible "+Thread.currentThread().getName()); setVisible(true); System.out.println("Current thread: after setVisible "+Thread.currentThread().getName()); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { add(new JButton("testbtn1"), BorderLayout.EAST); add(new JButton("testbtn2")); System.out.println("Current thread: "+Thread.currentThread().getName()); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 1E8; i++) { Math.sin(5.0); } System.out.println("Current thread: "+Thread.currentThread().getName()); } }).start(); } }); }
}
So in my anonim class where I add the two buttons im in the EDT, but the components are not added to my frame just after i resize it (which forces the edt to update its components??? what is the reason for this?).
So i dont get my new components even within edt, however when i create a random thread outside edt and make it change some gui element property (for example setText) in just works fine outside the edt.
So my first question is: why my components are not updated within edt and why they are visible after resize
second one: why can i make changes to swing components outside edt and everything works fine? Is this just random thread behvior where for example things just work fine without sync block but when you rerun you program at some point it will eventually crash because the lack of sync.
原文:
最满意答案
建议在发现时间小于小时的时间时添加日(24小时)。
$start_time = explode(":",$m_time); $start_time_hr = $start_time[0]; $start_time_min = $start_time[1]; $start_tot_min = intval($start_time_hr*60) + $start_time_min; $end_time = explode(":",$mx_time); $end_time_hr = $end_time[0]; // Add a day if end time smaller than start time if($end_time_hr<$start_time_hr){ $end_time_hr += 24; } $end_time_min = $end_time[1]; $end_tot_min = intval($end_time_hr*60) + $end_time_min; //converting hour to min + min $total_min_diff = intval($end_tot_min - $start_tot_min); $hr_diff = intval($total_min_diff/60); $min_diff = intval($total_min_diff%60);
希望这有帮助:D
Suggest to add day (24 hour) when find out the time out hour is smaller than time in hour.
$start_time = explode(":",$m_time); $start_time_hr = $start_time[0]; $start_time_min = $start_time[1]; $start_tot_min = intval($start_time_hr*60) + $start_time_min; $end_time = explode(":",$mx_time); $end_time_hr = $end_time[0]; // Add a day if end time smaller than start time if($end_time_hr<$start_time_hr){ $end_time_hr += 24; } $end_time_min = $end_time[1]; $end_tot_min = intval($end_time_hr*60) + $end_time_min; //converting hour to min + min $total_min_diff = intval($end_tot_min - $start_tot_min); $hr_diff = intval($total_min_diff/60); $min_diff = intval($total_min_diff%60);
Hope this help :D
相关问答
更多-
$datetime1 = new DateTime('05-11-2012 11:25:00'); $datetime2 = new DateTime('06-11-2012 17:45:00'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%a days'); //return +1 days //or $datetime1 = date_create('05-11-2012 11:25:00'); $datet ...
-
如何使用C#获取两个DateTime对象之间的时差?(How do I get the time difference between two DateTime objects using C#?)[2023-07-12]
以下示例演示如何执行此操作: DateTime a = new DateTime(2010, 05, 12, 13, 15, 00); DateTime b = new DateTime(2010, 05, 12, 13, 45, 00); Console.WriteLine(b.Subtract(a).TotalMinutes); 执行时打印“30”,因为日期/时间之间有30分钟的差异。 DateTime.Subtract(DateTime x)的结果是一个TimeSpan对象 ,它提供了其他有用的属性 ... -
使用此功能 function timeAgo($time_ago) { $time_ago = strtotime($time_ago); $cur_time = time(); $time_elapsed = $cur_time - $time_ago; $seconds = $time_elapsed; $minutes = round($time_elapsed / 60); $hours = round( ...
-
.htaccess没有“思考”任何关于时间的事情。 您应该检查系统时间,例如* nix系统上的date输出,并将其与PHP的time()进行比较。 差异可能来自PHP的时区配置。 使用date_default_timezone_get检查PHP时区。 如果您无法控制PHP的ini配置, date_default_timezone_set在运行时使用date_default_timezone_set 。 .htaccess doesn't "think" anything about the time. Yo ...
-
但我需要如下:24小时3分5秒 如果您使用的是PHP 5.3或更高版本(您应该是),则可以使用内置的DateTime类生成可轻松格式化的DateInterval 。 $time_one = new DateTime('2010-07-29 12:43:54'); $time_two = new DateTime('2010-07-30 01:23:45'); $difference = $time_one->diff($time_two); echo $difference->format('%h hour ...
-
用PHP获取Windows时间(Getting Windows Time in PHP)[2021-03-04]
我认为PHP不考虑Windows DST设置。 时区根据定义的标准进行解释。 此外,根据上次出现: 即使在设置时区后,夏令时也不正确 但请参阅http://php.net/manual/en/function.date-default-timezone-set.php 有一些示例代码使用WShell在Win注册表中查询实际值,包括DST标志。 然后使用timezone_abbreviations_list()您可以找到具有正确时区名称和DST标志的别名,设置此项。 另外@Pekka在这里有另一个手动解决方法 ... -
时差我做得对吗?(time difference Am I doing it right?)[2022-10-28]
你可以使用date_diff 。 链接页面的示例: diff($datetime2); echo $interval->format('%R%a days'); ?> 可在此处找到可能格式的列表 You can use date_diff. Exampl ... -
您是否考虑了客户端和服务器之间的延迟? 即使它们是同一个,如果处理响应需要> 1 ms,您将获得非零差异。 编辑:尝试getTimezoneOffset() (JavaScript,返回分钟)和DateTime::getOffset() (PHP,返回秒)。 getTime()和time()可能返回与时区相关的值。 如果您尚未在PHP脚本中设置时区,那么这也可能是一个因素。 您也可以在PHP中尝试这个: echo $client_time."\n".$server_time; 看看JS和PHP每次报告的时 ...
-
您的日期和时间格式不是导致问题的有效strototime格式。 Your date and time format is not a valid strototime format which is causing the problem.
-
建议在发现时间小于小时的时间时添加日(24小时)。 $start_time = explode(":",$m_time); $start_time_hr = $start_time[0]; $start_time_min = $start_time[1]; $start_tot_min = intval($start_time_hr*60) + $start_time_min; $end_time = explode(":",$mx_time); $end_time_hr = $end_time[0]; ...