急求:如何实现从一个UI1到UI2,以及从UI2返回UI1的过程?谢谢大家!

jarylover 2007-06-21 02:36:24
请问您一下,我们用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!
...全文
157 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuokai 2007-06-22
  • 打赏
  • 举报
回复
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;

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)
{

}
}
jarylover 2007-06-21
  • 打赏
  • 举报
回复
其中,我现在用的UI1种对应enter的键盘事件的action如下结构:但是总出现UI死掉键盘操作不了的问题!
希望得到指点和提示!

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);

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧