栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在不等待用户的Gui输入的情况下停止Java运行整个代码

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在不等待用户的Gui输入的情况下停止Java运行整个代码

您不应该让Jframe启动其他Jframe,尤其是如果您希望子窗口充当模式对话框时,这种对话框会在启动窗口中暂停代码,直到完全处理完为止。如果是这样的话,使对话窗口
对话框 在地方Jframes的对话窗口的使用模式JDialogs。

例如:

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MainWelcomeGui2 {   public static void main(String[] args) {      final Jframe frame = new Jframe("Main GUI");      JButton addDeptButtonLaunchJframe = new JButton( "Add a New Department, Launch Jframe");      JButton addDeptButtonLaunchJDialog = new JButton( "Add a New Department, Launch JDialog");      addDeptButtonLaunchJDialog.addActionListener(new LaunchJDialogListener( frame));      addDeptButtonLaunchJframe.addActionListener(new LaunchJframeListener());      JPanel panel = new JPanel();      panel.add(addDeptButtonLaunchJDialog);      panel.add(addDeptButtonLaunchJframe);      frame.add(panel);      frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);      frame.pack();      frame.setLocationRelativeTo(null);      frame.setVisible(true);   }}class LaunchJDialogListener implements ActionListener {   JDialog dialog;   public LaunchJDialogListener(Jframe parentframe) {      JButton doneButton = new JButton(new AbstractAction("Done") {         public void actionPerformed(ActionEvent e) { dialog.dispose();         }      });      JPanel panel = new JPanel();      panel.setPreferredSize(new Dimension(100, 100));      panel.add(doneButton);      dialog = new JDialog(parentframe, "Dialog", true);      dialog.add(panel);      dialog.pack();      dialog.setLocationRelativeTo(null);   }   @Override   public void actionPerformed(ActionEvent e) {      System.out.println("called before setting dialog visible");      dialog.setVisible(true);      System.out .println("called after setting dialog visible. Note that this line doesn't show until the dialog disappears");   }}class LaunchJframeListener implements ActionListener {   Jframe frame;   public LaunchJframeListener() {      JButton doneButton = new JButton(new AbstractAction("Done") {         public void actionPerformed(ActionEvent e) { frame.dispose();         }      });      JPanel panel = new JPanel();      panel.setPreferredSize(new Dimension(100, 100));      panel.add(doneButton);      frame = new Jframe("Jframe");      frame.add(panel);      frame.pack();      frame.setLocationRelativeTo(null);   }   @Override   public void actionPerformed(ActionEvent e) {      System.out.println("called before setting frame visible");      frame.setVisible(true);      System.out .println("called after setting frame visible.  Note that this line shows up immediately.");   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496457.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号