请问您一下,我们用swing来设计UI: 从一个UI1,按某个button后可进入第二个UI2,然后在UI2中按ESC可返回UI1;
(问题1)这个过程用什么样的方法可以实现呢?
(问题2)当从UI1进入UI2时,是通过dispose(0)还是通过setVisible(false)来处理UI1呢?并需要创建一个UI2,是吧?
(问题3)当从UI2返回UI1时,同样是dispose还是setVisible(false)来处理这UI2呢?这里需要重新创建UI1吗?
(问题4)是否有相关的例子啊?
Any suggestion would be welcom!
Thanks a lot!
(问题1)这个过程用什么样的方法可以实现呢?
(问题2)当从UI1进入UI2时,是通过dispose(0)还是通过setVisible(false)来处理UI1呢?并需要创建一个UI2,是吧?
(问题3)当从UI2返回UI1时,同样是dispose还是setVisible(false)来处理这UI2呢?这里需要重新创建UI1吗?
(问题4)是否有相关的例子啊?
Any suggestion would be welcom!
Thanks a lot!
希望得到指点和提示!jContentPane.getInputMap(jContentPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "MoveEnter");
Action ActionEnter = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if(current==0)
{
dispose();
//创建UI2...
}
}
};
jContentPane.getActionMap ().put("MoveEnter", ActionEnter);
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;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)
{
}
}