3

私はjTextfieldとjButtonを持っています..

方法

  • ユーザーはjTextfieldをクリックできます(マウスはjtextfieldに出入りできます)が、ユーザーが何かを入力しても何もしません(テキスト全体を削除するバックスペースを除く)
  • ユーザーがボタンをクリックすると、

jTextfield.setText("何か");

したがって、jtextfield テキストを指定する唯一の方法は、ボタンをクリックすることです

  • そこにテキストがある場合(カーソルがjtextfield内にある場合)、ユーザーがバックスペースを入力すると、jtextfieldのテキスト全体が削除されます..

これを行う方法?

私の英語を許してください..どんな種類の助けにも感謝します..

4

2 に答える 2

5

を使用し、好きなようDocumentFilterに追加するだけです:JTextField

 public class Test {

    public void initComponents() {

        //create frame

        //add DoucmentFilter to JTextField
        MyDocumentFilter myFilter = new MyDocumentFilter();
        JTextField myArea = new JTextField();
        ((AbstractDocument)myArea.getDocument()).setDocumentFilter(myFilter);

         //add components set frame visible
    }

 }

class MyDocumentFilter extends DocumentFilter {

    @Override
    public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
        super.replace(fb, i, i1, string, as);
    }

    @Override
    public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
        super.remove(fb, i, i1);
    }

    @Override
    public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
        super.insertString(fb, i, string, as);
    }

}

代わりに

次のような (再利用性のための)既にあるカスタム JTextFieldを作成したい場合があります。DocumentFilter

public class MyCustomField extends JTextField {

    public MyCustomField(int cols) {
        super(cols);
    }

    protected Document createDefaultModel() {
        return ((Document) new MyDocument());
    }

    static class MyDocument extends DocumentFilter {

        @Override
        public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
            super.insertString(fb, i, string, as);
        }

        @Override
        public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
            super.remove(fb, i, i1);
        }

        @Override
        public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
            super.replace(fb, i, i1, string, as);
        }
    }
}

ホバークラフトからの編集
私はこれらの線に沿ってもっと考えていました

import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.text.*;

public class Test {

   public void initComponents() {

      JPanel panel = new JPanel();
      final MyDocumentFilter myFilter = new MyDocumentFilter();
      final JTextField myArea = new JTextField(20);
      ((AbstractDocument) myArea.getDocument()).setDocumentFilter(myFilter);

      panel.add(myArea);

      panel.add(new JButton(new AbstractAction("Set Text") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            myFilter.setFiltering(false);
            myArea.setText("Fe Fi Fo Fum");
            myFilter.setFiltering(true);
         }
      }));

      JOptionPane.showMessageDialog(null, panel);

      // add components set frame visible
   }

   public static void main(String[] args) {
      new Test().initComponents();
   }

}

class MyDocumentFilter extends DocumentFilter {
   private boolean filtering = true;

   @Override
   public void replace(FilterBypass fb, int i, int i1, String string,
         AttributeSet as) throws BadLocationException {
      if (!filtering) {
         super.replace(fb, i, i1, string, as);
      }
   }

   @Override
   public void remove(FilterBypass fb, int i, int i1)
         throws BadLocationException {
      int offset = 0;
      int length = fb.getDocument().getLength();
      super.remove(fb, offset, length);
   }

   @Override
   public void insertString(FilterBypass fb, int i, String string,
         AttributeSet as) throws BadLocationException {
      if (!filtering) {
         super.insertString(fb, i, string, as);         
      }
   }

   public void setFiltering(boolean filtering) {
      this.filtering = filtering;
   }

}
于 2012-11-22T16:52:20.433 に答える
3

jTextfield のキー リスナーで、keyTyped イベントで、バックスペースの場合は e.getKeyChar() をチェックし、そうでない場合は e.consume(); を実行します。それはイベントをキャンセルします

バックスペースのキー文字は 8 です

以下に例を示します。

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class ConsumeExceptForBackSpace extends JFrame {

    private boolean canWrite = false;

    public ConsumeExceptForBackSpace() {
        super();

        JButton b = new JButton("Click");
        JTextField f = new JTextField("");
        this.setLayout(new BorderLayout());
        this.add(b, BorderLayout.CENTER);
        this.add(f, BorderLayout.SOUTH);

        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                canWrite = !canWrite;
            }
        });

        f.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyChar() != KekEvent.VK_BACK_SPACE && !canWrite) e.consume();
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {

            }
        });

        this.pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new ConsumeExceptForBackSpace();
    }
}
于 2012-11-22T16:52:11.043 に答える