swing中如何从一个JFrame跳到另一个JFrame?急!
我的登陆首页已经做好了,如果帐号和密码验证正确了,我希望进入到另一个有具体操作的页面.但是不知道怎么才能显示另一个页面.我的代码是这样写的:
public class MyFrame extends JFrame implements ActionListener {
private JTextField jtfName;
private JPasswordField jpfPwd;
private JButton jbtSubmit;
private JButton jbtReset;
private String[][] user = { { "xiezhen", "1" }, { "xiaowei", "2" },
{ "zhangmeijie", "3" }, { "wanghui", "4" }, { "zhangyikun", "5" } };
public MyFrame() {
JPanel jpLables = new JPanel();
jpLables.setLayout(new GridLayout(2, 1));
jpLables.add(new JLabel("用户名"));
jpLables.add(new JLabel("密 码"));
jtfName = new JTextField(20);
jtfName.setSize(50, 20);
jpfPwd = new JPasswordField(20);
jpfPwd.setSize(50, 20);
JPanel jpTextFields = new JPanel();
jpTextFields.setLayout(new GridLayout(2, 1));
jpTextFields.add(jtfName);
jpTextFields.add(jpfPwd);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(jpLables, BorderLayout.WEST);
p1.add(jpTextFields, BorderLayout.CENTER);
JPanel p2 = new JPanel();
p2.add(jbtSubmit = new JButton("提交"));
p2.add(jbtReset = new JButton("重置"));
getContentPane().setLayout(null);
p1.setBounds(new Rectangle(300, 250, 300, 80));
getContentPane().add(p1, null);
p2.setBounds(new Rectangle(300, 350, 300, 80));
getContentPane().add(p2, null);
jbtSubmit.addActionListener(this);
jbtReset.addActionListener(this);
}
public static void main(String[] args) {
// 得到屏幕长和宽
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
// 创建框架
MyFrame frame = new MyFrame();
frame.setTitle("运营管理平台");
frame.setSize(1000, 800);
// 取到中间位置
int x = (screenWidth - frame.getSize().width) / 2;
int y = (screenHeight - frame.getSize().height) / 2;
// 设置frame属性
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(x, y);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtSubmit) {
find(jtfName.getText().trim(), new String(jpfPwd.getPassword()));
} else if (e.getSource() == jbtReset) {
reset();
}
}
public void find(String name, String password) {
for (int i = 0; i < user.length; i++) {
if (user[i][0].equals(name) && user[i][1].equals(password)) {
TestPanels newPanel = new TestPanels();
newPanel.validate();
return;
} else if (user[i][0].equals(name)) {
JOptionPane
.showMessageDialog(this, "password not match name",
"For Your Information",
JOptionPane.INFORMATION_MESSAGE);
return;
} else {
JOptionPane
.showMessageDialog(this, "name not fount",
"For Your Information",
JOptionPane.INFORMATION_MESSAGE);
return;
}
}
}
public void reset() {
if (jtfName.getText() != null
|| String.valueOf(jpfPwd.getPassword()) != null) {
jtfName.setText(null);
jpfPwd.setText(null);
}
}
}