我有多个文本字段和按钮。选择文本字段时,应在按下按钮时向其添加文本,但不会插入任何字母代码。我错过了什么。 谢谢你的帮助。
public class ButtonExample_Extended extends JFrame implements ActionListener {
public JPanel createContentPane (){
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 50);
buttonPanel.setSize(1370, 770);
totalGUI.add(buttonPanel);
B9 = new JButton("9");
B9.setLocation(1190, 570);
B9.setSize(50, 50);
B9.addActionListener(this);
buttonPanel.add(B9);
JPasswordField passwordField = new JPasswordField(20);
passwordField.setLocation(900,565);
passwordField.setSize(120,30);
buttonPanel.add(passwordField);
}
private JTextComponent selectedTextField;
// TextFields onFocus event
private void a33FocusGained(java.awt.event.FocusEvent evt) {
selectedTextField = (JTextComponent) evt.getSource();
}
// action for button
public void actionPerformed (ActionEvent evt) {
if (evt.getSource() == B9)
selectedTextField.setText( selectedTextField.getText() + "9" );
}
}
使用上面的代码我希望将9插入textPasswordField,但它没有。
答案 0 :(得分:2)
你确定吗
private void a33FocusGained(java.awt.event.FocusEvent evt) {
selectedTextField = (JTextComponent) evt.getSource();
}
有史以来?我想你的类应该实现FocusListener并添加像
这样的东西passwordField.addFocusListener(this);
@Override
public void focusGained(FocusEvent e) {
selectedTextField = (JTextComponent) e.getSource();
}
@Override
public void focusLost(FocusEvent e) {
selectedTextField = null;
}
这是你应该做的事情的示例代码(如果我理解正确的话),请注意,首先你需要将光标设置到密码字段,之后按钮将起作用,但你可以看到不好的一面这种方法在focusLost方法中
public class Snippet implements ActionListener, FocusListener {
public JFrame totalGUI = new JFrame();
private JPanel buttonPanel;
private JButton B9;
public Snippet() {
createContentPane();
}
public void createContentPane() {
buttonPanel = new JPanel(new GridBagLayout());
B9 = new JButton("9");
B9.addActionListener(this);
buttonPanel.add(B9);
JPasswordField passwordField = new JPasswordField(20);
passwordField.setSize(120, 30);
passwordField.addFocusListener(this);
buttonPanel.add(passwordField);
totalGUI.getContentPane().add(buttonPanel);
totalGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
totalGUI.pack();
}
private JTextComponent selectedTextField;
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == B9 && selectedTextField != null)
selectedTextField.setText(selectedTextField.getText() + "9");
}
public static void main(String[] args) {
new Snippet().totalGUI.setVisible(true);
}
@Override
public void focusGained(FocusEvent e) {
if(e.getSource() instanceof JTextComponent)
selectedTextField = (JTextComponent) e.getSource();
}
@Override
public void focusLost(FocusEvent e) {
// when you push the button the text field will lose focus
// selectedTextField = null;
}
}
答案 1 :(得分:0)
不要同时使用FocusListener和ActionListener。这假定事件将按特定顺序触发,即首先是focusGained,然后是actionPerformed。 Swing不保证事件的顺序。
相反,您可以扩展TextAction
。 TextAction是一个特殊的Action,用于Swing组件,因为它跟踪最后一个具有焦点的组件。例如,要创建一个选择所有文本的Action:
class SelectAll extends TextAction
{
public SelectAll()
{
super("Select All");
}
public void actionPerformed(ActionEvent e)
{
JTextComponent component = getFocusedComponent();
component.selectAll();
component.requestFocusInWindow();
}
}
然后使用您要执行的操作:
b9.addActionListener( new SelectAll() );