Jtextfield中如何屏蔽键盘输入?还有如何消除它的光标显示? 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 从类 javax.swing.text.JTextComponent 继承的方法:setEditable,设置成false就不可编辑了。下次还是自己查API吧老兄。 对应不同的情况有不同的实现,setEditable的话还是可以有光标的哦 我想要这个Jtextfield只接受数字和几个特定的键盘输入,而且不想显示光标 实现屏蔽输入可以通过Document实现,当然还有其他的途径。只是比不要光标的话怎么实现插入、或者如何定位删除的位置?! 显示是固定格式00:00:00就是普通的时间显示,不需要删除和添加,只是进行数字输入时进行替换,我是用setSelectionStart和setSelectionEnd对要进行变换的值进行替换,把光标Caret.deinstall()后setSelectionEnd报错 屏蔽输入setEditable(false)消除光标用setFocusable(false)试试 setFocusable(false)怎么能获取键盘输入呢?!!!屏蔽键盘就用 Document 实现,自定义光标,来实现消除光标的效果 把光标颜色和背景色设置一样就看不见了package test;import java.awt.Color;import java.awt.event.KeyEvent;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JTextField;public class MyTextField extends JTextField{ public MyTextField(){ setBackground(Color.red); setCaretColor(Color.red); } public void processKeyEvent(KeyEvent e){ if(e.getID() == KeyEvent.KEY_TYPED){ if(Character.isDigit(e.getKeyChar())){ e.setKeyChar((char)KeyEvent.VK_CLEAR); } } super.processKeyEvent(e); } public static void main(String[] args){ new TestFrame(); }}class TestFrame extends JFrame{ public TestFrame(){ setBounds(200, 200, 200, 200); setLayout(null); JTextField textField = new MyTextField(); add(textField); textField.setBounds(10, 10, 50, 20); setVisible(true); } public void processWindowEvent(WindowEvent e){ if(e.getID() == WindowEvent.WINDOW_CLOSING){ System.exit(0); } }} 1、仅仅使用设置光标颜色是不行的,一旦滑选光标就会显现2、不能通过Key监听来实现,你的Ctrl+V就避免不了了,Ctrl+V仍然可以将数字粘进去全盘否定 ctrl + v有两种办法可以解决,一种是他每次操作以后检查文本框内容是否符合,不符合就删除最后一个字符,直到符合为止另一种是禁止他用ctrl + v,就是我下面改的这种"滑选"是什么情况呢,我不明白,不知道setSelectionColor能否解决这个问题package test;import java.awt.Color;import java.awt.event.KeyEvent;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JTextField;public class MyTextField extends JTextField{ private boolean ctrPressed; public MyTextField(){ setBackground(Color.red); setCaretColor(Color.red); setSelectionColor(Color.red); } public void processKeyEvent(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_V && ctrPressed){ return; }else if(e.getID() == KeyEvent.KEY_TYPED){ if(!Character.isDigit(e.getKeyChar())){ e.setKeyChar((char)KeyEvent.VK_CLEAR); } }else if(e.getKeyCode() == KeyEvent.VK_CONTROL){ if(e.getID() == KeyEvent.KEY_PRESSED){ ctrPressed = true; }else if(e.getID() == KeyEvent.KEY_RELEASED){ ctrPressed = false; } } super.processKeyEvent(e); } public static void main(String[] args){ new TestFrame(); }}class TestFrame extends JFrame{ public TestFrame(){ setBounds(200, 200, 200, 200); setLayout(null); JTextField textField = new MyTextField(); add(textField); textField.setBounds(10, 10, 50, 20); setVisible(true); } public void processWindowEvent(WindowEvent e){ if(e.getID() == WindowEvent.WINDOW_CLOSING){ System.exit(0); } }} 哦,我重写了DefaultCaret方法,把光标不再像是出来了。另外我想问下,如何判断控件是第一次获取焦点?public void TimecodeMouseClicked(MouseEvent e){ if(focus){ TimeCodefocus(e); }else{ code.addFocusListener(new FocusAdapter(){ public void Getfocus(FocusEvent e){ TimeCodeGetfocus(e); focus=true; }}}这段代码我想在控件第一次获取焦点的时候执行TimeCodeGetfocus(e),但运行起来发现不执行那段代码, 重写Caret才是正道,设置其他的属性会把你的界面设置得乱七八糟的。 没必要这么做,重载JTextField中的好像是 createDocumen方法就行了,让它返回你自定义的Document,在你的Document中你想做什么就做什么 全选的高亮显示也没什么,自定义一个高亮显示就可以了,去找本《Java Swing》看看,你就会明白了 逼我,给你个Caret的,Document自己实现吧import java.awt.BorderLayout;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.Shape;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.text.DefaultCaret;import javax.swing.text.Highlighter;import javax.swing.text.JTextComponent;public class CornerCaret extends DefaultCaret { public CornerCaret() { } private Highlighter.HighlightPainter lhp = new LineHighlightPainter(); protected Highlighter.HighlightPainter getSelectionPainter() { return lhp; } protected synchronized void damage(Rectangle r) { if (r == null) { return; } x = r.x; y = r.y + (r.height * 4 / 5 - 3); width = 5; height = 5; repaint(); } public void paint(Graphics g) { } class LineHighlightPainter implements Highlighter.HighlightPainter { public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) { } } public static void main(String args[]) { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea area = new JTextArea(8, 32); area.setCaret(new CornerCaret()); area .setText("This is the story\nof the hare who\nlost his spectacles."); frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER); frame.pack(); frame.setVisible(true); }} 高手帮帮忙,一道笔试题 java中random随机数 模拟分店向总店上传下载文件的压力测试 java中数据库sql参数查询 在eclipse中写个连接oracle的数据库程序,运行正常,导出jar文件运行,就连接不上了,还需要什么操作么? javaz中能用方法更改两个数的值吗?类似C语言的指针的效果? zip压缩中参数的中文处理 STACK和HEAP的问题 study_body 进来 请问HttpSessionListener怎么用? 关于表的级联操作 简单Gui画图问题
对应不同的情况有不同的实现,setEditable的话还是可以有光标的哦
消除光标用setFocusable(false)试试
setFocusable(false)怎么能获取键盘输入呢?!!!屏蔽键盘就用 Document 实现,
自定义光标,来实现消除光标的效果
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;public class MyTextField extends JTextField{
public MyTextField(){
setBackground(Color.red);
setCaretColor(Color.red);
}
public void processKeyEvent(KeyEvent e){
if(e.getID() == KeyEvent.KEY_TYPED){
if(Character.isDigit(e.getKeyChar())){
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
}
super.processKeyEvent(e);
}
public static void main(String[] args){
new TestFrame();
}
}class TestFrame extends JFrame{
public TestFrame(){
setBounds(200, 200, 200, 200);
setLayout(null);
JTextField textField = new MyTextField();
add(textField);
textField.setBounds(10, 10, 50, 20);
setVisible(true);
}
public void processWindowEvent(WindowEvent e){
if(e.getID() == WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
}
1、仅仅使用设置光标颜色是不行的,一旦滑选光标就会显现2、不能通过Key监听来实现,你的Ctrl+V就避免不了了,Ctrl+V仍然可以将数字粘进去全盘否定
一种是他每次操作以后检查文本框内容是否符合,不符合就删除最后一个字符,直到符合为止
另一种是禁止他用ctrl + v,就是我下面改的这种
"滑选"是什么情况呢,我不明白,不知道setSelectionColor能否解决这个问题package test;import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;public class MyTextField extends JTextField{
private boolean ctrPressed;
public MyTextField(){
setBackground(Color.red);
setCaretColor(Color.red);
setSelectionColor(Color.red);
}
public void processKeyEvent(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_V && ctrPressed){
return;
}else if(e.getID() == KeyEvent.KEY_TYPED){
if(!Character.isDigit(e.getKeyChar())){
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
}else if(e.getKeyCode() == KeyEvent.VK_CONTROL){
if(e.getID() == KeyEvent.KEY_PRESSED){
ctrPressed = true;
}else if(e.getID() == KeyEvent.KEY_RELEASED){
ctrPressed = false;
}
}
super.processKeyEvent(e);
}
public static void main(String[] args){
new TestFrame();
}
}class TestFrame extends JFrame{
public TestFrame(){
setBounds(200, 200, 200, 200);
setLayout(null);
JTextField textField = new MyTextField();
add(textField);
textField.setBounds(10, 10, 50, 20);
setVisible(true);
}
public void processWindowEvent(WindowEvent e){
if(e.getID() == WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
}
另外我想问下,如何判断控件是第一次获取焦点?public void TimecodeMouseClicked(MouseEvent e){
if(focus){
TimeCodefocus(e);
}else{
code.addFocusListener(new FocusAdapter(){
public void Getfocus(FocusEvent e){
TimeCodeGetfocus(e);
focus=true;
}
}
}
这段代码我想在控件第一次获取焦点的时候执行TimeCodeGetfocus(e),但运行起来发现不执行那段代码,
重写Caret才是正道,设置其他的属性会把你的界面设置得乱七八糟的。
没必要这么做,重载JTextField中的好像是 createDocumen方法就行了,让它返回你自定义的Document,在你的Document中你想做什么就做什么
全选的高亮显示也没什么,自定义一个高亮显示就可以了,去找本《Java Swing》看看,你就会明白了
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;public class CornerCaret extends DefaultCaret { public CornerCaret() {
} private Highlighter.HighlightPainter lhp = new LineHighlightPainter(); protected Highlighter.HighlightPainter getSelectionPainter() {
return lhp;
} protected synchronized void damage(Rectangle r) {
if (r == null) {
return;
}
x = r.x;
y = r.y + (r.height * 4 / 5 - 3);
width = 5;
height = 5;
repaint();
} public void paint(Graphics g) { } class LineHighlightPainter implements Highlighter.HighlightPainter { public void paint(Graphics g, int p0, int p1, Shape bounds,
JTextComponent c) { }
} public static void main(String args[]) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea area = new JTextArea(8, 32);
area.setCaret(new CornerCaret());
area
.setText("This is the story\nof the hare who\nlost his spectacles.");
frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}