[英]Cant add to arraylist inside actionevent Buttonclicked
Im kind of new to programming in java but here im trying to figure out how to add the value of a button clicked in ActionEvent.我对 java 中的编程有点陌生,但在这里我试图弄清楚如何添加在 ActionEvent 中单击的按钮的值。 I even changed the actionlistener to MouseListener etc but still not working.
我什至将 actionlistener 更改为 MouseListener 等,但仍然无法正常工作。 It only displays the current button clicked but i would like to add every button clicked into an arraylist.
它只显示当前单击的按钮,但我想将每个单击的按钮添加到 arraylist 中。
Problem: I cant add the value of buttons clicked into an arraylist.问题:我无法将点击按钮的值添加到 arraylist 中。
Example: if i click on the button named E then i want E to be added to arraylist, after that if i click on the button S then it should add S o the same arraylist.示例:如果我单击名为 E 的按钮,那么我希望将 E 添加到 arraylist,之后如果我单击按钮 S,那么它应该添加相同的 arraylist。 but its not adding aything to the arraylist.
但它并没有为 arraylist 添加任何内容。 it only displays the current button clicked
它只显示当前单击的按钮
public class WordFinder extends JFrame implements ActionListener {
private JButton buttons[];
ArrayList<String> LettersClicked = new ArrayList<String>();
private JTextArea jta;
private JLabel jLabel;
public WordFinder(){
int numberOfLetters = 64;
buttons = new JButton[numberOfLetters];
JFrame frame = new JFrame("wordfinder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.red);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
topPanel.setPreferredSize(new Dimension(600, 600));
topPanel.setMaximumSize(new Dimension(600, 600));
topPanel.setBackground(Color.red);
JPanel bottomPanel = new JPanel();
bottomPanel.setBorder(BorderFactory.createLineBorder(Color.yellow));
bottomPanel.setPreferredSize(new Dimension(600, 300));
bottomPanel.setMaximumSize(new Dimension(600, 300));
bottomPanel.setBackground(Color.green);
Font f3 = new Font(Font.DIALOG, Font.BOLD, 35);
for (int i = 0;i<numberOfLetters;i++) {
char letter = alphabet();
buttons[i] = new JButton(String.valueOf(letter));
buttons[i].setFont(f3);
// buttons[i].setMinimumSize(new Dimension(40, 20));
buttons[i].setPreferredSize(new Dimension(65, 65));
buttons[i].setMargin(new Insets(0,0,0,0));
buttons[i].setBackground(Color.white);
buttons[i].setFocusPainted(false);
buttons[i].addActionListener(this);
topPanel.add(buttons[i]);
}
String buttValue = "";
jLabel = new JLabel(""+buttValue);
jLabel.setFont(f3);
bottomPanel.add(jLabel);
LettersClicked.add(jLabel.toString());
for (int z = 0; z<LettersClicked.size(); z++){
JLabel aa = new JLabel(""+LettersClicked.size());
bottomPanel.add(aa);
}
mainPanel.add(topPanel);
mainPanel.add(bottomPanel);
frame.getContentPane().add(mainPanel);
frame.setSize(1000,1000);
frame.setMinimumSize(new Dimension(1000, 1000));
frame.setVisible(true);
}
public char alphabet(){
Random rnd = new Random();
String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char letter = alphabets.charAt(rnd.nextInt(alphabets.length()));
return letter;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
JButton button = (JButton) actionEvent.getSource();
jLabel.setText(button.getText());
LettersClicked.add(button.getText());
}
}
I think you are confused about the content of the JLabel
displayed on the GUI and your ArrayList
.我认为您对 GUI 上显示的
JLabel
和您的ArrayList
的内容感到困惑。 The issue is actually in the former.问题其实出在前者。
Your current implementation replaces the text of the JLabel
instead of appending it for each pressed button.您当前的实现替换了
JLabel
的文本,而不是为每个按下的按钮附加它。
So, in your actionPerformed()
method, change this line因此,在您的
actionPerformed()
方法中,更改此行
jLabel.setText(button.getText());
to至
jLabel.setText(jLabel.getText() + button.getText());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.