本文介绍了JTextField中的预定义文本不可编辑,但可以将其他文本附加到其中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在经历Java摇摆时,我遇到了这个问题.我有一个JTextField,它具有预定义的且不可编辑的文本.用户应该能够向其添加其他文本,但无需编辑预定义的文本.是否有任何方法可以获取此解决方案或其他任何方法?

While going through Java swing I faced this problem.I have a JTextField which has predefined and not editable text. the user should be able to append other text to it but without editing the predefined text. Is there any method to obtain this solution or any other?

推荐答案

使用

  1. JComboBox(不可编辑)

JSpinner SpinnerListModel

最初由@camickr制作

originally made by @camickr

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

public class NavigationFilterPrefixWithBackspace extends NavigationFilter {

    private int prefixLength;
    private Action deletePrevious;

    public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component) {
        this.prefixLength = prefixLength;
        deletePrevious = component.getActionMap().get("delete-previous");
        component.getActionMap().put("delete-previous", new BackspaceAction());
        component.setCaretPosition(prefixLength);
    }

    @Override
    public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
        fb.setDot(Math.max(dot, prefixLength), bias);
    }

    @Override
    public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
        fb.moveDot(Math.max(dot, prefixLength), bias);
    }

    class BackspaceAction extends AbstractAction {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            JTextComponent component = (JTextComponent) e.getSource();
            if (component.getCaretPosition() > prefixLength) {
                deletePrevious.actionPerformed(null);
            }
        }
    }

    public static void main(String args[]) throws Exception {
        JTextField textField = new JTextField(" $ ", 20);
        textField.setNavigationFilter(new NavigationFilterPrefixWithBackspace(textField.getDocument().getLength(), textField));
        JFrame frame = new JFrame("Navigation Filter Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(textField);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

  • 我建议通过将JLabel区域的JTextField中的Insets(输入区域)更改来使用OverlayLayout(在JTextField之上的JLabel),否则JTextField中的任何格式都会使该线程中的代码和建议非常无用,并且输出奇怪到Swing GUI

    • I'd suggest to use OverlayLayout (JLabel over JTextField), by changing Insets (input area) in JTextField for JLabels area, otherwise any formatting in JTextField make code and suggestion in this thread quite useless and with strange output to the Swing GUI

      例如JTextField.setHorizontalAlignment(JTextField.RIGHT);

      编辑

      • 放入JLabel& JTextField到JPanel,非常简单,没有副作用

      • put JLabel & JTextField to JPanel, quite simple and without side effects

      更改

      需要在JLabel中的文本更改的情况下调用revalidate()和repaint()

      required to call revalidate() and repaint() in the case that text in JLabel is changed

      import java.awt.BorderLayout;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JTextField;
      import javax.swing.SwingUtilities;
      
      public class NavigationFilterBias {
      
          private JFrame frame = new JFrame("Navigation Filter Example");
          private JPanel panel = new JPanel();
          private JLabel label = new JLabel(" $ ");
          private JTextField textField = new JTextField();
      
          public NavigationFilterBias() {
              panel.setBorder(textField.getBorder());
              panel.setBackground(textField.getBackground());
              panel.setLayout(new BorderLayout());
              panel.add(label,BorderLayout.WEST);
              textField.setBorder(null);
              panel.add(textField,BorderLayout.CENTER);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.getContentPane().add(panel);
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
          }
      
          public static void main(String args[]) throws Exception {
              SwingUtilities.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      NavigationFilterBias exam = new NavigationFilterBias();
                  }
              });
          }
      }
      

      这篇关于JTextField中的预定义文本不可编辑,但可以将其他文本附加到其中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:50