【问题标题】:"append" text for JTextFieldJTextField 的“附加”文本
【发布时间】:2015-07-10 11:31:31
【问题描述】:

我正在尝试制作一个小型应用程序,其中 U 输入 ID(在 JButton 的帮助下 - 从 0 到 9 - )然后将已按下的数字传递给 putText-Method,然后将其显示在 JTextField 中 - 问题是每次我按新号码时,我在消失之前按的那个号码:s。有人可以帮我吗?

public class IdPanel extends JPanel {

    private JTextField idField;
    private JLabel idLabel;

    public IdPanel() {

        setLayout(new GridBagLayout());

        setPreferredSize(new Dimension(500, 70));

        idField = new JTextField(20);
        idField.setFont(new Font("Serif", Font.PLAIN, 20));

        idLabel = new JLabel("Enter ID:");
        idLabel.setFont(new Font("Serif", Font.PLAIN, 20));

        GridBagConstraints gc = new GridBagConstraints();

        gc.gridx = 0;
        gc.gridy = 0;
        gc.insets = new Insets(9, 9, 9, 9);
        gc.anchor = GridBagConstraints.CENTER;
        add(idLabel, gc);

        gc.gridx = 1;
        gc.anchor = GridBagConstraints.CENTER;
        add(idField, gc);
    }

    public void putText(String number) {
        idField.setText(number);
    }
}

【问题讨论】:

    标签: java swing append jtextfield


    【解决方案1】:

    尝试:

    idField.setText(idField.getText()+number);
    

    【讨论】:

      【解决方案2】:

      借助 JButton - 从 0 到 9 -

      这是一个有 9 个按钮的示例:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.border.*;
      
      public class CalculatorPanel extends JPanel
      {
          private JTextField display;
      
          public CalculatorPanel()
          {
              Action numberAction = new AbstractAction()
              {
                  @Override
                  public void actionPerformed(ActionEvent e)
                  {
      //              display.setCaretPosition( display.getDocument().getLength() );
                      display.replaceSelection(e.getActionCommand());
                  }
              };
      
              setLayout( new BorderLayout() );
      
              display = new JTextField();
              display.setEditable( false );
              display.setHorizontalAlignment(JTextField.RIGHT);
              add(display, BorderLayout.NORTH);
      
              JPanel buttonPanel = new JPanel();
              buttonPanel.setLayout( new GridLayout(0, 5) );
              add(buttonPanel, BorderLayout.CENTER);
      
              for (int i = 0; i < 10; i++)
              {
                  String text = String.valueOf(i);
                  JButton button = new JButton( text );
                  button.addActionListener( numberAction );
                  button.setBorder( new LineBorder(Color.BLACK) );
                  button.setPreferredSize( new Dimension(50, 50) );
                  buttonPanel.add( button );
      
                  InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                  inputMap.put(KeyStroke.getKeyStroke(text), text);
                  inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
                  button.getActionMap().put(text, numberAction);
              }
          }
      
          private static void createAndShowUI()
          {
      //      UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );
      
              JFrame frame = new JFrame("Calculator Panel");
              frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              frame.add( new CalculatorPanel() );
              frame.pack();
              frame.setLocationRelativeTo( null );
              frame.setVisible(true);
          }
      
          public static void main(String[] args)
          {
              EventQueue.invokeLater(new Runnable()
              {
                  public void run()
                  {
                      createAndShowUI();
                  }
              });
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-24
        • 2016-10-02
        • 1970-01-01
        • 2014-07-22
        • 2016-07-30
        • 2011-06-30
        相关资源
        最近更新 更多