这边是我做的一个退出窗口确认信息的程序,由两个窗口组成,当我运行第一窗口也就是主窗口的时候,那个退出窗口也会自动弹出来,此处有不解。若按下主窗口中的Quit,那个退出窗口可以正常弹出,若按退出窗口中的No,退出窗口消失,又回到主窗口中,但是可以同时对两个窗口进行控制,我还有一个不解是:如何才能在同一时刻只能对一个窗口进行操作呢。 CODE: //SimpleExample.java 主窗口 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*;
public class SimpleExample extends JFrame implements ActionListener { JButton a,b; public SimpleExample() { a = new JButton("Click"); b = new JButton("Quit"); this.getContentPane().setLayout(new FlowLayout()); this.getContentPane().add(a); this.getContentPane().add(b); this.setTitle("Menu"); this.setBounds(450,300,200,200); a.addActionListener(this); b.addActionListener(new QuitFrame()); this.setVisible(true); } public void actionPerformed(ActionEvent ae) { this.setVisible(false); try { Thread.sleep(2000); } catch (Exception ex) { ex.printStackTrace(); } this.setVisible(true); } public static void main(String[] args) { SimpleExample se = new SimpleExample(); } } ------------------------------------------------------------------------- //QuitFrame.java 退出确认窗口 import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class QuitFrame implements ActionListener { static JFrame win; public void actionPerformed(ActionEvent ea) { win.setVisible(false); if((ea.getActionCommand()).equals("Quit")) win.setVisible(true); if((ea.getActionCommand()).equals("No")) win.setVisible(false); if((ea.getActionCommand()).equals("Yes")) { win.setVisible(false); System.exit(0); } } public QuitFrame() { JLabel msg = new JLabel(); JButton yesbutton = new JButton(); JButton nobutton = new JButton(); JPanel buttonbox = new JPanel(); win = new JFrame("Quit"); msg.setText("Do you really want to quit?"); msg.setBorder(new EmptyBorder(50,50,50,50)); yesbutton.setText("Yes"); nobutton.setText("No");
buttonbox.add(yesbutton); buttonbox.add(nobutton);
win.getContentPane().setLayout(new BorderLayout()); buttonbox.setLayout(new FlowLayout()); win.getContentPane().add(buttonbox,"South"); win.getContentPane().add(msg,"Center"); yesbutton.addActionListener(this);
nobutton.addActionListener(this); win.pack(); win.show(); } public static void main(String [] args) { QuitFrame qf = new QuitFrame(); } } THAT'S ALL
[此贴子已经被作者于2005-9-8 16:44:54编辑过]