当前位置: 代码迷 >> J2SE >> java 的textfield类解决办法
  详细解决方案

java 的textfield类解决办法

热度:118   发布时间:2016-04-24 02:18:13.0
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;            }        }    }}
  相关解决方案