我有一个简单的登录屏幕。我希望UserName和Password显示在第一个和第二个文本字段中,直到它们被单击为止。此功能正常运行。但是,第一个文本字段始终在应用程序启动时聚焦,因此显示为“”,直到它失去焦点。我试图设置一个默认按钮,请求焦点无济于事。看起来按钮正确默认,但由于某种原因它没有获得焦点。任何人都知道如何解决这个问题?
public class Basics implements ActionListener{
private JFrame frmBasics;
private JTextField userNameFeild;
private JTextField passwordFeild;
private JButton btnSignIn;
private JButton btnSignUp;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Basics window = new Basics();
window.frmBasics.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}//end main
/**
* Create the application.
*/
public Basics() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmBasics = new JFrame();
frmBasics.setTitle("Welcome to the POOPalace!!!");
frmBasics.setBounds(100, 100, 511, 344);
frmBasics.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmBasics.getContentPane().setLayout(null);
userNameFeild = new JTextField("UserName");
userNameFeild.setBounds(148, 79, 214, 20);
userNameFeild.addFocusListener(new FocusedClass());
frmBasics.getContentPane().add(userNameFeild);
userNameFeild.setColumns(10);
passwordFeild = new JTextField("Password");
passwordFeild.setBounds(148, 126, 214, 20);
passwordFeild.addFocusListener(new FocusedClass());
frmBasics.getContentPane().add(passwordFeild);
passwordFeild.setColumns(10);
btnSignIn = new JButton("Sign In");
btnSignIn.setBounds(148, 182, 89, 23);
btnSignIn.addActionListener(this);
frmBasics.getContentPane().add(btnSignIn);
btnSignUp = new JButton("Sign Up");
btnSignUp.setBounds(273, 182, 89, 23);
btnSignUp.addActionListener(this);
frmBasics.getContentPane().add(btnSignUp);
//from what I've been reading these 2 lines should be the solution
//but the request focus seems to not be working
frmBasics.getRootPane().setDefaultButton(btnSignIn);;
btnSignIn.requestFocus();
}
@Override
public void actionPerformed(ActionEvent e) {
//frmBasics.getContentPane().removeAll();
//frmBasics.repaint();
System.out.println(userNameFeild.getText());
System.out.println(passwordFeild.getText());
}//actionPerformed
private class FocusedClass implements FocusListener {
@Override
public void focusGained(FocusEvent arg0) {
if(arg0.getSource().equals(userNameFeild) && userNameFeild.getText().compareTo("UserName") == 0){
userNameFeild.setText("");
}
if(arg0.getSource().equals(passwordFeild) && passwordFeild.getText().compareTo("Password") == 0){
passwordFeild.setText("");
}
}
@Override
public void focusLost(FocusEvent arg0) {
if(userNameFeild.getText().compareTo("") == 0){
userNameFeild.setText("UserName");
}
if(passwordFeild.getText().compareTo("") == 0){
passwordFeild.setText("Password");
}
frmBasics.getContentPane().repaint();
}
}
}//class
答案 0 :(得分:1)
请求焦点仅在窗口布局完成后才起作用。
我们需要在三种特定情况之一中调用requestFocusInWindow()
:
windowOpened()
方法中。 EventQueue's invokeLater()
中,将在处理完所有待处理事件后运行。JFrame's
setVisible()
方法中。第一个选项:
//btnSignIn.requestFocusInWindow();
frmBasics.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
btnSignIn.requestFocusInWindow();
}
});
另请注意,requestFocusInWindow()
比requestFocus()
更便携
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.notification
{
background-color:#006699;
min-height:40px;
width:30%;
margin:0 auto;
text-align:center;
line-height:50px;
color:#fff;
font-size:18px;
box-shadow: 10px 10px 5px #888888;
}
</style>
<script src="google.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$(".btn1").click(function () {
$("#dvMsg").fadeOut(5000, 0, 0);
});
});
</script>
</head>
<body>
<form id="form1">
<div id="dvMsg" class="notification" runat="server" visible="true">
<asp:Label ID="lblMsg" runat="server" Text="This is just "></asp:Label>
</div>
<button class="btn1">Fade out</button>
</form>
</body>
</html>
。