用Java制作表单

用Java制作表单,java,swing,jdbc,Java,Swing,Jdbc,我正在尝试用Java制作一个在线考试系统,我对这门语言还不熟悉。因此,我制作了整洁的表单,包含文本字段、单选按钮和提交按钮。 我检查了事件处理函数,现在我几乎被卡住了。事件处理程序调用对其进行操作的元素。这很好,但我无法找到一种方法来获取每个元素的数据。getActionCommand()只获取调用处理程序的元素的信息。 另一件事是,我不确定在成功获取数据库结果后如何更改帧,如果密码不匹配或没有找到这样的用户,那么它只会显示一条错误消息(我知道这将由Joptionpanel完成)并返回 具有主要

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

具有主要功能的类是

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 );
        }

    }

}

必须先使用
setActionCommand()
设置actioncommand,然后才能请求它

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

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
    }

}
了解中的嵌套类(顺便说一句,它还包括Swing-in-depth)


最好为每个侦听器使用单独的类,而不是一个具有长链if块的唯一类来测试事件的来源。

您希望从元素中获取哪些数据?如果您想获取用户输入数据,可以像“If(event.getSource()instanceof JTextField){JTextField tf=(JTextField)event.getSource();String content=tf.getText();}”这样做,但我建议您不要使用FieldHandler类,在继续之前只使用usr.addActionListener(new ActionListener(){@override ActionPerformed(){}),在
JPasswordField
上执行
setActionCommand(…)
,因为它中的密码可以通过本文引用的
String
来访问,出于安全原因,这不是一个好方法:-)此外,不要像您这样访问密码,访问该线程以获取更多信息:-)
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
        }
        ...
    }

}