日期:2014-05-20  浏览次数:20801 次

java 的textfield类
java 的textfield类怎么选中指定的字符数?是那个方法?

------解决方案--------------------
Java code
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class Example {
    public static void main(final java.lang.String[] args) {
        java.awt.EventQueue.invokeLater(new java.lang.Runnable(){
                @Override public void run(){
                    JFrame frame = new JFrame("Example");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLocationRelativeTo(null);

                    Box container = Box.createVerticalBox();
                    Box top = Box.createHorizontalBox();
                    Box bottom = Box.createHorizontalBox();

                    final JTextField input = new JTextField();

                    final JTextField from = new JTextField("0",3);
                    from.setDocument(new HereDocument(input));
                    final JTextField to = new JTextField("1",3);
                    from.setDocument(new HereDocument(input));
                    final JButton select = new JButton("Select");
                    final JLabel status = new JLabel("Selected Text:");

                    select.addActionListener(new ActionListener(){
                            @Override public void actionPerformed(ActionEvent e){
                                input.setSelectionStart(Integer.parseInt(from.getText()));
                                input.setSelectionEnd(Integer.parseInt(to.getText()));
                                status.setText(String.format("Selected Text: %s",input.getSelectedText()));
                                input.requestFocusInWindow();
                            }
                        });

                    top.add(new JLabel("Input:"));
                    top.add(Box.createHorizontalStrut(3));
                    top.add(input);

                    bottom.add(select);
                    bottom.add(Box.createHorizontalStrut(5));
                    bottom.add(new JLabel("From:"));
                    bottom.add(Box.createHorizontalStrut(3));
                    bottom.add(from);
                    bottom.add(Box.createHorizontalStrut(5));
                    bottom.add(new JLabel("To:"));
                    bottom.add(to);

                    container.add(top);
                    container.add(Box.createVerticalStrut(5));
                    container.add(bottom);
                    container.add(Box.createVerticalStrut(5));
                    container.add(status);
                    container.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
                    frame.setContentPane(container);

                    frame.pack();
                    frame.setVisible(true);
                }
            });
    }
    private static class HereDocument extends PlainDocument {
        private JTextField input;
        public HereDocument(JTextField input){
            this.input = input;
        }

        @Override public void insertString(int offset, String s, AttributeSet as)
            throws BadLocationException {
            if (s.matches("[0-9]*") && ((getLength()+s.length()) <= input.getText().length())) {
                super.insertString(offset,s,as);
            }else {
                return;
            }
        }
    }
}