1

我正在使用 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;
}
4

3 回答 3

0

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

顺便说一句fontField.setEditable(true);fontField.setFocusable(true);默认情况下不是必需的,它们设置为 true。

于 2013-05-06T05:09:16.657 回答
0

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

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

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

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

fontField.addActionListener(this);

a 上的 ActionListenerJTextField什么也不做。尝试这样做:

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
    }
});
于 2013-05-06T05:22:55.863 回答
0

正如@andrew-thompson 指出的那样,问题是awt 和 swing 的混合。我想 GComponent 不知何故覆盖了 Swing 组件 JTextField ,这使得它无法访问。因此,解决方法是为 JTextField 创建 JPanel 和 KeyListener,如 @marco 所述。这是有效的代码: 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;

}

谢谢 :)

于 2013-05-06T09:43:31.210 回答