java这个程序中的按钮如何设置助记符为回车键,帮忙改动,谢谢。

2024-11-30 11:09:43
推荐回答(2个)
回答1:

你这个是UI登陆界面的吧, 给登陆按钮添高圆加个监缺念旅听器 就OK
如:
//给登陆按钮绑伏凳定监听器
登陆按钮.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
登陆的处理程序
}
}
);

回答2:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class KongJian2 extends JFrame implements ActionListener
{

JTextField jtf=new JTextField(10);
JPasswordField jpf=new JPasswordField(10);
JLabel jl1=new JLabel("用户名");
JLabel jl2=new JLabel("密码");

JLabel lab1=new JLabel("您好, 登录成功。");
JLabel lab2=new JLabel("对不起,你输入的帐号或密码有误,请重新登录。");

JButton jb=new JButton("提交");

JPanel jp=new JPanel();

public KongJian2()
{
this.setTitle("Java课程设计-钟冠新-李冠康制作");
jp.setLayout(null);
jl1.setBounds(30,20,80,30);
jp.add(jl1);
jl2.setBounds(30,70,80,30);
jp.add(jl2);
jtf.setBounds(80,20,200,30);
jp.add(jtf);
jpf.setBounds(80,70,200,30);
jp.add(jpf);
jb.setBounds(180,130,80,30);
jp.add(jb);
/********给密码框添加了键滑和盘监听 回车执行登陆**********/
jpf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==10) {//如果按了回车键
yanZheng(); //提出了你的点击登陆按钮的登录功能
}
}

});
/****************************************************/
jb.addActionListener(this);
this.add(jp);
this.setBounds(300,250,350,250);
this.setVisible(true);
}

public void yanZheng(){ //提毕让握出的登陆代码
String accunt = jtf.getText();
String password = new String(jpf.getPassword());
System.out.println("账号:"+accunt+" 密码:"+password);
if("201143502244".equals(accunt)&&"123".equals(password))
{
JOptionPane.showMessageDialog(jp, lab1);
}
else{
JOptionPane.showMessageDialog(jp, lab2);
}
jtf.setText("");
jpf.setText("手庆");

}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jb){
String accunt = jtf.getText();
String password = new String(jpf.getPassword());
if("201143502244".equals(accunt)&&"123".equals(password))
{
JOptionPane.showMessageDialog(jp, lab1);
}
else{
JOptionPane.showMessageDialog(jp, lab2);
}
jtf.setText("");
jpf.setText("");
}
}

public static void main(String atgs[])
{
KongJian2 kj=new KongJian2();
}

}