我的登陆首页已经做好了,如果帐号和密码验证正确了,我希望进入到另一个有具体操作的页面.但是不知道怎么才能显示另一个页面.我的代码是这样写的: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);
}
}
}

解决方案 »

  1.   

    TestPanels newPanel = new TestPanels(); 
    newPanel.validate(); 
    return; 
    这段是我进入新JFrame的代码,但是操作的结果是进不了新的JFrame,请知道的帮忙看一下,谢谢!
      

  2.   

    JFrame newFrame = new JFrame();
    在JFrame中加JPanel
      

  3.   

    TestPanels这个是我自己继承JFrame写的一个类,显示出来就是一个自己的JFrame
      

  4.   

    晕,怎么起这么个名字,还有记得加上testPanel.setVisible(true)才能显示
      

  5.   

    JFrame frame = new JFrame;
    TestPanels newPanel = new TestPanels(); 
    frame.add(newPanels);
    frame.setVisible(true);
      

  6.   

    按照上边的代码写了,但是运行的时候报这样的错误:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at jframe.MyFrame.find(MyFrame.java:95)
    at jframe.MyFrame.actionPerformed(MyFrame.java:84)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
      

  7.   

    你的TestPanel要是继承JFrame的话就不要用5楼的代码
    TestPanel p = TestPanel();
    p.setVisible(true);
    就行了