您不应该让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."); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)