java,swing,如何为“执行”按钮添加enter快捷键》???
2015-08-28
展开全部
// 回答完毕,采纳即可
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class YuGiOh extends JFrame
{
private static final long serialVersionUID = 1L;
private static final int WIDTH = 300;
private static final int HEIGHT = 70;
public YuGiOh()
{
setTitle("登陆");
setSize(WIDTH, HEIGHT);
setResizable(false);
setAlwaysOnTop(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private YuGiOh addComponents()
{
JPanel panel = new JPanel();
JTextField txt = new JTextField(13);
JButton login = new JButton("登陆");
JButton register = new JButton("注册");
login.registerKeyboardAction(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("login");
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
panel.add(txt);
panel.add(login);
panel.add(register);
add(panel);
return this;
}
public static void main(String[] args) throws IOException
{
new YuGiOh().addComponents().setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询