2015-10-06 32 views
0

我是新來的java和實施圖形用戶界面和即時卡...我必須在框架中包含用戶名和密碼,當我點擊提交我指向另一個框架稱爲菜單中,我有四個按鈕,現在我想點擊一個按鈕並重定向到另一個框架,但我無法做到這一點....請幫助我!如何通過點擊java中的按鈕去新框架?

這裏是我的代碼:

import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.beans.Statement; 
    import java.sql.*; 
    class Log extends imp implements ActionListener { 

    JButton login = new JButton("Login"); 
    JPanel panel = new JPanel(); 
    JTextField tx = new JTextField(15); 
    JPasswordField pass = new JPasswordField(15); 

    Log() { 
     //super("Login Autentification"); 
     setSize(300, 200); 
     setLocation(500, 280); 
     panel.setLayout(null); 


     tx.setBounds(70, 30, 150, 20); 
     pass.setBounds(70, 65, 150, 20); 
     login.setBounds(110, 100, 80, 20); 

     panel.add(login); 
     panel.add(tx); 
     panel.add(pass); 

     getContentPane().add(panel); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     actionlogin(); 
    } 

    public void actionlogin() { 
     login.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       String puname = tx.getText(); 
       String ppaswd = pass.getText(); 
       if (puname.equals("test") && ppaswd.equals("12345")) { 
        newframe regFace = new newframe(); // next GUI.... 

        regFace.setVisible(true); 
        dispose(); 
       } else { 

        JOptionPane.showMessageDialog(null, "Wrong Password/Username"); 
        tx.setText(""); 
        pass.setText(""); 
        tx.requestFocus(); 
       } 




      } 
     }); 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 


    } 

    class newframe extends imp { 


    JButton menu = new JButton("Customer"); 
    JButton menu2 = new JButton("Reports"); 
    JButton menu3 = new JButton("Settings"); 
    JButton menu4 = new JButton("Rooms"); 
    JPanel p2 = new JPanel(); 

    //int p = Integer.parseInt(jButton.getText()); 
    newframe() { 
     //super("MENU"); 
     setSize(300, 200); 
     //setLocation(500,280); 
     p2.setLayout(null); 

     menu.setBounds(150, 10, 100, 50); 

     p2.add(menu); 

     menu2.setBounds(40, 10, 100, 50); 
     p2.add(menu2); 

     menu3.setBounds(150, 90, 100, 50); 
     p2.add(menu3); 
     menu4.setBounds(40, 90, 100, 50); 
     p2.add(menu4); 

     getContentPane().add(p2); 
     //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //setVisible(true); 

     menu.setActionCommand("1"); 


     menu2.setActionCommand("2"); 
     menu3.setActionCommand("3"); 
     menu4.setActionCommand("4"); 
     System.out.println("Reached"); 
    } 



    public void actionPerformed(ActionEvent ae) { 
     System.out.println("Yahadsdasfasfn bhi"); 
     int a = Integer.parseInt(ae.getActionCommand()); 
     switch (a) { 
      case 1: 
       System.out.println("CUSTOMER"); 
       Customer C = new Customer(); 
       C.setSize(400, 400); 
       C.setVisible(true); 
       C.isvissible(true); 
       break; 

     } 

    } 

    } 




    class Customer extends imp implements ActionListener { 

    JLabel jaddcustomer, jsearchcustomer, jdeletecustomer, jupdatecustomer; 
    JButton jb6, jb7, jb8, jb9; 

    Customer() { 
     //super("Customer"); 

     Container c = getContentPane(); 
     FlowLayout fl = new FlowLayout(FlowLayout.LEFT); 
     c.setLayout(fl); 
     jb6 = new JButton("Adding New Customer"); 
     jaddcustomer = new JLabel(""); 
     jb6.addActionListener(this); 
     jb7 = new JButton("Search Customer Record"); 
     jsearchcustomer = new JLabel(""); 
     jb7.addActionListener(this); 

     jb8 = new JButton("Delete Customer Record"); 
     jdeletecustomer = new JLabel(""); 
     jb8.addActionListener(this); 
     jb9 = new JButton("Update Customer Record"); 
     jupdatecustomer = new JLabel(""); 
     jb9.addActionListener(this); 

     c.add(jb6); 
     c.add(jaddcustomer); 

     c.add(jb7); 
     c.add(jsearchcustomer); 
     c.add(jb8); 
     c.add(jdeletecustomer); 

     c.add(jb9); 
     c.add(jupdatecustomer); 

    }@Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

    } 



    } 

    class login { 

    public static void main(String[] args) { 
     try { 

      // execute..... 


      // login 
      Log l = new Log(); //displays login page 

      Jbuton b = new Jbuton(); 
      // newframe f=newframe(); 
      Customer c = new Customer(); 
      //l.isvissible(false); 
      c.isvissible(true); 
     } catch (Exception e) { 
      System.out.println("Exception"); 

      JOptionPane.showMessageDialog(null, e.getMessage()); 
     } 
    } 
    } 
+0

這裏的imp類:import java.awt.event.ActionEvent; import javax.swing.JFrame; 公共類小鬼延伸的JFrame { \t公共IMP(){} \t公共無效isvissible(布爾噸){ \t \t調用setVisible(T); \t \t \t} \t \t \t \t \t \t \t \t} –

+1

http://stackoverflow.com/help/how-to-ask – STW

+0

@MahNoor如果您有任何你任何你想添加到您的問題,而不是**將其添加到您的問題,請不要發表評論**。 –

回答

0

您需要更改您處理按下一個JButton的方式你的代碼。您需要添加ActionListener來收聽緊急事件:

// make this class implements ActionListener 
public class newframe extends imp implements ActionListener{ 

    JButton menu = new JButton("Customer"); 
    // add this as the action listener 
    menu.addActionListener(this); 
}