java这个程序中的按钮如何设置助记符为回车键,帮忙改动,谢谢。
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassKongJian2exten...
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);
jb.addActionListener(this);
this.add(jp);
this.setBounds(300,250,350,250);
this.setVisible(true);
}
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();
}
} 展开
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);
jb.addActionListener(this);
this.add(jp);
this.setBounds(300,250,350,250);
this.setVisible(true);
}
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();
}
} 展开
2个回答
展开全部
你这个是UI登陆界面的吧, 给登陆按钮添加个监听器 就OK
如:
//给登陆按钮绑定监听器
登陆按钮.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
登陆的处理程序
}
}
);
如:
//给登陆按钮绑定监听器
登陆按钮.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
登陆的处理程序
}
}
);
追问
你在看清楚点,这个按钮的监听器方法,是有的,但不会改动。
追答
String password = new String(jpf.getPassword());
这句能得到密码么?
你用system.out.println(accunt +password );控制台输出看看能得到输入的信息不
现在是监听器没运行 还是运行了没结果
展开全部
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();
}
}
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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询