gpt4 book ai didi

java - 如何禁用文本字段的自动聚焦

转载 作者:行者123 更新时间:2023-11-30 03:25:40 24 4
gpt4 key购买 nike

当我创建一堆 JTextFields 时,我看到第一个被选中。我想取消选择它,因为我有焦点监听器并且它会自动运行。
有什么线索吗?

SSCCE:

JTextField tf = new JTextField("hello");
tf.setForeground(Color.decode("0x8C8C8C")); // for nice comment inside the text field
textFieldKwotaWplacona.addFocusListener(new FocusListener() {

@Override
public void focusGained(FocusEvent e)
{

if(tf.getForeground() != Color.BLACK)
{
tf.setText("");
tf.setForeground(Color.BLACK);
}
} @Override
public void focusLost(FocusEvent arg0) {}});
//for deleting "nice comment" after click

tf.setBounds(//some bounds);
add(tf);

对另一个文本字段重复该过程

编辑2:实际代码(我相信它的 sscce :P)

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class Main extends JFrame implements ActionListener
{
JTextField textFieldKwotaWplacona, textFieldOprocentowanie, textFieldDlugoscLokaty, textFieldKwotaOtrzymana;

Main()
{ setSize(500,300);
setLayout(null);
setTitle("Program do liczenia procentu składanego");
setDefaultCloseOperation(EXIT_ON_CLOSE);


textFieldKwotaWplacona = new JTextField("Ilość pieniędzy wpłaconych");
textFieldKwotaWplacona.setForeground(Color.decode("0x8C8C8C"));
textFieldKwotaWplacona.addActionListener(this);
textFieldKwotaWplacona.addFocusListener(new FocusListener() {

@Override
public void focusGained(FocusEvent e)
{

if(textFieldKwotaWplacona.getForeground() != Color.BLACK)
{
textFieldKwotaWplacona.setText("");
textFieldKwotaWplacona.setForeground(Color.BLACK);
}
} @Override
public void focusLost(FocusEvent arg0) {}});

textFieldKwotaWplacona.setBounds(10, 10, 100, 20);
add(textFieldKwotaWplacona);

textFieldOprocentowanie = new JTextField("Oprocentowanie");
textFieldOprocentowanie.setForeground(Color.decode("0x8C8C8C"));
textFieldOprocentowanie.addActionListener(this);


textFieldOprocentowanie.addFocusListener(new FocusListener() {

@Override
public void focusGained(FocusEvent e)
{

if(textFieldOprocentowanie.getForeground() != Color.BLACK)
{
textFieldOprocentowanie.setText("");
textFieldOprocentowanie.setForeground(Color.BLACK);
}
}

@Override
public void focusLost(FocusEvent arg0) {}});
textFieldOprocentowanie.setBounds(10, 40, 100, 20);
add(textFieldOprocentowanie);



}




@Override
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
}

public static void main(String[] args)
{
Main a=new Main();
a.setVisible(true);


}
}

我想将焦点设置到窗口或其他东西,以防止文本消失。

最佳答案

正如评论中所讨论的,我添加了一个单选按钮来获取焦点:

public class Main extends JFrame {

JTextField textFieldKwotaWplacona, textFieldOprocentowanie;

Main() {

setTitle("Program do liczenia procentu składanego");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());

textFieldKwotaWplacona = new JTextField("Ilość pieniędzy wpłaconych");
textFieldKwotaWplacona.setForeground(Color.decode("0x8C8C8C"));
textFieldKwotaWplacona.addFocusListener(new FieldFocusListener(textFieldKwotaWplacona));
add(textFieldKwotaWplacona);

textFieldOprocentowanie = new JTextField("Oprocentowanie");
textFieldOprocentowanie.setForeground(Color.decode("0x8C8C8C"));
textFieldOprocentowanie.addFocusListener(new FieldFocusListener(textFieldOprocentowanie));
add(textFieldOprocentowanie);

JRadioButton btn = new JRadioButton("text");
add(btn);

pack();
btn.requestFocusInWindow();
}

private class FieldFocusListener extends FocusAdapter {

private JTextField field;

FieldFocusListener(JTextField field) {

this.field = field;
}

@Override
public void focusGained(FocusEvent e) {

if (field.getForeground() != Color.BLACK) {
field.setText("");
field.setForeground(Color.BLACK);
}
}
}

public static void main(String[] args) {

Main a = new Main();
a.setVisible(true);
}
}

说明

来自tutorial :

If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed.

这意味着 btn.requestFocusInWindow() 必须出现在 pack() 之后和 a.setVisible(true) 之前。

您需要另一个组件来获取焦点的原因是,当窗口获得焦点时,窗口内的组件必须获得焦点。

注释:

  • 如果您想要更好的文本字段提示,请参阅 @camickr's answer .
  • 不要使用null布局。选择适合您的 GUI 设计的一个(我选择 FlowLayout 只是因为它使用起来很快,尽管可能不是您需要的)。
  • 在添加所有组件后,pack() 不再设置框架的大小。
  • 不必为每个文本字段创建相同的焦点监听器,只需将其创建为类并重用即可。我展示了一种将组件传递给构造函数的方法,但您可以摆脱它并使用 e.getComponent() 来获取文本字段实例。

关于java - 如何禁用文本字段的自动聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30309172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com