2
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

public class Loginpanel extends JFrame {

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField usr;
    private JPasswordField pass;
    private JButton submit;
    private JRadioButton admin;
    private JRadioButton student;
    private ButtonGroup type;

    public Loginpanel() 
    {
        super("User Login");
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( 350, 200 ); // set frame size

        FieldHandler handler = new FieldHandler();


        setLayout(new GridBagLayout());
        setResizable(false);

        GridBagConstraints c = new GridBagConstraints();

        label3 = new JLabel();
        label3.setText("Login");
        c.weightx =0;
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.VERTICAL;
        add(label3,c);

        label1 = new JLabel("Username:");
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(label1,c);

        usr = new JTextField(10);
        c.weightx = 0;
        c.gridx = 2;
        c.gridy = 2;
        c.ipadx = 2;

        add(usr,c);

        label2 = new JLabel("Password:");
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(label2,c);

        pass = new JPasswordField(10);
        c.weightx = 0;
        c.gridx = 2;
        c.gridy = 3;
        c.ipadx= 2;
        add(pass,c);

        admin = new JRadioButton( "Admin", true );
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 4;
        add(admin,c);

        student = new JRadioButton( "Student", false );
        c.weightx = 0;
        c.gridx = 1;
        c.gridy = 4;
        add(student,c);

        submit = new JButton("Submit");
        c.weightx = 0;
        c.gridx = 1;
        c.gridy = 5;
        add(submit,c);

        type = new ButtonGroup();
        type.add(admin);
        type.add(student);

        usr.addActionListener( handler );
        pass.addActionListener( handler );
        submit.addActionListener(handler);

    }

    class FieldHandler implements ActionListener{

        public void actionPerformed(ActionEvent event)  {
            String string ="";
            if(event.getSource() == usr){
                string = String.format("%s",event.getActionCommand());

            }
            else if(event.getSource() == pass){
                string = String.format("%s",event.getActionCommand());

            }
            else if(event.getSource() == submit){
                string = "Submit Button";

            }
            JOptionPanel.showMessageDialog( null, string );
        }

    }

}

我正在尝试用 Java 制作一个在线考试系统,而且我是该语言的新手。所以我制作了简洁的表单,带有文本字段、单选按钮和提交按钮。我检查了事件处理函数,我现在几乎被卡住了。事件处理程序调用对其进行操作的元素。这很好,但我找不到获取每个元素的数据的方法。event.getActionCommand() 仅获取调用处理程序的元素的信息。另一件事是我不确定在成功获取数据库结果后如何更改框架,如果密码不匹配或找不到这样的用户,那么它只会显示一条错误消息(我知道它将由 Joptionpanel 完成)并返回。

具有主要功能的类是

import java.awt.GridBagLayout;
import javax.swing.JFrame;

public class Start {

    public static void main(String args[]) {
        Loginpanel login = new Loginpanel();
        login.setVisible( true ); // display frame
    }

}
4

2 回答 2

2

FieldHandler 是 LoginPanel 的内部类。因此它可以访问 LoginPanel 的所有字段:

class FieldHandler implements ActionListener{

    public void actionPerformed(ActionEvent event)  {
        if (event.getSource() == submit){
            String user = usr.getText(); 
            // or String user = LoginPanel.this.usr.getText(); 
            char[] password = pass.getPassword();
            // do whatever you want with the user and password
        }
        ...
    }

}

在Java 教程中了解嵌套类(顺便说一下,它还深入介绍了 Swing)。

为每个侦听器使用单独的类而不是具有长链 if 块测试事件来自何处的唯一类是一个好习惯。

于 2013-07-18T06:58:52.707 回答
0

必须先设置一个动作命令,setActionCommand()然后才能请求它

于 2013-07-18T06:58:14.663 回答