Swing JFrame按钮动作侦听器无法正常工作

时间:2014-06-09 04:18:38

标签: java swing jframe jbutton actionlistener

在按JFrame命名为JFrame后,我想从一个JButton移到另一个login。我在下面附上我的两个框架代码,如果有更正或任何人想给我指示或指导,那么可以给。

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login implements ActionListener {
private JFrame jFrame = null;  
private JPanel jContentPane = null;
private JLabel id = null;
private JTextField userid = null;
private JLabel pass = null;
private JPasswordField password = null;
private JButton login = null;       
    private JFrame getJFrame() 
    {
        if (jFrame == null) 
        {
            jFrame = new JFrame();
    jFrame.setSize(new Dimension(346, 301));
            jFrame.setResizable( false );
    jFrame.setTitle("Shree Datta Digambar");
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setContentPane(getJContentPane());
        }
        return jFrame;
}
    private JPanel getJContentPane() 
    {
        if (jContentPane == null) 
        {
            pass = new JLabel();
    pass.setBounds(new Rectangle(21, 105, 116, 27));
        pass.setText("Password");
    id = new JLabel();
            id.setBounds(new Rectangle(21, 54, 119, 26));
    id.setText("Enter User Id");
            jContentPane = new JPanel();
    jContentPane.setLayout(null);
    jContentPane.add(id, null);
    jContentPane.add(getUserid(), null);
            jContentPane.add(pass, null);
    jContentPane.add(getPassword(), null);
    jContentPane.add(getLogin(), null);
        }
        return jContentPane;
}
    private JTextField getUserid() 
    {
        if (userid == null) 
        {
            userid = new JTextField();
    userid.setBounds(new Rectangle(164, 53, 140, 26));
        }
        return userid;
}   
private JPasswordField getPassword() 
    {
        if (password == null) 
        {
            password = new JPasswordField();
    password.setBounds(new Rectangle(165, 106, 137, 27));
        }
        return password;
}
private JButton getLogin() 
    {
        if (login == null) 
        {
            login = new JButton();
    login.setBounds(new Rectangle(165, 169, 136, 27));
    login.setText("Login");
            login.addActionListener((ActionListener) this);
        }
        return login;
}

public static void main(String[] args) 
    {
        Login l=new Login();
        JFrame f=new JFrame();           
        f=l.getJFrame();
        f.setLocation(300,150);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==login)
        {
            if(userid.getText().equals("")||password.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Please Enter Details");
            }
            else
            {
                if(userid.getText().equals("digambar")&&password.getText().equals("dalvi"))
                {                                 
                    JOptionPane.showMessageDialog(null,"Welcome To Customer Details of Papers");
                    getJFrame().dispose();  
                    JMenuBar menuBar = new JMenuBar();
                    MainMenu f = new MainMenu();                              
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Sorry Please Enter Valid Details");
                    userid.setText("");
                    password.setText("");
                }
            }
        }

    }       
}

第二帧代码是

       import java.awt.event.ActionEvent;
       import java.awt.event.ActionListener;
       import javax.swing.JFrame;
       import javax.swing.JMenu;
       import javax.swing.JMenuBar;
       import javax.swing.JMenuItem;
       public class MainMenu extends JFrame implements ActionListener
       {  
public static void main(String[] args) 
{
    MainMenu f=new MainMenu();
    f.setVisible(true);
    f.setSize(646,401);
    f.setResizable( false );
    f.setTitle("Shree Datta Digambar");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menuBar = new JMenuBar();
    f.setJMenuBar(menuBar);

    JMenu customer=new JMenu("Customer");
    menuBar.add(customer);
    JMenu paper=new JMenu("Paper");
    menuBar.add(paper);
    JMenu edit=new JMenu("Edit");
    menuBar.add(edit);              
    JMenu view=new JMenu("View");
    menuBar.add(view); 
    JMenu bill=new JMenu("Bill");
    menuBar.add(bill);
    JMenu help=new JMenu("Help");
    menuBar.add(help);

    JMenuItem newItem = new JMenuItem("New Customer");
    customer.add(newItem);
    JMenuItem deleteItem = new JMenuItem("Delete Customer");
    customer.add(deleteItem);

    JMenuItem addpaperItem = new JMenuItem("Add Paper");
    paper.add(addpaperItem);
    JMenuItem deletepaperItem = new JMenuItem("Delete Paper");
    paper.add(deletepaperItem);

    JMenuItem customer_detail = new JMenuItem("Customer Detail");
    edit.add(customer_detail);
    JMenuItem paper_detail = new JMenuItem("Paper Detail");
    edit.add(paper_detail);

    JMenuItem perticular_customer_detail = new JMenuItem("Perticular Customer Detail");
    view.add(perticular_customer_detail);

    JMenuItem customer_bill = new JMenuItem("Customer Bills");
    bill.add(customer_bill);

    newItem.addActionListener(new MainMenu());
    deleteItem.addActionListener(new MainMenu());
    addpaperItem.addActionListener(new MainMenu());
    deletepaperItem.addActionListener(new MainMenu());
    customer_detail.addActionListener(new MainMenu());
    paper_detail.addActionListener(new MainMenu());
    perticular_customer_detail.addActionListener(new MainMenu());
    customer_bill.addActionListener(new MainMenu());

    customer.addActionListener(new MainMenu());        
    paper.addActionListener(new MainMenu());
    edit.addActionListener(new MainMenu());
    view.addActionListener(new MainMenu());
    bill.addActionListener(new MainMenu());
    help.addActionListener(new MainMenu());
}
@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}
}

3 个答案:

答案 0 :(得分:1)

您可以使用课程的 constructors 。构造函数用于初始化对象。我用过构造函数。请查找更新的代码。

<强> Login.java

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;

public class Login implements ActionListener {

private JFrame jFrame = null;  
private JPanel jContentPane = null;
private JLabel id = null;
private JTextField userid = null;
private JLabel pass = null;
private JPasswordField password = null;
private JButton login = null;       

    //Constructor   
public Login() {

     pass = new JLabel();
     pass.setBounds(new Rectangle(21, 105, 116, 27));
     pass.setText("Password");
     id = new JLabel();
     id.setBounds(new Rectangle(21, 54, 119, 26));
     id.setText("Enter User Id");

     userid = new JTextField();
     userid.setBounds(new Rectangle(164, 53, 140, 26));

     password = new JPasswordField();
     password.setBounds(new Rectangle(165, 106, 137, 27));

     login = new JButton();
     login.setBounds(new Rectangle(165, 169, 136, 27));
     login.setText("Login");
     login.addActionListener((ActionListener) this);

     jContentPane = new JPanel();
     jContentPane.setLayout(null);
     jContentPane.add(id, null);
     jContentPane.add(userid, null);
     jContentPane.add(pass, null);
     jContentPane.add(password, null);
     jContentPane.add(login, null);


    jFrame = new JFrame();
    jFrame.setSize(new Dimension(346, 301));
    jFrame.setResizable( false );
    jFrame.setTitle("Shree Datta Digambar");
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setContentPane(jContentPane);
    jFrame.setLocation(300,150);
    jFrame.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==login)
        {
            if(userid.getText().equals("")||password.getPassword().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Please Enter Details");
            }
            else
            {
                if(userid.getText().equals("digambar")&&  Arrays.equals(password.getPassword(), "dalvi".toCharArray()))
                {                                 
                    JOptionPane.showMessageDialog(null,"Welcome To Customer Details of Papers");
                    this.jFrame.dispose();  
                    MainMenu f = new MainMenu();
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Sorry Please Enter Valid Details");
                    userid.setText("");
                    password.setText("");
                }
            }
        }

    } 

   public static void main(String[] args){
        Login l=new Login();
    }
}

<强> MainMenu.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainMenu extends JFrame implements ActionListener
{ 
        //Constructor   
    public MainMenu() 
    {
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        this.setVisible(true);
        this.setSize(646,401);
        this.setResizable( false );
        this.setTitle("Shree Datta Digambar");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenu customer=new JMenu("Customer");
        menuBar.add(customer);
        JMenu paper=new JMenu("Paper");
        menuBar.add(paper);
        JMenu edit=new JMenu("Edit");
        menuBar.add(edit);              
        JMenu view=new JMenu("View");
        menuBar.add(view); 
        JMenu bill=new JMenu("Bill");
        menuBar.add(bill);
        JMenu help=new JMenu("Help");
        menuBar.add(help);

       JMenuItem newItem = new JMenuItem("New Customer");
        customer.add(newItem);
        JMenuItem deleteItem = new JMenuItem("Delete Customer");
        customer.add(deleteItem);

        JMenuItem addpaperItem = new JMenuItem("Add Paper");
        paper.add(addpaperItem);
        JMenuItem deletepaperItem = new JMenuItem("Delete Paper");
        paper.add(deletepaperItem);

        JMenuItem customer_detail = new JMenuItem("Customer Detail");
        edit.add(customer_detail);
        JMenuItem paper_detail = new JMenuItem("Paper Detail");
        edit.add(paper_detail);

        JMenuItem perticular_customer_detail = new JMenuItem("Perticular Customer Detail");
        view.add(perticular_customer_detail);

        JMenuItem customer_bill = new JMenuItem("Customer Bills");
        bill.add(customer_bill);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public static void main(String arg[])
    {
        MainMenu menu1= new MainMenu();
    }
    }

答案 1 :(得分:1)

您可以根据JPanel构建用户界面而不是单独的框架,并使用CardLayout切换它们,或者使用JDialog登录面板和JFrame }用于主面板

重点是你可以将控制机制与UI组件分开,这样就可以更容易地改变逻辑路径。

请查看How to Use CardLayoutHow to Make Dialogs了解详情。

您还应该正确使用layout managers

答案 2 :(得分:0)

您可以为JMenuItem使用匿名内部类,

 JMenuItem newItem = new JMenuItem("New Customer");  
 newItem.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               MainMenu.this.dispose();
               New_Customer nn = new New_Customer() {};
           }
       });

让MainMenu实现ActionListener接口(class MainMenu implements ActionListener

JMenuItem newItem = new JMenuItem("New Customer");
newItem.addActionListener(this);
newItem.setActionCommand("New Customer");

public void actionPerformed(ActionEvent e) 
{
    if(e.getActionCommand().equals("New Customer"))
    {
        System.out.println("clicked new customer menu");
        MainMenu.this.dispose();
        New_Customer nn = new New_Customer() {};
    }
}