我現在的問題是如何在文本欄位上顯示星號的確切數量以顯示按下了多少個數字而不添加額外的星號?因為每當我按下輸入按鈕時,它都會在文本欄位上添加另一個星號。我一直在嘗試我應該放在哪里以及放什么來避免在按下 Enter 按鈕時添加額外的星號
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);
}
}
請幫我處理我的代碼。非常感謝!
uj5u.com熱心網友回復:
如果你不打算要提出的是匿名類的每個按鈕,那么你應該看看actionCommand的ActionEvent
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));
}
}
注意:這是對您的代碼的快速重新處理,這可能不是 100% 正確滿足您的需求,但應該讓您朝著正確的方向前進
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/327033.html
