如何在按下的数字上显示文本字段上星号的确切数量,而不是在按下输入时添加额外的星号?

Posted

技术标签:

【中文标题】如何在按下的数字上显示文本字段上星号的确切数量,而不是在按下输入时添加额外的星号?【英文标题】:How do you show the exact number of asterisk on the textfield to the numbers pressed and not add an additional asterisk when enter is pressed? 【发布时间】:2021-12-07 01:08:01 【问题描述】:

我现在的问题是如何在文本字段上显示确切的星号数量,而不是添加额外的星号?因为每当我按下回车按钮时,它都会继续在文本字段上添加另一个星号。我一直在尝试我应该在哪里以及应该放什么以避免在按下回车按钮时添加额外的星号

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

public class Pincode extends JPanel implements ActionListener 
JLabel display, displayExtra;
JButton numButton;
JButton clr;
JButton etr;
String displayContent = "";
String[] numPadContent = "1", "2", "3", "4", "5", "6", "7", "8", "9", "CLEAR", "0", "ENTER";
ArrayList<JButton> buttonList;
String PINCODE = "061904";
JFrame f;
Pincode(Container pane) 
    pane.setPreferredSize(new Dimension(320, 335));
    display = new JLabel(displayContent);
    displayExtra = new JLabel(displayContent);
    display.setPreferredSize(new Dimension(320, 30));
    display.setBorder(BorderFactory.createLoweredBevelBorder());
    pane.add(display, BorderLayout.PAGE_START);
    buttonList = new ArrayList<JButton>(12);
    JPanel numberPanel = new JPanel();
    numberPanel.setLayout(new GridLayout(4, 3, 0, 0));
    numberPanel.setPreferredSize(new Dimension(320, 260));
    for (int i = 0; i < numPadContent.length; i++) 
        numButton = new JButton(numPadContent[i]);
        buttonList.add(numButton);
    
    for (int n = 0; n < buttonList.size(); n++) 
        buttonList.get(n).addActionListener(this);
        numberPanel.add(buttonList.get(n));
    
    pane.add(numberPanel, BorderLayout.LINE_END);
    clr = new JButton("Clear");
    clr.setPreferredSize(new Dimension(160, 30));
    clr.addActionListener(this);
    etr = new JButton("Enter");
    etr.setPreferredSize(new Dimension(160, 30));
    etr.addActionListener(this);
    pane.add(clr);
    pane.add(etr);

public void Authorized()
    JOptionPane.showMessageDialog(f,"You are authorized");

public void Unauthorized()
    JOptionPane.showMessageDialog(f,"You are not authorized!!!");

public void actionPerformed(ActionEvent e) 
    String textThere = display.getText(), textThereExtra = displayExtra.getText();
    String additionalText = "";
    for (int a = 0; a < buttonList.size(); a++) 
        if (e.getSource().equals(buttonList.get(a)) && a!=11 && a!=9) 
            additionalText = buttonList.get(a).getText();
        
    
    int flag = 0;
    if (e.getSource().equals(buttonList.get(9))) 
        textThere = "";
        textThereExtra = "";
        flag = 1;
    
    if(flag!=1) 
        display.setText(textThere.concat("*"));
        displayExtra.setText(textThereExtra.concat(additionalText));
    
    else
    
        display.setText(textThere);
        displayExtra.setText(textThereExtra);
    

    if (e.getSource().equals(buttonList.get(11))) 
        System.out.println(textThereExtra);
        System.out.println(textThere);
        if(textThereExtra.equals(PINCODE)) 
            Authorized();
        
        else 
            Unauthorized();
        
    



public static void main(String[] args) 
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new Pincode(frame));
    frame.pack();
    frame.setVisible(true);


请帮助我编写代码。非常感谢!

【问题讨论】:

【参考方案1】:

如果你不打算为每个按钮创建匿名类,那么你应该看看actionCommandActionEvent

public void actionPerformed(ActionEvent e) 
    String textThere = display.getText(), textThereExtra = displayExtra.getText();
    String additionalText = "";

    if ("Enter".equalsIgnoreCase(e.getActionCommand())) 
        System.out.println(textThereExtra);
        System.out.println(textThere);
        if (textThereExtra.equals(PINCODE)) 
            Authorized();
         else 
            Unauthorized();
        
     else if ("Clear".equalsIgnoreCase(e.getActionCommand())) 
        textThere = "";
        textThereExtra = "";
        display.setText("");
        displayExtra.setText("");
     else 
        for (int a = 0; a < buttonList.size(); a++) 
            if (e.getSource().equals(buttonList.get(a)) && a != 11 && a != 9) 
                additionalText = buttonList.get(a).getText();
            
        
        display.setText(textThere.concat("*"));
        displayExtra.setText(textThereExtra.concat(additionalText));
    

nb:这是对您的代码的快速修改,可能无法 100% 满足您的需求,但应该让您朝着正确的方向前进

【讨论】:

以上是关于如何在按下的数字上显示文本字段上星号的确切数量,而不是在按下输入时添加额外的星号?的主要内容,如果未能解决你的问题,请参考以下文章

TextField 问题中的 TextSelection

如果用户在返回键盘之前按下按钮,如何将按钮操作应用于文本字段

只要按下键盘上的x按钮,如何从一个文本字段中删除值

如何检测按下的按钮并将其显示在另一个视图上? Objective-C

标签在按钮上,如何在iOS7中突出显示按钮按下的文本?

清除按钮按下swiftui时如何自动上一个文本字段