在 JTextField 中删除不适用于新的 BasicLookAndFeel
Delete inside JTextField does not work with a new BasicLookAndFeel
如果您为应用程序应用新的(基本)LookAndFeel,我很惊讶地看到 JTextField 停止响应删除操作。
下面是完整的代码:
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;
import java.awt.*;
import java.util.logging.Logger;
public class App2 {
private static final Logger log = Logger.getLogger(App2.class.getCanonicalName());
public static void main(String args[]){
setUI();
JFrame f = new JFrame();
JTextField tf = new JTextField();
f.add(tf);
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
static void setUI(){
try {
javax.swing.UIManager.setLookAndFeel(new DummyLookAndFeel());
} catch ( UnsupportedLookAndFeelException ex) {
log.warning("Setting UI not ok : " + ex.getMessage());
}
}
}
class DummyLookAndFeel extends BasicLookAndFeel {
@Override
protected void initComponentDefaults(UIDefaults table) {
table.put("TextField.background", new ColorUIResource(Color.RED));
}
@Override
public String getName() {
return "Just a fancy name";
}
@Override
public String getID() {
return "Something unique";
}
@Override
public String getDescription() {
return "A very very long description";
}
@Override
public boolean isNativeLookAndFeel() {
return false;
}
@Override
public boolean isSupportedLookAndFeel() {
return true;
}
}
知道为什么会这样吗?
似乎如果我将 initComponentDefaults(...) 更改为
protected void initComponentDefaults(UIDefaults table) {
super.initComponentDefaults(table);
Object fim = table.get("TextField.focusInputMap");
Object fieldInputMap = new UIDefaults.LazyInputMap(new Object[] {
"ctrl C", DefaultEditorKit.copyAction,
"ctrl V", DefaultEditorKit.pasteAction,
"ctrl X", DefaultEditorKit.cutAction,
"COPY", DefaultEditorKit.copyAction,
"PASTE", DefaultEditorKit.pasteAction,
"CUT", DefaultEditorKit.cutAction,
"control INSERT", DefaultEditorKit.copyAction,
"shift INSERT", DefaultEditorKit.pasteAction,
"shift DELETE", DefaultEditorKit.cutAction,
"shift LEFT", DefaultEditorKit.selectionBackwardAction,
"shift KP_LEFT", DefaultEditorKit.selectionBackwardAction,
"shift RIGHT", DefaultEditorKit.selectionForwardAction,
"shift KP_RIGHT", DefaultEditorKit.selectionForwardAction,
"ctrl LEFT", DefaultEditorKit.previousWordAction,
"ctrl KP_LEFT", DefaultEditorKit.previousWordAction,
"ctrl RIGHT", DefaultEditorKit.nextWordAction,
"ctrl KP_RIGHT", DefaultEditorKit.nextWordAction,
"ctrl shift LEFT", DefaultEditorKit.selectionPreviousWordAction,
"ctrl shift KP_LEFT", DefaultEditorKit.selectionPreviousWordAction,
"ctrl shift RIGHT", DefaultEditorKit.selectionNextWordAction,
"ctrl shift KP_RIGHT", DefaultEditorKit.selectionNextWordAction,
"ctrl A", DefaultEditorKit.selectAllAction,
"HOME", DefaultEditorKit.beginLineAction,
"END", DefaultEditorKit.endLineAction,
"shift HOME", DefaultEditorKit.selectionBeginLineAction,
"shift END", DefaultEditorKit.selectionEndLineAction,
"BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
"shift BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
"ctrl H", DefaultEditorKit.deletePrevCharAction,
"DELETE", DefaultEditorKit.deleteNextCharAction,
"ctrl DELETE", DefaultEditorKit.deleteNextWordAction,
"ctrl BACK_SPACE", DefaultEditorKit.deletePrevWordAction,
"RIGHT", DefaultEditorKit.forwardAction,
"LEFT", DefaultEditorKit.backwardAction,
"KP_RIGHT", DefaultEditorKit.forwardAction,
"KP_LEFT", DefaultEditorKit.backwardAction,
"ENTER", JTextField.notifyAction,
"ctrl BACK_SLASH", "unselect"/*DefaultEditorKit.unselectAction*/,
"control shift O", "toggle-componentOrientation"/*DefaultEditorKit.toggleComponentOrientation*/
});
table.put("TextField.focusInputMap", fieldInputMap);
table.put("TextField.background", new ColorUIResource(Color.RED));
}
JTextField 似乎可以正常工作。似乎 UIDefaults 没有 "TextField.focusInputMap" 的条目(在示例中 fim 对象为空)
如果您为应用程序应用新的(基本)LookAndFeel,我很惊讶地看到 JTextField 停止响应删除操作。
下面是完整的代码:
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;
import java.awt.*;
import java.util.logging.Logger;
public class App2 {
private static final Logger log = Logger.getLogger(App2.class.getCanonicalName());
public static void main(String args[]){
setUI();
JFrame f = new JFrame();
JTextField tf = new JTextField();
f.add(tf);
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
static void setUI(){
try {
javax.swing.UIManager.setLookAndFeel(new DummyLookAndFeel());
} catch ( UnsupportedLookAndFeelException ex) {
log.warning("Setting UI not ok : " + ex.getMessage());
}
}
}
class DummyLookAndFeel extends BasicLookAndFeel {
@Override
protected void initComponentDefaults(UIDefaults table) {
table.put("TextField.background", new ColorUIResource(Color.RED));
}
@Override
public String getName() {
return "Just a fancy name";
}
@Override
public String getID() {
return "Something unique";
}
@Override
public String getDescription() {
return "A very very long description";
}
@Override
public boolean isNativeLookAndFeel() {
return false;
}
@Override
public boolean isSupportedLookAndFeel() {
return true;
}
}
知道为什么会这样吗?
似乎如果我将 initComponentDefaults(...) 更改为
protected void initComponentDefaults(UIDefaults table) {
super.initComponentDefaults(table);
Object fim = table.get("TextField.focusInputMap");
Object fieldInputMap = new UIDefaults.LazyInputMap(new Object[] {
"ctrl C", DefaultEditorKit.copyAction,
"ctrl V", DefaultEditorKit.pasteAction,
"ctrl X", DefaultEditorKit.cutAction,
"COPY", DefaultEditorKit.copyAction,
"PASTE", DefaultEditorKit.pasteAction,
"CUT", DefaultEditorKit.cutAction,
"control INSERT", DefaultEditorKit.copyAction,
"shift INSERT", DefaultEditorKit.pasteAction,
"shift DELETE", DefaultEditorKit.cutAction,
"shift LEFT", DefaultEditorKit.selectionBackwardAction,
"shift KP_LEFT", DefaultEditorKit.selectionBackwardAction,
"shift RIGHT", DefaultEditorKit.selectionForwardAction,
"shift KP_RIGHT", DefaultEditorKit.selectionForwardAction,
"ctrl LEFT", DefaultEditorKit.previousWordAction,
"ctrl KP_LEFT", DefaultEditorKit.previousWordAction,
"ctrl RIGHT", DefaultEditorKit.nextWordAction,
"ctrl KP_RIGHT", DefaultEditorKit.nextWordAction,
"ctrl shift LEFT", DefaultEditorKit.selectionPreviousWordAction,
"ctrl shift KP_LEFT", DefaultEditorKit.selectionPreviousWordAction,
"ctrl shift RIGHT", DefaultEditorKit.selectionNextWordAction,
"ctrl shift KP_RIGHT", DefaultEditorKit.selectionNextWordAction,
"ctrl A", DefaultEditorKit.selectAllAction,
"HOME", DefaultEditorKit.beginLineAction,
"END", DefaultEditorKit.endLineAction,
"shift HOME", DefaultEditorKit.selectionBeginLineAction,
"shift END", DefaultEditorKit.selectionEndLineAction,
"BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
"shift BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
"ctrl H", DefaultEditorKit.deletePrevCharAction,
"DELETE", DefaultEditorKit.deleteNextCharAction,
"ctrl DELETE", DefaultEditorKit.deleteNextWordAction,
"ctrl BACK_SPACE", DefaultEditorKit.deletePrevWordAction,
"RIGHT", DefaultEditorKit.forwardAction,
"LEFT", DefaultEditorKit.backwardAction,
"KP_RIGHT", DefaultEditorKit.forwardAction,
"KP_LEFT", DefaultEditorKit.backwardAction,
"ENTER", JTextField.notifyAction,
"ctrl BACK_SLASH", "unselect"/*DefaultEditorKit.unselectAction*/,
"control shift O", "toggle-componentOrientation"/*DefaultEditorKit.toggleComponentOrientation*/
});
table.put("TextField.focusInputMap", fieldInputMap);
table.put("TextField.background", new ColorUIResource(Color.RED));
}
JTextField 似乎可以正常工作。似乎 UIDefaults 没有 "TextField.focusInputMap" 的条目(在示例中 fim 对象为空)