谢谢大家了!
做牛做马,感谢不止啊!
急求!

解决方案 »

  1.   

    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;public class ButtonTest extends JFrame implements ActionListener
    {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final int width = 100;
    private static final int height = 50;

    public ButtonTest()
    {
    super("Test");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(width,height);
    setLocation((dimension.width-width)/2,(dimension.height-height)/2);
    JButton button = new JButton("Test");
    button.addActionListener(this);
    getContentPane().add(button);
    pack();
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() instanceof JButton)
    {
    JOptionPane.showMessageDialog(null,"This is a button test!","Test", JOptionPane.INFORMATION_MESSAGE);
    }
    }
    public static void main(String[] args)
    {
    new ButtonTest();
    }
    }
      

  2.   

    非常感谢!
    如果要求两个UI都有几个Button,选中第一个UI的某一个button后按"Enter",进入UI2,UI2也有几个Button,在UI2中按“ESC”可返回UI1;
    是否有这样的例子呢?
    谢谢了!
      

  3.   

    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;public class ButtonTest implements ActionListener,KeyListener
    {
    private static final long serialVersionUID = 1L; private static final int width = 200; private static final int height = 100;

    private JFrame frame;

    private class DialogTest extends JDialog implements KeyListener
    {
    private static final long serialVersionUID = 1L; public DialogTest(JFrame frame)
    {
    super(frame,"DialogTest",true);
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((dimension.width + width) / 2, (dimension.height + height) / 2);
    setSize(width, height);
    JButton button = new JButton("Test");
    JButton button2 = new JButton("Test2");
    add(button,"North");
    add(button2,"South");
    button.addKeyListener(this);
    button2.addKeyListener(this);
    pack();
    setVisible(true);
    } public void keyPressed(KeyEvent e)
    {
    if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
    dispose();
    }
    public void keyReleased(KeyEvent e)
    {

    }
    public void keyTyped(KeyEvent e)
    {

    }
    }
    public ButtonTest()
    {
    frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setSize(width, height);
    frame.setLocation((dimension.width - width) / 2, (dimension.height - height) / 2);
    JButton button = new JButton("Test");
    JButton button2 = new JButton("Test2");
    button.addActionListener(this);
    button.addKeyListener(this);
    button2.addActionListener(this);
    button2.addKeyListener(this);
    frame.getContentPane().add(button2,"South");
    frame.getContentPane().add(button,"North");
    frame.pack();
    frame.setVisible(true);
    button.requestFocusInWindow();
    } public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() instanceof JButton)
    {
    // JOptionPane.showMessageDialog(null, "This is a button test!", "Test", JOptionPane.INFORMATION_MESSAGE);
    new DialogTest(frame);
    }
    } public static void main(String[] args)
    {
    new ButtonTest();
    } public void keyPressed(KeyEvent e)
    {
    if(e.getKeyCode() == KeyEvent.VK_ENTER)
    new DialogTest(frame);
    }
    public void keyReleased(KeyEvent e)
    {

    }
    public void keyTyped(KeyEvent e)
    {

    }
    }
      

  4.   

    如果从UI2中返回时,在UI2的class的ESC键盘事件的actionPerformed()中,
    {
       dispose();
       ui1.setVisible(true);  //get the handle of ui1;
    }然后在UI1的Enter键盘事件的actionPerformed()中实现如下代码:
    {
      dispose();
      UI2 ui2 = new UI2();
      ui2.setVisible(true);
    }
    这样使用dispose(),是否有问题啊? 因为我发现现在有时会出现UI停滞死掉(无任何响应)的问题!
    谢谢了!
    这里如何使用dispose()?它是彻底消除了对应的UI了吗?
    麻烦了!
      

  5.   

    -----------------------------------------------------------------
    Re:如果从UI2中返回时,在UI2的class的ESC键盘事件的actionPerformed()中,
    {
    dispose();
    ui1.setVisible(true); //ge....
    -------------------------------------------------------------------
    這樣用dispose有問題:dispose是將ui以及子組件全部銷毀;
    在UI1的Enter键盘事件中UI1已經被dispose了,在UI2的class的ESC键盘事件
    ===============================================
    dispose();
    ui1.setVisible(true); //get the handle of ui1;
    ===============================================
    ui1已經被銷毀,就算setVisible(true)也沒有組件顯示了.
    修改一下:
    UI1的Enter键盘事件的actionPerformed()中实现如下代码:
    {
    setVisible(false); //不要dispose掉ui1,dispose();
    UI2 ui2 = new UI2();
    ui2.setVisible(true);
    }估計是要實現下面的功能:
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;import javax.swing.JButton;
    import javax.swing.JFrame;public class ButtonTest implements ActionListener,KeyListener
    {
    private static final long serialVersionUID = 1L; private static final int width = 200; private static final int height = 100;

    private JFrame frame;

    private class DialogTest extends JFrame implements KeyListener
    {
    private static final long serialVersionUID = 1L; public DialogTest()
    {
    super("UI2");
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((dimension.width + width) / 2, (dimension.height + height) / 2);
    setSize(width, height);
    JButton button = new JButton("Test");
    JButton button2 = new JButton("Test2");
    add(button,"North");
    add(button2,"South");
    button.addKeyListener(this);
    button2.addKeyListener(this);
    pack();
    setVisible(true);
    } public void keyPressed(KeyEvent e)
    {
    if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
    {
    dispose();
    frame.setVisible(true);
    }
    }
    public void keyReleased(KeyEvent e)
    {

    }
    public void keyTyped(KeyEvent e)
    {

    }
    }
    public ButtonTest()
    {
    frame = new JFrame("UI1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setSize(width, height);
    frame.setLocation((dimension.width - width) / 2, (dimension.height - height) / 2);
    JButton button = new JButton("Test");
    JButton button2 = new JButton("Test2");
    button.addActionListener(this);
    button.addKeyListener(this);
    button2.addActionListener(this);
    button2.addKeyListener(this);

    frame.getContentPane().add(button2,"South");
    frame.getContentPane().add(button,"North");
    frame.pack();
    frame.setVisible(true);
    button.requestFocusInWindow();
    } public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() instanceof JButton)
    {
    // JOptionPane.showMessageDialog(null, "This is a button test!", "Test", JOptionPane.INFORMATION_MESSAGE);
    new DialogTest();
    frame.setVisible(false);
    }
    } public static void main(String[] args)
    {
    new ButtonTest();
    } public void keyPressed(KeyEvent e)
    {
    if(e.getKeyCode() == KeyEvent.VK_ENTER)
    {
    new DialogTest();
    frame.setVisible(false);
    }
    }
    public void keyReleased(KeyEvent e)
    {

    }
    public void keyTyped(KeyEvent e)
    {

    }
    }
      

  6.   

    非常感谢!
    但是你这个代码里好像没有从UI1里传handle到UI2里阿?那么如何在从UI2里的ESC按键处理过程中实现让UI1 setVisible(true)呢?
    即UI1中的Enter 的actionPerformed(){}部分代码的实现,
    和UI2的ESC的actionPerformed(){}的实现
    这两部分着重提示一下!!
    非常感谢!
    真是太感谢你了阿!
    祝愿工作顺利!
      

  7.   

    非常感谢!
    但是您刚才这个代码,我运行了!确实就是我所想要的!
    但是问题和我们的一样,就是如果用keyboard上的Enter和ESC来操作的话,操作一段时间,会出现UI停滞无响应,即彻底死掉的问题!这个就是我们现在希望解决的问题!
    是不是由于比如从UI2返回UI1时,dispose()UI2,而且重新new一个UI1,这样做是否存在conflict! 
    非常感谢,怎么样解决这样的问题呢!!