无法添加到 arraylist inside actionevent Buttonclicked
Cant add to arraylist inside actionevent Buttonclicked
我是 java 编程的新手,但我想弄清楚如何添加在 ActionEvent 中单击的按钮的值。我什至将 actionlistener 更改为 MouseListener 等,但仍然无法正常工作。它只显示当前点击的按钮,但我想将每个点击的按钮添加到数组列表中。
问题:我无法将单击的按钮的值添加到数组列表中。
示例:如果我单击名为 E 的按钮,然后我希望将 E 添加到数组列表中,之后如果我单击按钮 S,则它应该添加 S o 相同的数组列表。
但它没有向 arraylist 添加任何东西。它只显示当前点击的按钮
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());
}
}
我认为您对 GUI 上显示的 JLabel
内容和您的 ArrayList
感到困惑。其实问题出在前者
您当前的实现替换了 JLabel
的文本,而不是为每个按下的按钮附加它。
因此,在您的 actionPerformed()
方法中,更改此行
jLabel.setText(button.getText());
到
jLabel.setText(jLabel.getText() + button.getText());
我是 java 编程的新手,但我想弄清楚如何添加在 ActionEvent 中单击的按钮的值。我什至将 actionlistener 更改为 MouseListener 等,但仍然无法正常工作。它只显示当前点击的按钮,但我想将每个点击的按钮添加到数组列表中。
问题:我无法将单击的按钮的值添加到数组列表中。
示例:如果我单击名为 E 的按钮,然后我希望将 E 添加到数组列表中,之后如果我单击按钮 S,则它应该添加 S o 相同的数组列表。 但它没有向 arraylist 添加任何东西。它只显示当前点击的按钮
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());
}
}
我认为您对 GUI 上显示的 JLabel
内容和您的 ArrayList
感到困惑。其实问题出在前者
您当前的实现替换了 JLabel
的文本,而不是为每个按下的按钮附加它。
因此,在您的 actionPerformed()
方法中,更改此行
jLabel.setText(button.getText());
到
jLabel.setText(jLabel.getText() + button.getText());