在我的应用程序中,我必须在另一个 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();
}
} }
}
有人对此有什么建议吗