带有事件监听器的Java多菜单项?

时间:2016-12-30 10:35:06

标签: java swing user-interface layout listener

我这里有两个菜单项,一个是学生信息,另一个是教师信息。我已经为学生和老师添加了菜单。

我听到事件监听器如何将动作添加到菜单和menu1,假设当用户点击菜单1然后应该显示学生信息时,对于menu2应该执行教师信息动作。我很困惑在哪里添加动作监听器并为学生和教师执行操作。

我是java.kindly中事件和动作列表器的新手,建议解决方案

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;

public class Searchdb extends JFrame implements ActionListener {

//Initializing Components
    JLabel lb,lbd,lb1, lb2, lb3, lb5;
    JTextField tf1, tf2,tf3,tf5,tfd;
    JButton btn;
 JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Student");
    JMenu menu1 = new JMenu("Teacher");
    //Creating Constructor for initializing JFrame components
    Searchdb() {
        //Providing Title
        super("Fetching Roll Information");

        setJMenuBar(menuBar);
        menuBar.add(menu);
        menuBar.add(menu1);


        lb5 = new JLabel("Roll Number:");
        lb5.setBounds(20, 20, 100, 20);
        tf5 = new JTextField(20);
        tf5.setBounds(130, 20, 200, 20);

        lbd = new JLabel("Date:");
        lbd.setBounds(20, 50, 100, 20);
        tfd = new JTextField(20);
        tfd.setBounds(130, 50, 200, 20);


        btn = new JButton("Submit");
        btn.setBounds(50, 50, 100, 20);
        btn.addActionListener(this);

        lb = new JLabel("Fetching Student Information From Database");
        lb.setBounds(30, 80, 450, 30);
        lb.setForeground(Color.black);
        lb.setFont(new Font("Serif", Font.PLAIN, 12));
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);

        lb1 = new JLabel("Name:");
        lb1.setBounds(20, 120, 100, 20);
        tf1 = new JTextField(50);
        tf1.setBounds(130, 120, 200, 20);
        lb2 = new JLabel("Fathername:");
        lb2.setBounds(20, 150, 100, 20);
        tf2 = new JTextField(100);
        tf2.setBounds(130, 150, 200, 20);
        lb3 = new JLabel("State:");
        lb3.setBounds(20, 180, 100, 20);
        tf3 = new JTextField(50);
        tf3.setBounds(130, 180, 200, 20);

        setLayout(null);

        //Add components to the JFrame
        add(lb5);
        add(tf5);
        add(lbd);
        add(tfd);
        add(btn);

        add(lb);
        add(lb1);
        add(tf1);
        add(lb2);
        add(tf2);
        add(lb3);
        add(tf3);


        //Set TextField Editable False
        tf1.setEditable(false);
        tf2.setEditable(false);
        tf3.setEditable(false);

    }

    public void actionPerformed(ActionEvent e) {
        //Create DataBase Coonection and Fetching Records

        try {
            String str = tf5.getText();

            Datestri = tfd.getText();//Getting the unable to convert String to Date error

            System.out.println(str);
            System.out.println(stri);

            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//host:port/servicename","username","password");
            PreparedStatement st = con.prepareStatement("select Name,Fathername,State from student_db where roll_number=? and medium=?");
            System.out.println(st);
            st.setString(1, str);
            st.setDate(2, stri);



            //Excuting Query
            ResultSet rs = st.executeQuery();
            System.out.println(rs);

            if (rs.next()) {
                String s = rs.getString(1);
                String s1 = rs.getString(2);
                String s2 = rs.getString(3);


                //Sets Records in TextFields.
                tf1.setText(s);
                tf2.setText(s1);
                tf3.setText(s2);

            } else {
                JOptionPane.showMessageDialog(null, "Student not Found");
            }

            //Create Exception Handler
        } catch (Exception ex) {

            System.out.println(ex);
        }
    }

    public void actionPerformed(ActionEvent e) {
        //Create DataBase Coonection and Fetching Records

        //Teacher information should be retrieved from the db

        }

//Running Constructor

    public static void main(String args[]) {
        new Searchdb();
    }
}

感谢您提前提供任何帮助。

2 个答案:

答案 0 :(得分:2)

希望,这会有所帮助

对于JMenu,您可以使用MenuListener

class MenuListenerAdapter implements MenuListener {

    @Override
    public void menuSelected(MenuEvent e) {
        System.out.println("Menu Selected");
    }

    @Override
    public void menuDeselected(MenuEvent e) {
        System.out.println("Menu Deselected");
    }

    @Override
    public void menuCanceled(MenuEvent e) {
        System.out.println("Menu Canceled");
    }
}

然后在菜单

上添加MenuListener
menu.addMenuListener(new MenuListenerAdapter());

答案 1 :(得分:1)

您可以使用MouseListener, 在下面找到您的解决方案,当您点击学生和教师菜单

时查看控制台
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextField;

public class Searchdb extends JFrame implements ActionListener {

    //Initializing Components
    JLabel lb, lbd, lb1, lb2, lb3, lb5;

    JTextField tf1, tf2, tf3, tf5, tfd;

    JButton btn;

    JMenuBar menuBar = new JMenuBar();

    JMenu menu = new JMenu("Student");

    JMenu menu1 = new JMenu("Teacher");

    //Creating Constructor for initializing JFrame components
    Searchdb() {
        //Providing Title
        super("Fetching Roll Information");

        setJMenuBar(menuBar);
        menuBar.add(menu);
        menuBar.add(menu1);

        menu.addMouseListener(new MouseListenerForStudentAndTeacher());
        menu1.addMouseListener(new MouseListenerForStudentAndTeacher());

        lb5 = new JLabel("Roll Number:");
        lb5.setBounds(20, 20, 100, 20);
        tf5 = new JTextField(20);
        tf5.setBounds(130, 20, 200, 20);

        lbd = new JLabel("Date:");
        lbd.setBounds(20, 50, 100, 20);
        tfd = new JTextField(20);
        tfd.setBounds(130, 50, 200, 20);

        btn = new JButton("Submit");
        btn.setBounds(50, 50, 100, 20);
        btn.addActionListener(this);

        lb = new JLabel("Fetching Student Information From Database");
        lb.setBounds(30, 80, 450, 30);
        lb.setForeground(Color.black);
        lb.setFont(new Font("Serif", Font.PLAIN, 12));
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);

        lb1 = new JLabel("Name:");
        lb1.setBounds(20, 120, 100, 20);
        tf1 = new JTextField(50);
        tf1.setBounds(130, 120, 200, 20);
        lb2 = new JLabel("Fathername:");
        lb2.setBounds(20, 150, 100, 20);
        tf2 = new JTextField(100);
        tf2.setBounds(130, 150, 200, 20);
        lb3 = new JLabel("State:");
        lb3.setBounds(20, 180, 100, 20);
        tf3 = new JTextField(50);
        tf3.setBounds(130, 180, 200, 20);

        setLayout(null);

        //Add components to the JFrame
        add(lb5);
        add(tf5);
        add(lbd);
        add(tfd);
        add(btn);

        add(lb);
        add(lb1);
        add(tf1);
        add(lb2);
        add(tf2);
        add(lb3);
        add(tf3);

        //Set TextField Editable False
        tf1.setEditable(false);
        tf2.setEditable(false);
        tf3.setEditable(false);

    }

    public void actionPerformed(ActionEvent e) {

        // removed code, you can add your code later on
    }

    public static void main(String args[]) {

        new Searchdb();
    }

    private class MouseListenerForStudentAndTeacher extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {

            if (e.getSource() == menu) {
                System.out.println("Student Menu Clicked");
            }
            if (e.getSource() == menu1) {
                System.out.println("Teacher Menu Clicked");
            }

        }
    }
}