gpt4 book ai didi

java - 用 Java 制作表单

转载 作者:行者123 更新时间:2023-11-30 09:19:51 24 4
gpt4 key购买 nike

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
}

}

最佳答案

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
}
...
}

}

the Java tutorial 中了解嵌套类(顺便说一句,它还深入介绍了 Swing)。

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

关于java - 用 Java 制作表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17716405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com