Java Swing:后台进程freez Jdialog

时间:2013-07-24 06:42:28

标签: java swing jframe jpanel awt

在我的应用程序中,我必须在另一个jframe的顶部显示自定义对话框,我设计了对话框,它将在调用时执行一些长操作,并在完成后台进程后将一些值返回给jframe。这个应用程序很好我从正常的控制台应用程序(没有GUI的普通java类)调用对话框,但是当我在另一个Jframe上调用我的自定义对话框时,它会冻结我的对话框,并且我尝试使用dobackground进程和线程,这意味着流程正在工作但我不能将值返回到jframe因为线程

//测试应用程序

 public class testapp {

    JFrame jtfMainFrame;
    JButton jbnButton1, jbnButton2;
    JTextField jtfInput;
    JPanel jplPanel;
    public testapp() {
        jtfMainFrame = new JFrame("Which Button Demo");
        jtfMainFrame.setSize(100, 100);
        jbnButton1 = new JButton("Generalization");
        jbnButton2 = new JButton("Verification");
        jtfInput = new JTextField(60);
        jplPanel = new JPanel();
        jbnButton1.setMnemonic(KeyEvent.VK_I); //Set ShortCut Keys
        jbnButton1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {



                Recognization objrecg = new Recognization();//


             objrecg.ShowDialog();//show the dialog
                String tem=objrecg.Feature();//string return from dialog


jtfInput.setText("Button 1!");

            }
        });
        jbnButton2.setMnemonic(KeyEvent.VK_I);
        jbnButton2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                jtfInput.setText("Button 2!");
            }
        });
        jplPanel.setLayout(new FlowLayout());
        jplPanel.add(jtfInput);
        jplPanel.add(jbnButton1);
        jplPanel.add(jbnButton2);
        jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
        jtfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jtfMainFrame.pack();
        jtfMainFrame.setVisible(true);
    }
    public static void main(String[] args) {

        testapp application = new testapp();
    }
}

//自定义对话框代码

public class Recognization extends JPanel{

    public Recognization() {
        setLayout(null); 

        // load the background image
        try {
            img = ImageIO.read(new File("/usr/lib/Legend/bs.bmp"));
        } catch (IOException e) {
            e.printStackTrace();

        }
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // paint the background image and scale it to fill the entire space
        g.drawImage(img, 0, 0, 600, 300, this);
    }
    public void setTemplate(String template)
    {
        this.isoTemplate = template;
    }
    public String getTemplate()
    {
        return this.isoTemplate;
    }

    public void setUtilobject(LSUtil lsutil)

    {
        this.objLSUtil=lsutil;
    }

    public LSUtil getUtilobject()
    {
        return this.objLSUtil;
    }

    public int ShowDialog() {

        try {

            int ret = -1;
        objLog=new LSLog();
        frame= new JFrame("Verification");



        bgPanel = new Recognization();

        imageLabel = new JLabel();


        ImageIcon ii = new ImageIcon("/usr/lib/Legend/capturfp.gif");
        imageLabel.setIcon(ii);

        imageLabel.setBounds(450, 50, 140, 200);
        bgPanel.add(imageLabel);

        JButton exit = new JButton("Exit");

        exit.setBounds(260, 250, 80, 25);

        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }
        });

        bgPanel.add(exit);
            frame.setUndecorated(true);

            frame.setSize(600, 300);

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

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // add the panel with the background image
            frame.add(bgPanel);

            // show the window
            frame.setVisible(true);

            frame.setLocationRelativeTo(null);

            GuiWorker s=new GuiWorker();
            s.execute();

    }

    class GuiWorker extends SwingWorker<Integer, Integer> {
int ret;

          public GuiWorker() {

          }

          @Override
          protected Integer doInBackground() throws Exception {
            System.out.println( "GuiWorker.doInBackground" );
         ///here i do the long process


                }
            return 0;
          }

          @Override
          protected void done() {
          setTemplate(template);//here i set the string whis is return to jframe
            //here i close the dialog
                frame.dispose();
            }

          }       }

}

有没有人建议过这个

0 个答案:

没有答案