首页 \ 问答 \ 在edt中更新swing组件(Updating swing components within edt)

在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.


原文:
更新时间:2022-06-29 20:06

最满意答案

建议在发现时间小于小时的时间时添加日(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

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。