如题

解决方案 »

  1.   

    从类 javax.swing.text.JTextComponent 继承的方法:setEditable,设置成false就不可编辑了。下次还是自己查API吧老兄。
      

  2.   


    对应不同的情况有不同的实现,setEditable的话还是可以有光标的哦
      

  3.   

    我想要这个Jtextfield只接受数字和几个特定的键盘输入,而且不想显示光标
      

  4.   

    实现屏蔽输入可以通过Document实现,当然还有其他的途径。只是比不要光标的话怎么实现插入、或者如何定位删除的位置?!
      

  5.   

    显示是固定格式00:00:00就是普通的时间显示,不需要删除和添加,只是进行数字输入时进行替换,我是用setSelectionStart和setSelectionEnd对要进行变换的值进行替换,把光标Caret.deinstall()后setSelectionEnd报错
      

  6.   

    屏蔽输入setEditable(false)
    消除光标用setFocusable(false)试试
      

  7.   


    setFocusable(false)怎么能获取键盘输入呢?!!!屏蔽键盘就用 Document 实现,
    自定义光标,来实现消除光标的效果
      

  8.   

    把光标颜色和背景色设置一样就看不见了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);
    }
    }
    }
      

  9.   


    1、仅仅使用设置光标颜色是不行的,一旦滑选光标就会显现2、不能通过Key监听来实现,你的Ctrl+V就避免不了了,Ctrl+V仍然可以将数字粘进去全盘否定
      

  10.   

    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);
    }
    }
    }
      

  11.   

    哦,我重写了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),但运行起来发现不执行那段代码,
      

  12.   


    重写Caret才是正道,设置其他的属性会把你的界面设置得乱七八糟的。
      

  13.   


    没必要这么做,重载JTextField中的好像是 createDocumen方法就行了,让它返回你自定义的Document,在你的Document中你想做什么就做什么
      

  14.   


    全选的高亮显示也没什么,自定义一个高亮显示就可以了,去找本《Java Swing》看看,你就会明白了
      

  15.   

    逼我,给你个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);
    }
    }