设置键盘焦点
Set keyboard focus
我目前正在学习有关如何创建猜谜游戏 GUI 应用程序的 java 教程。然而,在说明中的某一时刻,它说将键盘焦点设置到该字段;我不知道这意味着什么或如何去做。任何澄清将不胜感激。
下面是确切的说明:将用户的注意力集中在 Player 字段上:
将键盘焦点设置到该字段。
到目前为止,这是我的代码:
public class GOM extends JFrame implements ActionListener, KeyListener
{
Container content = this.getContentPane();
//top
JTextField theGuess = new JTextField(10);
JLabel bankroll = new JLabel("");
//bottom
JButton newplayer = new JButton("New Player");
JButton newnumber = new JButton("New Number");
JTextField thePlayer = new JTextField(20);
//center
JTextArea theoutput = new JTextArea("");
//invisible
String playerName;
int theNumber;
int numTries;
int numGames;
double amtRemaining;
Random randomizer()
{
Random rnd = new Random();
return rnd;
}
JScrollPane scrollArea = new JScrollPane(theoutput);
public GOM()
{
this.setVisible(true);
this.setSize(500,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Guess O'Matic");
//top panel
JPanel p1 = new JPanel();
p1.add(theGuess);
p1.add(bankroll);
p1.add(new JLabel("Make Your Guess"));
content.add(p1, BorderLayout.NORTH);
//bottom panel
JPanel p2 = new JPanel();
p2.add(newplayer);
p2.add(newnumber);
p2.add(thePlayer);
content.add(p2, BorderLayout.SOUTH);
// finishing touches
content.add(new JLabel(" "), BorderLayout.WEST);
content.add(new JLabel(" "), BorderLayout.EAST);
content.add(scrollArea, BorderLayout.CENTER);
newplayer.addActionListener(this);
newnumber.addActionListener(this);
thePlayer.addKeyListener(this);
theGuess.addKeyListener(this);
newPlayer();
}
public void newPlayer()
{
theoutput.setText(playerName);
theoutput.setEnabled(false);
theGuess.setEnabled(false);
newnumber.setEnabled(false);
newplayer.setEnabled(false);
theGuess.setBackground(Color.WHITE);
thePlayer.setEnabled(true);
thePlayer.setText(playerName);
thePlayer.setBackground(Color.YELLOW);
}
@Override
public void actionPerformed(ActionEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
}
}
如果您的 GUI 包含多个 JTextField 和可能的其他文本组件,则键盘焦点一次只能位于其中一个字段上。换句话说,如果您键入,只有一个字段可以显示插入符,然后通常会显示键入的文本。当显示 Swing GUI 时,GUI 必须决定哪个文本组件应该具有焦点,并且它使用其焦点遍历策略来决定这一点。默认策略通常会将焦点放在创建的第一个文本字段中。 您 可以通过在要保持焦点的文本组件上调用 requestFocusInWindow()
来更改此设置。
我目前正在学习有关如何创建猜谜游戏 GUI 应用程序的 java 教程。然而,在说明中的某一时刻,它说将键盘焦点设置到该字段;我不知道这意味着什么或如何去做。任何澄清将不胜感激。
下面是确切的说明:将用户的注意力集中在 Player 字段上: 将键盘焦点设置到该字段。
到目前为止,这是我的代码:
public class GOM extends JFrame implements ActionListener, KeyListener
{
Container content = this.getContentPane();
//top
JTextField theGuess = new JTextField(10);
JLabel bankroll = new JLabel("");
//bottom
JButton newplayer = new JButton("New Player");
JButton newnumber = new JButton("New Number");
JTextField thePlayer = new JTextField(20);
//center
JTextArea theoutput = new JTextArea("");
//invisible
String playerName;
int theNumber;
int numTries;
int numGames;
double amtRemaining;
Random randomizer()
{
Random rnd = new Random();
return rnd;
}
JScrollPane scrollArea = new JScrollPane(theoutput);
public GOM()
{
this.setVisible(true);
this.setSize(500,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Guess O'Matic");
//top panel
JPanel p1 = new JPanel();
p1.add(theGuess);
p1.add(bankroll);
p1.add(new JLabel("Make Your Guess"));
content.add(p1, BorderLayout.NORTH);
//bottom panel
JPanel p2 = new JPanel();
p2.add(newplayer);
p2.add(newnumber);
p2.add(thePlayer);
content.add(p2, BorderLayout.SOUTH);
// finishing touches
content.add(new JLabel(" "), BorderLayout.WEST);
content.add(new JLabel(" "), BorderLayout.EAST);
content.add(scrollArea, BorderLayout.CENTER);
newplayer.addActionListener(this);
newnumber.addActionListener(this);
thePlayer.addKeyListener(this);
theGuess.addKeyListener(this);
newPlayer();
}
public void newPlayer()
{
theoutput.setText(playerName);
theoutput.setEnabled(false);
theGuess.setEnabled(false);
newnumber.setEnabled(false);
newplayer.setEnabled(false);
theGuess.setBackground(Color.WHITE);
thePlayer.setEnabled(true);
thePlayer.setText(playerName);
thePlayer.setBackground(Color.YELLOW);
}
@Override
public void actionPerformed(ActionEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
}
}
如果您的 GUI 包含多个 JTextField 和可能的其他文本组件,则键盘焦点一次只能位于其中一个字段上。换句话说,如果您键入,只有一个字段可以显示插入符,然后通常会显示键入的文本。当显示 Swing GUI 时,GUI 必须决定哪个文本组件应该具有焦点,并且它使用其焦点遍历策略来决定这一点。默认策略通常会将焦点放在创建的第一个文本字段中。 您 可以通过在要保持焦点的文本组件上调用 requestFocusInWindow()
来更改此设置。