无法在JTextfield中键入或删除文本

时间:2013-05-06 04:59:26

标签: java swing actionlistener jtextfield acm-java-libraries

我正在使用Java的艺术和科学(使用Java SE 6u45)学习Java。尝试通过在JTextField中输入新的字体类型来更改文本字体。但问题是我无法在JTextField中输入任何文本。这个问题在我使用的其他摆动组件中很常见,如JButton,JCheckBox。但是在后面的组件中我可以看到选择的效果,即使视觉选择保持不变,这意味着即使在单击之后复选框仍保持检查但代码显示未选中框的结果。

但是在JTextField的情况下,甚至没有显示效果。我也不能删除我放在JTextField中的测试文本。试图使用isEditable()grabFocus()isFocusable()。它可能是一个Java错误吗?

/**
* Example 10.9
 * 
 * This program prints the given text in the font inputted by the user in JTextField
 */
package ASJ_Examples;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JTextField;

import acm.graphics.GLabel;
import acm.program.GraphicsProgram;

public class FontSampler extends GraphicsProgram implements ActionListener{
/**
 * Eclispe Generated
 */
private static final long serialVersionUID = -5734136235409079420L;
private static final String TEST_STRING = "This is a test";
private static final double LEFT_MARGIN = 3;
private static final int MAX_FONT_NAME = 10;

public void init(){
    addJFontLabel();
    addJFontTextField();
    lastY = 0;
    addGLabel(); 
}
/**
 * Adds a text field to enter the required font
 */
private void addJFontTextField() {
    String test = "new";
    fontField = new JTextField(test, MAX_FONT_NAME);    //added to see if Jtextfiled is responding 
//      fontField.setEnabled(true);
//      fontField.setEditable(true);
    fontField.addActionListener(this);
    //added these to give focus to jtextfield but no effect
    fontField.isEditable();
    fontField.grabFocus();
    fontField.isFocusable();
    //add to window
    add(fontField, SOUTH);

}
/**
 * Adds JFontLAbel to denote the text input field
 */
private void addJFontLabel() {
    add(new JLabel("Font"), SOUTH);

}

/**
 * Adds the test label to canvas
 */
private void addGLabel() {
    lastLabel = new GLabel(TEST_STRING);
    add(lastLabel, 20, 20);

}

public void ActionPerformed(ActionEvent e){
    if(e.getSource() == fontField){
        GLabel label = new GLabel(TEST_STRING);
        label.setFont(lastLabel.getFont()); //to display the text even if the suer entered a non-allowed font
        label.setFont(fontField.getText()); //change the font to u4ser demanded font
        addGlabel(label);
        lastLabel = label;
    }
}
/**
 *adds a Glabel on the next line adjusting for heights
 * @param label
 */
private void addGlabel(GLabel label) {
    lastY += label.getHeight();
    lastY += lastLabel.getDescent() - label.getDescent();
    add(label, LEFT_MARGIN, lastY);

}
/**
 * JTextField to enter font
 */
private JTextField fontField;
/**
 * GLabel which is being worked on
 */
private GLabel lastLabel;
/**
 * 
 */
private double lastY;
}

3 个答案:

答案 0 :(得分:0)

尝试使用fontField.requestFocus();代替fontField.grabFocus();fontField.setEditable(true);代替fontField.isEditable(); fontField.setFocusable(true);代替fontField.isFocusable();

btw fontField.setEditable(true);fontField.setFocusable(true);默认情况下不需要设置为true。

答案 1 :(得分:0)

  

但是在后面的组件中我可以看到选择的效果,即使视觉选择保持不变,这意味着即使点击后复选框仍保持检查,但代码显示未选中框的结果。

这听起来很奇怪。也许GraphicsProgram班级正在做一些顽皮的事情。但不能告诉它的代码。

fontField.isEditable();
fontField.grabFocus();
fontField.isFocusable();

如果字段可编辑,则第一个返回布尔值(默认情况下)。 客户端程序不应使用第二个,而是使用fontField.requestFocusInWindow()。如果字段是可聚焦的,则第三个返回布尔值(默认情况下)。

fontField.addActionListener(this);

JTextField上的ActionListener什么都不做。试着这样做:

fontfield.getDocument().addDocumentListener(new DocumentListener() {

    @Override
    public void removeUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub  
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
    }
});

答案 2 :(得分:0)

正如@ andrew-thompson指出的那样,问题是mix of awt and swing。我想某种程度上GComponent覆盖了Swing组件JTextField,这使得它无法访问。因此,解决方法是按照@marco的说明为JTextField创建JPanel和KeyListener。 这是有效的代码:   SSCCE代码

 /**
 * 
 * This program prints the given text in the font inputted by the user in JTextField
 */

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.JTextField;

import acm.graphics.GLabel;
import acm.program.GraphicsProgram;

public class FontSampler extends GraphicsProgram implements KeyListener{
private static final String TEST_STRING = "This is a test";
private static final double LEFT_MARGIN = 3;
private static final int MAX_FONT_NAME = 10;

public void init(){
    initPanel();    //init panel
    addJFontTextField();    //adds fontField to enter the font type
    addGLabel();    //adds default label to GCanvas
    lastY = 0;  //default y offset for post-processed label
    add(panel); //adds panel to canvas
}

/**
 * initialises panel 
 */
private void initPanel() {
    panel = new JPanel();
    panel.setLayout(new BorderLayout());

}

/**
 * Adds a text field to enter the required font
 */
private void addJFontTextField() {
    fontField = new JTextField( MAX_FONT_NAME);//added to see if Jtextfiled is responding
    panel.add(fontField, BorderLayout.SOUTH);
    fontField.addKeyListener(this); //adds key listener
}

/**
 * Adds the test label to canvas
 */
private void addGLabel() {
    lastLabel = new GLabel(TEST_STRING);
    add(lastLabel, 20, 20);

}

    /**
     * Called when any key is pressed
     */
public void keyPressed(KeyEvent e){
    if(e.getKeyCode() == KeyEvent.VK_ENTER){//check for enter key pressed
        GLabel label = new GLabel(TEST_STRING);
        label.setFont(lastLabel.getFont()); 
        label.setFont(fontField.getText());
        addGlabel(label);
        lastLabel = label;
    }
}

/**
 *adds a Glabel on the next line adjusting for heights
 * @param label
 */
private void addGlabel(GLabel label) {
    lastY += label.getHeight();
    lastY += lastLabel.getDescent() - label.getDescent();
    add(label, LEFT_MARGIN, lastY);
}

/**
 * JTextField to enter font
 */
private JTextField fontField;
/**
 * GLabel which is being worked on
 */
private GLabel lastLabel;
/**
 * 
 */
private double lastY;

private JPanel panel;

}

谢谢:)